1 / 37

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

( ** Python Certification Training: https://www.edureka.co/python ** ) <br>This Edureka PPT on Advanced Python tutorial covers all the important aspects of using Python for advanced use-cases and purposes. It establishes all of the concepts like system programming , shell programming, pipes and forking to show how wide of a spectrum Python offers to the developers. <br><br><br>Python Tutorial Playlist: https://goo.gl/WsBpKe <br>Blog Series: http://bit.ly/2sqmP4s <br><br>Follow us to never miss an update in the future. <br><br>Instagram: https://www.instagram.com/edureka_learning/ <br>Facebook: https://www.facebook.com/edurekaIN/ <br>Twitter: https://twitter.com/edurekain <br>LinkedIn: https://www.linkedin.com/company/edureka

EdurekaIN
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Python Tutorial Agenda Python Certification Training https://www.edureka.co/python

  2. Advanced Python Tutorial Agenda Python Certification Training https://www.edureka.co/python

  3. Agenda 01 Introduction Introduction to Advanced Python 02 Getting Started Python and Shell 03 Concepts Key Concepts in Python 04 Practical Approach Looking at code to understand theory Python Certification Training https://www.edureka.co/python

  4. Advanced Python Tutorial Introduction to Advanced Python Python Certification Training https://www.edureka.co/python

  5. 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

  6. What Is Advanced Python Python spreading its wings across multiple dimensions and use-cases in many fields What is “Advanced”? Sys Programming 1 5 Computer Science 2 6 Numerical Compute Graph Theory Mathematics 3 7 Databases Python Certification Training https://www.edureka.co/python

  7. Advanced Python Tutorial Introduction To System Programming Python Certification Training https://www.edureka.co/python

  8. System Programming Import sys Sys -Module Provides information about constants, functions and methods of the Python interpreter. Data streams stdin stdout stderr Hello Advanced Python! >>> 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

  9. Command – Line Arguments Command Line Sys -Module argv argc #!/usr/bin/python import sys $ python arguments.py arg1 arg2 ['arguments.py', 'arg1', 'arg2'] Function name: arguments.py 1. argument: arg1 2. argument: arg2 $ # 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 Certification Training https://www.edureka.co/python

  10. Changing Output Behaviour Python Interactive Mode Sys -Module Write expression Get Output >>> 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

  11. Standard Data Streams stdout Sys -Module stdin SHELL 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 >>> import sys >>> for i in (sys.stdin, sys.stdout, sys.stderr): ... print(i) ... ', mode 'w' at 0x7f3397a2c150> ', mode 'w' at 0x7f3397a2c1e0> >>> '42' >>> Python Certification Training https://www.edureka.co/python

  12. Redirections stdout Sys -Module stdin SHELL stderr import sys import sys print("Coming through stdout") save_stderr = sys.stderr fh = open("errors.txt","w") sys.stderr = fh # stdout is saved save_stdout = sys.stdout fh = open("test.txt","w") x = 10 / 0 sys.stdout = fh print("This line goes to test.txt") # return to normal: sys.stderr = save_stderr # return to normal: sys.stdout = save_stdout fh.close() fh.close() Python Certification Training https://www.edureka.co/python

  13. Variables & Constants In Sys Module Sys -Module byteorder executable maxint Variables Constants modules path platform Python Certification Training https://www.edureka.co/python

  14. Advanced Python Tutorial Python And Shell Python Certification Training https://www.edureka.co/python

  15. 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 CUI GUI Bourne-Shell C-Shell Bash Shell Python Certification Training https://www.edureka.co/python

  16. System Programming The activity of programming system components or system software What is “SP”? “System focussed programming” Modules sys os Simple and clear Advantages Well structured Highly flexible Python Certification Training https://www.edureka.co/python

  17. The ‘os’ Module The osmodule 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\"") from msvcrt import getch getch() Python Certification Training https://www.edureka.co/python

  18. The ‘os’ Module Executing Shell scripts with os.system() Subprocess Module 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() os.system os.spawn* os.popen* print("Type a key!") getch() print("Okay") Python Certification Training https://www.edureka.co/python

  19. Advanced Python Tutorial Forking In Python Python Certification Training https://www.edureka.co/python

  20. Fork In Python import os What is Forking? def child(): print('\nA new child ', os.getpid()) os._exit(0) Process Parent def parent(): while True: newpid = os.fork() if newpid == 0: child() else: pids = (os.getpid(), newpid) print("parent: %d, child: %d\n" % pids) reply = input("q for quit / c for new fork") if reply == 'c': continue else: break Copy of Process Child Starting independent processes with fork() exec*() function parent() Python Certification Training https://www.edureka.co/python

  21. 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

  22. Advanced Python Tutorial Threads In Python Python Certification Training https://www.edureka.co/python

  23. What Is A Thread? What is a thread? The smallest unit that can be scheduled in an operating system 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

  24. What Is A Thread? Process Global Variables THREAD THREAD THREAD Local variables Local variables Local variables Code Code Code Python Certification Training https://www.edureka.co/python

  25. The Thread Module Example 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.") Python Certification Training https://www.edureka.co/python

  26. The Thread Module Previous example with counters for the threads 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 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 • • 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 Python Certification Training https://www.edureka.co/python

  27. 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() ... The solution 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 Python Certification Training https://www.edureka.co/python

  28. Threading Module The Thread of the example doesn't do a lot, essentially it just sleeps for 5 seconds and then prints out a message: Example 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() Python Certification Training https://www.edureka.co/python

  29. 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

  30. Advanced Python Tutorial Pipes In Python Python Certification Training https://www.edureka.co/python

  31. 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 P1 Anonymous Pipes Named Pipes P4 P2 Exist solely within processes and are usually used in combination with forks P3 Python Certification Training https://www.edureka.co/python

  32. Pipes – A Classic Example Drinking is injurious to health 99 Bottles of Beer 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

  33. Named Pipes What are named pipes? Called FIFOs – Creating pipes which are implemented as files. 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

  34. Advanced Python Tutorial Conclusion Python Certification Training https://www.edureka.co/python

  35. Conclusion Advanced Python, yay! Python Certification Training https://www.edureka.co/python

More Related