SlideShare a Scribd company logo
Python Certification Training https://www.edureka.co/python
Agenda
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
Agenda
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
Agenda
Introduction 01
Introduction to
Advanced Python
Getting Started 02
Concepts 03
Practical Approach 04
Python and Shell
Looking at code to
understand theory
Key Concepts in Python
Python Certification Training https://www.edureka.co/python
Introduction to Advanced Python
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
Introduction To Advanced Python
Python is an interpreted, high-level, general-purpose
programming language.
What is Python?
Advanced: Other
Languages
Learning
Advanced Python
Python Certification Training https://www.edureka.co/python
What Is Advanced Python
Computer Science5
Numerical Compute6
Databases7
Sys Programming 1
Graph Theory 2
Mathematics 3
Python spreading its wings across multiple dimensions and
use-cases in many fields
What is “Advanced”?
Python Certification Training https://www.edureka.co/python
Introduction To System Programming
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
System Programming
Sys - Module
Hello Advanced Python!
Import sys
Provides information about constants, functions
and methods of the Python interpreter.
Data streams
stdout stderrstdin
>>> import sys
>>> sys.version
'2.6.5 (r265:79063, Apr 16 2010,
13:57:41) n[GCC 4.4.3]'
>>> sys.version_info
(2, 6, 5, 'final', 0)
>>>
Python Certification Training https://www.edureka.co/python
Command – Line Arguments
Sys - Module
Command Line
argcargv
#!/usr/bin/python
import sys
# it's easy to print this list of course:
print sys.argv
# or it can be iterated via a for loop:
for i in range(len(sys.argv)):
if i == 0:
print "Function name: %s" % sys.argv[0]
else:
print "%d. argument: %s" % (i,sys.argv[i])
$ python arguments.py arg1 arg2
['arguments.py', 'arg1', 'arg2']
Function name: arguments.py
1. argument: arg1
2. argument: arg2
$
Python Certification Training https://www.edureka.co/python
Changing Output Behaviour
Sys - Module
Python
Interactive Mode
Get Output
Write expression
>>> import sys
>>> x = 42
>>> x
42
>>> import sys
>>> def my_display(x):
... print "out: ",
... print x
...
>>> sys.displayhook = my_display
>>> x
out: 42
>>>
>>> print x
42
Behaviour of print() not changed
Python Certification Training https://www.edureka.co/python
Standard Data Streams
Sys - Module
>>> import sys
>>> for i in (sys.stdin, sys.stdout, sys.stderr):
... print(i)
...
', mode 'w' at 0x7f3397a2c150>
', mode 'w' at 0x7f3397a2c1e0>
>>>
SHELLstdin stdout
stderr
>>> import sys
>>> print "Going via stdout"
Going via stdout
>>> sys.stdout.write("Another way to do it!n")
Another way to do it!
>>> x = raw_input("read value via stdin: ")
read value via stdin: 42
>>> print x
42
>>> print "type in value: ", ; sys.stdin.readline()[:-1]
type in value: 42
'42'
>>>
Python Certification Training https://www.edureka.co/python
Redirections
Sys - Module
import sys
print("Coming through stdout")
# stdout is saved
save_stdout = sys.stdout
fh = open("test.txt","w")
sys.stdout = fh
print("This line goes to test.txt")
# return to normal:
sys.stdout = save_stdout
fh.close()
SHELLstdin stdout
stderr
import sys
save_stderr = sys.stderr
fh = open("errors.txt","w")
sys.stderr = fh
x = 10 / 0
# return to normal:
sys.stderr = save_stderr
fh.close()
Python Certification Training https://www.edureka.co/python
Variables & Constants In Sys Module
Sys - Module
byteorder
executable
maxint
modules
path
platform
Variables Constants
Python Certification Training https://www.edureka.co/python
Python And Shell
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
Python And Shell
A piece of software that provides an interface for a user to some
other software or the operating system
What is “Shell”?
SHELL
GUICUI
Bourne-Shell
C-Shell
Bash Shell
Python Certification Training https://www.edureka.co/python
System Programming
The activity of programming system components or system
software
What is “SP”?
“System focussed programming”
Modules
sys os
Simple and clear
Well structured
Highly flexible
Advantages
Python Certification Training https://www.edureka.co/python
The ‘os’ Module
The os module allows platform independent programming by
providing abstract methods
What is “os” module?
Executing Shell scripts with os.system()
import os
def getch():
os.system("bash -c "read -n 1"")
getch()
from msvcrt import getch
Python Certification Training https://www.edureka.co/python
The ‘os’ Module
Executing Shell scripts with os.system()
import os, platform
if platform.system() == "Windows":
import msvcrt
def getch():
if platform.system() == "Linux":
os.system("bash -c "read -n
1"")
else:
msvcrt.getch()
print("Type a key!")
getch()
print("Okay")
Subprocess Module
os.system
os.spawn*
os.popen*
Python Certification Training https://www.edureka.co/python
Forking In Python
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
Fork In Python
What is Forking?
import os
def child():
print('nA new child ', os.getpid())
os._exit(0)
def parent():
while True:
newpid = os.fork()
if newpid == 0:
child()
else:
pids = (os.getpid(), newpid)
print("parent: %d, child: %dn"
% pids)
reply = input("q for quit / c for
new fork")
if reply == 'c':
continue
else:
break
parent()
Process
Copy of Process
Parent
Child
Starting independent processes with fork()
exec*() function
Python Certification Training https://www.edureka.co/python
The exec*() Functions
exec*() functions
• os.execl(path, arg0, arg1, ...)
• os.execle(path, arg0, arg1, ..., env)
• os.execlp(file, arg0, arg1, ...)
• os.execlpe(file, arg0, arg1, ..., env)
• os.execv(path, args)
• os.execve(path, args, env)
• os.execvp(file, args)
• os.execvpe(file, args, env)
Python Certification Training https://www.edureka.co/python
Threads In Python
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
What Is A Thread?
The smallest unit that can be scheduled in an operating systemWhat is a thread?
How to create a thread?
By forking a computer program in
two or more parallel tasks
Types of threads
Kernel threads User threads
Implemented in kernel NOT implemented in kernel
Python Certification Training https://www.edureka.co/python
What Is A Thread?
Global Variables
Process
Local variables
Code
THREAD
Local variables
Code
THREAD
Local variables
Code
THREAD
Python Certification Training https://www.edureka.co/python
The Thread Module
from thread import start_new_thread
def heron(a):
"""Calculates the square root of a"""
eps = 0.0000001
old = 1
new = 1
while True:
old,new = new, (new + a/new) / 2.0
print old, new
if abs(new - old) < eps:
break
return new
start_new_thread(heron,(99,))
start_new_thread(heron,(999,))
start_new_thread(heron,(1733,))
c = raw_input("Type something to quit.")
Example
Python Certification Training https://www.edureka.co/python
The Thread Module
from thread import start_new_thread
num_threads = 0
def heron(a):
global num_threads
num_threads += 1
# code has been left out, see above
num_threads -= 1
return new
start_new_thread(heron,(99,))
start_new_thread(heron,(999,))
start_new_thread(heron,(1733,))
start_new_thread(heron,(17334,))
while num_threads > 0:
pass
Previous example with counters for the threads
• Reading the value of num_thread
• A new int instance will be incremented or
decremented by 1
• the new value has to be assigned to
num_threads
Python Certification Training https://www.edureka.co/python
The Thread Module
from thread import start_new_thread, allocate_lock
num_threads = 0
thread_started = False
lock = allocate_lock()
def heron(a):
global num_threads, thread_started
lock.acquire()
num_threads += 1
thread_started = True
lock.release()
...
lock.acquire()
num_threads -= 1
lock.release()
return new
start_new_thread(heron,(99,))
start_new_thread(heron,(999,))
start_new_thread(heron,(1733,))
while not thread_started:
pass
while num_threads > 0:
pass
The solution
Python Certification Training https://www.edureka.co/python
Threading Module
import time
from threading import Thread
def sleeper(i):
print "thread %d sleeps for 5 seconds" % i
time.sleep(5)
print "thread %d woke up" % i
for i in range(10):
t = Thread(target=sleeper, args=(i,))
t.start()
Example
The Thread of the example doesn't do a lot, essentially it just
sleeps for 5 seconds and then prints out a message:
Python Certification Training https://www.edureka.co/python
Threading Module
thread 0 sleeps for 5 seconds
thread 1 sleeps for 5 seconds
thread 2 sleeps for 5 seconds
thread 3 sleeps for 5 seconds
thread 4 sleeps for 5 seconds
thread 5 sleeps for 5 seconds
thread 6 sleeps for 5 seconds
thread 7 sleeps for 5 seconds
thread 8 sleeps for 5 seconds
thread 9 sleeps for 5 seconds
thread 1 woke up
thread 0 woke up
thread 3 woke up
thread 2 woke up
thread 5 woke up
thread 9 woke up
thread 8 woke up
thread 7 woke up
thread 6 woke up
thread 4 woke up
Python Certification Training https://www.edureka.co/python
Pipes In Python
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
What Are Pipes
Introducing modularity in the program to serve one instance output as
the input to another instance and so on till solution is achieved
What are pipes?
Two kinds of pipes
Named PipesAnonymous Pipes
Exist solely within processes and are usually
used in combination with forks
P1
P2
P3
P4
Python Certification Training https://www.edureka.co/python
Pipes – A Classic Example
99 Bottles of Beer
Drinking is injurious to health
Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer.
Take one down, pass it around, Ninety-eight bottles of beer on the wall.
Lyrics of the song:
Python Certification Training https://www.edureka.co/python
Named Pipes
Called FIFOs – Creating pipes which are implemented as files.What are named pipes?
First In – First Out
A process reads from and writes to such a pipe as if it
were a regular file. Sometimes more than one process
write to such a pipe but only one process reads from it.
Python Certification Training https://www.edureka.co/python
Conclusion
Advanced Python Tutorial
Python Certification Training https://www.edureka.co/python
Conclusion
Advanced Python, yay!
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programming Training | Edureka
Ad

More Related Content

What's hot (20)

Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
Tahani Al-Manie
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Data Visualization(s) Using Python
Data Visualization(s) Using PythonData Visualization(s) Using Python
Data Visualization(s) Using Python
Aniket Maithani
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
Shivam Gupta
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
Kamal Acharya
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
srinivasanr281952
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its Applications
Abhijeet Singh
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
Tahani Al-Manie
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Data Visualization(s) Using Python
Data Visualization(s) Using PythonData Visualization(s) Using Python
Data Visualization(s) Using Python
Aniket Maithani
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
Shivam Gupta
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
Kamal Acharya
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its Applications
Abhijeet Singh
 

Similar to Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programming Training | Edureka (20)

Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
vceder
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
SugumarSarDurai
 
First look on python
First look on pythonFirst look on python
First look on python
senthil0809
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
Chui-Wen Chiu
 
Mufix Network Programming Lecture
Mufix Network Programming LectureMufix Network Programming Lecture
Mufix Network Programming Lecture
SiliconExpert Technologies
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
ericholscher
 
Python_intro.ppt
Python_intro.pptPython_intro.ppt
Python_intro.ppt
Mariela Gamarra Paredes
 
iNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGE
iNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGEiNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGE
iNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGE
shuhbou39
 
Python1
Python1Python1
Python1
AllsoftSolutions
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 Tutorial
Justin Lin
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
Matt Harrison
 
Wait, IPython can do that?
Wait, IPython can do that?Wait, IPython can do that?
Wait, IPython can do that?
Sebastian Witowski
 
Python lec1
Python lec1Python lec1
Python lec1
Swarup Ghosh
 
Python para equipos de ciberseguridad
Python para equipos de ciberseguridad Python para equipos de ciberseguridad
Python para equipos de ciberseguridad
Jose Manuel Ortega Candel
 
Python for Engineers and Architects Stud
Python for Engineers and Architects StudPython for Engineers and Architects Stud
Python for Engineers and Architects Stud
RaviRamachandraR
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
ashukiller7
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Diego Freniche Brito
 
Introduction to Python and Basic Syntax.pptx
Introduction to Python and Basic Syntax.pptxIntroduction to Python and Basic Syntax.pptx
Introduction to Python and Basic Syntax.pptx
GevitaChinnaiah
 
AI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python DevsAI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python Devs
Amr Shawqy
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
vceder
 
First look on python
First look on pythonFirst look on python
First look on python
senthil0809
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
Oon Arfiandwi
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
ericholscher
 
iNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGE
iNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGEiNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGE
iNTRODUCATION TO PYTHON IN PROGRAMMING LANGUAGE
shuhbou39
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 Tutorial
Justin Lin
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
Matt Harrison
 
Python for Engineers and Architects Stud
Python for Engineers and Architects StudPython for Engineers and Architects Stud
Python for Engineers and Architects Stud
RaviRamachandraR
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
ashukiller7
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Diego Freniche Brito
 
Introduction to Python and Basic Syntax.pptx
Introduction to Python and Basic Syntax.pptxIntroduction to Python and Basic Syntax.pptx
Introduction to Python and Basic Syntax.pptx
GevitaChinnaiah
 
AI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python DevsAI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python Devs
Amr Shawqy
 
Ad

More from Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
Ad

Recently uploaded (20)

Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 

Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programming Training | Edureka

  • 1. Python Certification Training https://www.edureka.co/python Agenda Advanced Python Tutorial
  • 2. Python Certification Training https://www.edureka.co/python Agenda Advanced Python Tutorial
  • 3. Python Certification Training https://www.edureka.co/python Agenda Introduction 01 Introduction to Advanced Python Getting Started 02 Concepts 03 Practical Approach 04 Python and Shell Looking at code to understand theory Key Concepts in Python
  • 4. Python Certification Training https://www.edureka.co/python Introduction to Advanced Python Advanced Python Tutorial
  • 5. Python Certification Training https://www.edureka.co/python Introduction To Advanced Python Python is an interpreted, high-level, general-purpose programming language. What is Python? Advanced: Other Languages Learning Advanced Python
  • 6. Python Certification Training https://www.edureka.co/python What Is Advanced Python Computer Science5 Numerical Compute6 Databases7 Sys Programming 1 Graph Theory 2 Mathematics 3 Python spreading its wings across multiple dimensions and use-cases in many fields What is “Advanced”?
  • 7. Python Certification Training https://www.edureka.co/python Introduction To System Programming Advanced Python Tutorial
  • 8. Python Certification Training https://www.edureka.co/python System Programming Sys - Module Hello Advanced Python! Import sys Provides information about constants, functions and methods of the Python interpreter. Data streams stdout stderrstdin >>> import sys >>> sys.version '2.6.5 (r265:79063, Apr 16 2010, 13:57:41) n[GCC 4.4.3]' >>> sys.version_info (2, 6, 5, 'final', 0) >>>
  • 9. Python Certification Training https://www.edureka.co/python Command – Line Arguments Sys - Module Command Line argcargv #!/usr/bin/python import sys # it's easy to print this list of course: print sys.argv # or it can be iterated via a for loop: for i in range(len(sys.argv)): if i == 0: print "Function name: %s" % sys.argv[0] else: print "%d. argument: %s" % (i,sys.argv[i]) $ python arguments.py arg1 arg2 ['arguments.py', 'arg1', 'arg2'] Function name: arguments.py 1. argument: arg1 2. argument: arg2 $
  • 10. Python Certification Training https://www.edureka.co/python Changing Output Behaviour Sys - Module Python Interactive Mode Get Output Write expression >>> import sys >>> x = 42 >>> x 42 >>> import sys >>> def my_display(x): ... print "out: ", ... print x ... >>> sys.displayhook = my_display >>> x out: 42 >>> >>> print x 42 Behaviour of print() not changed
  • 11. Python Certification Training https://www.edureka.co/python Standard Data Streams Sys - Module >>> import sys >>> for i in (sys.stdin, sys.stdout, sys.stderr): ... print(i) ... ', mode 'w' at 0x7f3397a2c150> ', mode 'w' at 0x7f3397a2c1e0> >>> SHELLstdin stdout stderr >>> import sys >>> print "Going via stdout" Going via stdout >>> sys.stdout.write("Another way to do it!n") Another way to do it! >>> x = raw_input("read value via stdin: ") read value via stdin: 42 >>> print x 42 >>> print "type in value: ", ; sys.stdin.readline()[:-1] type in value: 42 '42' >>>
  • 12. Python Certification Training https://www.edureka.co/python Redirections Sys - Module import sys print("Coming through stdout") # stdout is saved save_stdout = sys.stdout fh = open("test.txt","w") sys.stdout = fh print("This line goes to test.txt") # return to normal: sys.stdout = save_stdout fh.close() SHELLstdin stdout stderr import sys save_stderr = sys.stderr fh = open("errors.txt","w") sys.stderr = fh x = 10 / 0 # return to normal: sys.stderr = save_stderr fh.close()
  • 13. Python Certification Training https://www.edureka.co/python Variables & Constants In Sys Module Sys - Module byteorder executable maxint modules path platform Variables Constants
  • 14. Python Certification Training https://www.edureka.co/python Python And Shell Advanced Python Tutorial
  • 15. Python Certification Training https://www.edureka.co/python Python And Shell A piece of software that provides an interface for a user to some other software or the operating system What is “Shell”? SHELL GUICUI Bourne-Shell C-Shell Bash Shell
  • 16. Python Certification Training https://www.edureka.co/python System Programming The activity of programming system components or system software What is “SP”? “System focussed programming” Modules sys os Simple and clear Well structured Highly flexible Advantages
  • 17. Python Certification Training https://www.edureka.co/python The ‘os’ Module The os module allows platform independent programming by providing abstract methods What is “os” module? Executing Shell scripts with os.system() import os def getch(): os.system("bash -c "read -n 1"") getch() from msvcrt import getch
  • 18. Python Certification Training https://www.edureka.co/python The ‘os’ Module Executing Shell scripts with os.system() import os, platform if platform.system() == "Windows": import msvcrt def getch(): if platform.system() == "Linux": os.system("bash -c "read -n 1"") else: msvcrt.getch() print("Type a key!") getch() print("Okay") Subprocess Module os.system os.spawn* os.popen*
  • 19. Python Certification Training https://www.edureka.co/python Forking In Python Advanced Python Tutorial
  • 20. Python Certification Training https://www.edureka.co/python Fork In Python What is Forking? import os def child(): print('nA new child ', os.getpid()) os._exit(0) def parent(): while True: newpid = os.fork() if newpid == 0: child() else: pids = (os.getpid(), newpid) print("parent: %d, child: %dn" % pids) reply = input("q for quit / c for new fork") if reply == 'c': continue else: break parent() Process Copy of Process Parent Child Starting independent processes with fork() exec*() function
  • 21. Python Certification Training https://www.edureka.co/python The exec*() Functions exec*() functions • os.execl(path, arg0, arg1, ...) • os.execle(path, arg0, arg1, ..., env) • os.execlp(file, arg0, arg1, ...) • os.execlpe(file, arg0, arg1, ..., env) • os.execv(path, args) • os.execve(path, args, env) • os.execvp(file, args) • os.execvpe(file, args, env)
  • 22. Python Certification Training https://www.edureka.co/python Threads In Python Advanced Python Tutorial
  • 23. Python Certification Training https://www.edureka.co/python What Is A Thread? The smallest unit that can be scheduled in an operating systemWhat is a thread? How to create a thread? By forking a computer program in two or more parallel tasks Types of threads Kernel threads User threads Implemented in kernel NOT implemented in kernel
  • 24. Python Certification Training https://www.edureka.co/python What Is A Thread? Global Variables Process Local variables Code THREAD Local variables Code THREAD Local variables Code THREAD
  • 25. Python Certification Training https://www.edureka.co/python The Thread Module from thread import start_new_thread def heron(a): """Calculates the square root of a""" eps = 0.0000001 old = 1 new = 1 while True: old,new = new, (new + a/new) / 2.0 print old, new if abs(new - old) < eps: break return new start_new_thread(heron,(99,)) start_new_thread(heron,(999,)) start_new_thread(heron,(1733,)) c = raw_input("Type something to quit.") Example
  • 26. Python Certification Training https://www.edureka.co/python The Thread Module from thread import start_new_thread num_threads = 0 def heron(a): global num_threads num_threads += 1 # code has been left out, see above num_threads -= 1 return new start_new_thread(heron,(99,)) start_new_thread(heron,(999,)) start_new_thread(heron,(1733,)) start_new_thread(heron,(17334,)) while num_threads > 0: pass Previous example with counters for the threads • Reading the value of num_thread • A new int instance will be incremented or decremented by 1 • the new value has to be assigned to num_threads
  • 27. Python Certification Training https://www.edureka.co/python The Thread Module from thread import start_new_thread, allocate_lock num_threads = 0 thread_started = False lock = allocate_lock() def heron(a): global num_threads, thread_started lock.acquire() num_threads += 1 thread_started = True lock.release() ... lock.acquire() num_threads -= 1 lock.release() return new start_new_thread(heron,(99,)) start_new_thread(heron,(999,)) start_new_thread(heron,(1733,)) while not thread_started: pass while num_threads > 0: pass The solution
  • 28. Python Certification Training https://www.edureka.co/python Threading Module import time from threading import Thread def sleeper(i): print "thread %d sleeps for 5 seconds" % i time.sleep(5) print "thread %d woke up" % i for i in range(10): t = Thread(target=sleeper, args=(i,)) t.start() Example The Thread of the example doesn't do a lot, essentially it just sleeps for 5 seconds and then prints out a message:
  • 29. Python Certification Training https://www.edureka.co/python Threading Module thread 0 sleeps for 5 seconds thread 1 sleeps for 5 seconds thread 2 sleeps for 5 seconds thread 3 sleeps for 5 seconds thread 4 sleeps for 5 seconds thread 5 sleeps for 5 seconds thread 6 sleeps for 5 seconds thread 7 sleeps for 5 seconds thread 8 sleeps for 5 seconds thread 9 sleeps for 5 seconds thread 1 woke up thread 0 woke up thread 3 woke up thread 2 woke up thread 5 woke up thread 9 woke up thread 8 woke up thread 7 woke up thread 6 woke up thread 4 woke up
  • 30. Python Certification Training https://www.edureka.co/python Pipes In Python Advanced Python Tutorial
  • 31. Python Certification Training https://www.edureka.co/python What Are Pipes Introducing modularity in the program to serve one instance output as the input to another instance and so on till solution is achieved What are pipes? Two kinds of pipes Named PipesAnonymous Pipes Exist solely within processes and are usually used in combination with forks P1 P2 P3 P4
  • 32. Python Certification Training https://www.edureka.co/python Pipes – A Classic Example 99 Bottles of Beer Drinking is injurious to health Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer. Take one down, pass it around, Ninety-eight bottles of beer on the wall. Lyrics of the song:
  • 33. Python Certification Training https://www.edureka.co/python Named Pipes Called FIFOs – Creating pipes which are implemented as files.What are named pipes? First In – First Out A process reads from and writes to such a pipe as if it were a regular file. Sometimes more than one process write to such a pipe but only one process reads from it.
  • 34. Python Certification Training https://www.edureka.co/python Conclusion Advanced Python Tutorial
  • 35. Python Certification Training https://www.edureka.co/python Conclusion Advanced Python, yay!