1 / 48

Python Tutorial

Python Tutorial. Learning Python 5 th Edition. Why Python. Python is a general-purpose programming language that is often applied in scripting roles . It is commonly defined as an object-oriented scripting language Why we use python Software quality Developer productivity

klamb
Download Presentation

Python Tutorial

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. Python Tutorial Learning Python 5th Edition

  2. Why Python • Python is a general-purpose programming language that is often applied in scripting roles. It is commonly defined as an object-oriented scripting language • Why we use python • Software quality • Developer productivity • Program portability • Support libraries • Component integration • Enjoyment

  3. Who Uses Python Today? • Google, YouTube ,Dropbox storage service, The Raspberry Pisingle-board computer, • EVE Online, a massively multiplayer online game (MMOG) by CCP Games, • The widespread Bit Torrent peer-to-peer file sharing system, • Industrial Light & Magic, Pixar, and others use Python in the production of animated movies. • ESRI uses as customization tool for its popular GIS mapping products. • Google’s App Engine web development framework • The Iron Port email server product • Maya, a powerful integrated 3D modeling and animation system, • The NSA uses Python for cryptography and intelligence analysis. • iRobot uses Python to develop commercial and military robotic devices

  4. What can I do with python • Systems Programming • GUIs • Internet Scripting • Component Integration • Database Programming • Rapid Prototyping • Numeric and Scientific Programming • And More: Gaming, Images, Data Mining, Robots, Excel...

  5. How Python Runs Programs? • Python Interpreter • An interactive version of python that works in interpreted way • Hello to interpreter • Make a file with name script0.py • Write this code and save it • Run Program as

  6. How you run programs • Interactive way • Starting the python • Or use GUI based environment • Entering multiline statements

  7. First Script • Make a file in text editor (i.e. notepad) with name script1.py • Run it like below • route the printed output of a Python script to a file to save it for later use

  8. Module import and reloads • every file of Python source code whose name ends in a .py extension is a module • You can import the file within python environment as • To use script again and again in running session • Use reload method • A module is mostly just a package of • variable names (known as a Namespace) • the names within that package are called attributes. • An attribute is simply a variable name that is attached to a specific object (like a module)

  9. Module Properties • Make a variable in a file called myfile.py • Make a file threenames.py title = "The Meaning of Life"

  10. Modules and namespaces • modules • are the largest program structure in Python programs, and one of the first key concepts in the language • Name Space • Python programs are composed of multiple module files linked together by import statements, and each module file is a package of variables • each module is a self-contained namespace • there are more ways to run code stored in module files

  11. Variable Name Rules • Syntax: (underscore or letter) + (any number of letters, digits, or underscores) • Case matters: SPAM is not the same as spam • Reserved words are off-limits • Naming conventions • Names that begin with a single underscore (_X) are not imported by a from module import * statement • Names that have two leading and trailing underscores (__X__) are system-defined names that have special meaning to the interpreter • Names that begin with two underscores and do not end with two more (__X) are localized (“mangled”) to enclosing classes • The name that is just a single underscore (_) retains the result of the last expression when you are working interactively

  12. Types and Operations • The Python Conceptual Hierarchy • Programs are composed of modules. • Modules contain statements. • Statements contain expressions. • Expressions create and process objects.

  13. Classes

  14. Python Expressions

  15. Variables and Basic Expressions • Variables are created when they are first assigned values. • Variables are replaced with their values when used in expressions. • Variables must be assigned before they can be used in expressions. • Variables refer to objects and are never declared ahead of time. • comment

  16. Dynamic Typing • Variables, Objects, and References

  17. Reference Checking

  18. String • Python strings are categorized as immutable sequences

  19. String • Implicit concatenation: title = "Meaning " 'of' " Life“ • Concatenation: • Repetition: • Triple Quotes Code Multiline Block Strings

  20. String Indexing and Slicing • X[1:10:2] • will fetch every other item in X from offsets 1–9 • Character code conversions

  21. Changing Strings • Strings are immutable • Method Call Syntax • Attribute fetches • An expression of the form object.attribute means “fetch the value of attribute in object. • Call expressions • An expression of the form function(arguments) means “invoke the code of function, passing zero or more comma-separated argument objects to it, and return function’s result value • Method call expression: • object.method(arguments)

  22. String class methods

  23. String Methods: Examples

  24. String Method Examples: Parsing Text

  25. Other Common String Methods in Action

  26. String Formatting Expressions • Allows us to perform multiple type-specific substitutions on a string in a single step • String formatting expressions: '...%s...' % (values) • String formatting method calls: '...{ }...'.format(values) • Dictionary-Based Formatting Expressions

  27. String Formatting Method Calls

  28. Lists and Dictionaries • Lists • Ordered collections of arbitrary objects • Accessed by offset • Variable-length, heterogeneous, and arbitrarily nestable • Of the category “mutable sequence” • Arrays of object references

  29. Lists

  30. Dictionaries • Accessed by key, not offset position • Unordered collections of arbitrary objects • Variable-length, heterogeneous, and arbitrarily nestable • Of the category “mutable mapping” • Tables of object references (hash tables)

  31. Tuples, Files, and Everything Else • Tuples • Ordered collections of arbitrary objects • Accessed by offset • Of the category “immutable sequence” • Fixed-length, heterogeneous, and arbitrarily nestable • Arrays of object references

  32. Tuples in Action • Concatenation • Repetition • Indexing/Slicing

  33. Files

  34. Files in Action

  35. Storing Python Objects in Files: Conversions

  36. Statements and Syntax

  37. Assignment Statements

  38. Expression Statements

  39. Decision Statements

  40. Switch statement Block Delimiters: Indentation Rules Statement Delimiters: Lines and Continuations Statements may span multiple lines if you’re continuing an open syntactic pair Statements may span multiple lines if they end in a backslash

  41. Loops

  42. Handling Errors with try Statements

  43. Functions and Generators • def is executable code • def creates an object and assigns it to a name • lambda creates an object but returns it as a result • return sends a result object back to the caller • yield sends a result object back to the caller, but remembers where it left off • global declares module-level variables that are to be assigned • nonlocal declares enclosing function variables that are to be assigned • Arguments are passed by assignment (object reference) • Arguments are passed by position, unless you say otherwise • Arguments, return values, and variables are not declared def Executes at Runtime

  44. Functions: Examples Definition Arbitrary scope nesting Calls

  45. Arguments • Arguments are passed by automatically assigning objects to local variable names • Assigning to argument names inside a function does not affect the caller • Changing a mutable object argument in a function may impact the caller • Immutable arguments are effectively passed “by value.” • Mutable arguments are effectively passed “by pointer.”

  46. Keyword and Default Examples

  47. Arbitrary Arguments Examples • The last two matching extensions, * and ** (for keyword arguments only) , are designed to support functions that take any number of arguments.

More Related