1 / 23

Introduction of Python

Introduction of Python . Based on: Learning Python By Mark Lutz & David Ascher , O'Reilly Introduction to Python By Guido van Rossum Learning Python By Mark Lutz & David Ascher , O'Reilly 10 Reasons Python Rocks for Research By Hoyt Koepke. A Brief History.

marilu
Download Presentation

Introduction of Python

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. Introduction of Python Based on: Learning Python By Mark Lutz & David Ascher, O'Reilly Introduction to Python By Guido van Rossum Learning Python By Mark Lutz & David Ascher, O'Reilly 10 Reasons Python Rocks for Research By Hoyt Koepke

  2. A Brief History • Invented in 1990 by Guido Van Rossum • The name ‘Python’ stems from • "Monty Python's Flying Circus“ • Intended to be a scripting language on Amoeba OS • Python was influenced by ABC and Modula-3 • First public release was in 1991

  3. Python • On the Web: www.python.org

  4. Python Advantages • Object-Oriented • Dynamic Type Checking makes it inherently generic – C++ templates for free! • Free, as in Open Source free • Portable • Powerful language constructs / features • Powerful toolkit / library • Mixable with other languages • Easy to use & learn

  5. Features for writing codes • Great for learning the language • Great for experimenting with the library • Great for testing your own modules • High efficiency • Clarity

  6. Python Principles • Python treats everything as an object • Python is an interpreter • It gives immediate results • It generates byte code (similar to Java)

  7. Running Python • Interactively from console: • C:>python • >>>print2*3 • 6 • As Python module files: • C:>python mypgm.py Interactive prompt No statement delimiter Python modules are text files with .py extensions

  8. Simple examples • Built-in and explicit print • >>>"Hello all" • 'Hello all' • >>>print"A b" • A b • >>>ALongName = 177 / 3 • >>>ALongName • 59 Builtin print gives double quotes as single quotes. " and ' quotes are same. print statement removes quotes

  9. Built-in Object Types

  10. Basic Operations • Assignment creates names • s = 'A string'# s is created • Names can be any length • Names are case sensitive • >>> A = 1; a = 2; A+a • 3 Semicolons separates statements on the same line

  11. String Formatting Adjacent strings are concatenated, like in C • Like C's printf with similar specifiers • "It's " '%d great life!' % 1 • "It's 1 great life!" • '%s %s much' % ("Python's", 2) • "Python's 2 much" • C's backslash conventions used • Raw strings take backslashes literally • print "a\tc" # outputs a c • print R"a\tc" # outputs a\tc

  12. String Operations Range includes lower bound and excludes upper bound Suppress new line on output

  13. Lists • Sequence of mutable heterogeneous objects (items can be changed in-place).

  14. Dictionaries • Mapping of unordered immutable keys to mutable heterogeneous objects.

  15. IF Statement • General form example: • if 'a' <= c <= 'z': • print 'Lower case letter' • elif 'A' <= c <= 'Z': # optional • print 'Upper case letter' • else:# optional • print 'Not a letter'

  16. WHILE Statement • General format: while <test> : # loop conditional <stmt-block1> # loop body else: # optional - run <stmt-block2> # if no break used a = 0; b = 5 while a < b : print a, # outputs 0 1 2 3 4 a = a + 1

  17. BREAK, CONTINUE, PASS • break terminates the innermost executing loop and transfer control after the loop. • continue immediately transfers control to the top of the innermost executing loop. • pass is the no-op statement in Python. • while <test0> :# loop header • <stmts1> # run if test0 true • if <test1> :break# exit, skip else • if <test2> :continue# go to loop header • <stmts2> # not run if test2 true • else: • <stmts3> # run if didn't hit break

  18. FOR Statement • General format: for <target> in <object> : # loop header <stmt-block1> # loop body else: # optional, run else clause <stmt-block2> # if no break used sum = 0 for x in [1, 2, 3, 5] : sum = sum + x sum # outputs 11

  19. Modules • Modules are implemented using files. • Module source files have a .py extension. • Compiled byte code modules have .pyc extension.

  20. Loading Modules • There are 3 ways to load a module:

  21. Import Statement • Using the import statement: # sigma1.py - test module counter = 1 def Sigma(L) : sum = 0 for x in L : sum = sum + x return sum print "Loaded module sigma1" >>> import sigma1 Loaded module sigma1 >>> sigma1.counter 1 >>> sigma1.Sigma([1, 2, 3]) 6 >>> sigma1.counter = 2 >>> import sigma1 >>> sigma1.counter 2

  22. Reasons Python Rocks for Research • Holistic Language Design • Readability • Balance of High Level and Low Level Programming • Language Interoperability • Documentation System • Hierarchical Module System • Data Structures • Portable • Available Libraries • Free

  23. Python VS Matlab • Python is totally free. Matlab is not. • Python is a general-purpose language. Matlab is purely for scientific computing, although it does a great job at it. • Python syntax is beautiful. • Python is inherently object oriented. • Python is fast enough. • Python has great community among programmers, scientists, mathematicians, and engineers, great libraries. • Python packages can do nearly everything Matlab can do for signal processing.

More Related