1 / 22

Introduction to Python

Introduction to Python. (for C++ programmers). Background Information. History created in December 1989 by Guido van Rossum Interpreted Dynamically-typed language no int , char, long nonsense Object Oriented Current versions are 2.7.3 and 3.3.0 not backwards compatible. Terms.

totie
Download Presentation

Introduction to 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 to Python (for C++ programmers)

  2. Background Information • History • created in December 1989 by Guido van Rossum • Interpreted • Dynamically-typed language • no int, char, long nonsense • Object Oriented • Current versions are 2.7.3 and 3.3.0 • not backwards compatible

  3. Terms • Pythonic • hard to define • programming in a way that lends itself well to the Python language • Pythonista – a person who codes in a pythonic way

  4. The Zen of Python • Beautiful is better than ugly. • Explicit is better than implicit. • Simple is better than complex. • Complex is better than complicated. • Flat is better than nested. • Sparse is better than dense. • Readability counts. • Special cases aren't special enough to break the rules. • Although practicality beats purity. • Errors should never pass silently. • Unless explicitly silenced. • …

  5. Syntax • Indentation • Colon on the line before indented block • No Braces

  6. Types of Quotes • Double quote – “string” • Single quotes – ‘string’ • Triple quotes –

  7. Let’s Dive In! • Hello World • A more interesting Hello World

  8. Data Types • Numeric Types — int, float, long, complex • List – [] • Tuple – () More About Tuples • Dictionary – {} dict • string • set, frozenset • Mutable vs. Immutable • Indexing (slicing, step slicing) • Heterogeneous

  9. C++ <=> Python

  10. for • iterate through stuff for var in iterable: do_something

  11. in • Check for membership • Iterating through lists (as in the ‘for’ example)

  12. is vs. == • Remember Python is an OOPL? • The ‘is’ keyword checks if two variable names point to the same memory. • == checks the value. Uses the __eq__ function in classes.

  13. Pythonic Programming The idiomatic way to perform an operation on all items in a list in C looks like this: for (i=0; i < mylist_length; i++) { do_something(mylist[i]); } The direct equivalent in Python would be this: i = 0 while i < mylist_length: do_something(mylist[i]) i += 1 That, however, while it works, is not considered Pythonic. It's not an idiom the Python language encourages. We could improve it. A typical idiom in Python to generate all numbers in a list would be to use something like the built-in range() function: for i in range(mylist_length): do_something(mylist[i]) This is however not Pythonic either. Here is the Pythonic way, encouraged by the language itself: for element in mylist: do_something(element)

  14. Functions • Functions are objects too • Can return multiple values using tuple unpacking • Can pass in arbitrary number of elements

  15. Useful Builtin Functions • len() • range() and xrange() • enumerate() • map() • zip() • any() and all() • dir() • http://docs.python.org/py3k/library/functions.html

  16. List Comprehension and Generators • List comprehension makes a list (subscriptable, iterable) • Generators • not a tuple • iterable • calculation gets done on demand (when iterated) • can’t subscript

  17. Python 2 vs. Python 3 • print • range and xrange • raw_input and input • division • 2 does integer division. Use // to do floating • 3 does floating point. Use // to do integer • str and bytes • Unicode • __future__ • Full changes at http://bit.ly/djYOVa

  18. Modules (Importing)

  19. Gotchas • Other languages have "variables“ • Mixing tabs and spaces or inconsistent indentation • no i++ notation; use i += 1 • http://zephyrfalcon.org/labs/python_pitfalls.html

  20. Exceptions and Classes • for another day • just know they exist

  21. Tips and Tricks • Swap Values • Chained comparisons a < b < c is the same as a < b and b < c • easy_install - setuptools 0.6c11 • http://pypi.python.org/pypi • pdb - The Python Debugger • profile

  22. Learning Python • http://python.org/ • Great documentation for every version! • help() • ipython • python shell • tab-completion • ? and ?? • http://bit.ly/2noLNE • http://pythontutor.com/ • http://www.doughellmann.com/PyMOTW/index.html

More Related