Download presentation
Presentation is loading. Please wait.
Published byErika Robinson Modified over 9 years ago
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, complexintfloatlongcomplex List – [] Tuple – () More About TuplesMore About Tuples Dictionary – {} dictdict string set, frozenset Mutable vs. Immutable Indexing (slicing, step slicing) Heterogeneous
9
C++ Python C++Python NULLNone trueTrue falseFalse !not &&and ||or while(true);while(True): pass
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
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) Pythonic Programming
14
Ternary Operator a if b else c b ? a : c Frowned upon in Python just like in C
15
Functions Functions are objects too Can return multiple values using tuple unpacking Can pass in arbitrary number of elements
16
Useful Builtin Functions len() range() and xrange() enumerate() map() zip() any() and all() dir() http://docs.python.org/py3k/library/functions.ht ml http://docs.python.org/py3k/library/functions.ht ml
17
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
18
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/djYOVahttp://bit.ly/djYOVa
19
Classes Everything is public. Python provides a way to ‘hide’ things, but it’s STILL public. self
20
Modules (Importing)
21
Gotchas Other languages have "variables“ Mixing tabs and spaces or inconsistent indentation no i++ notation; use i += 1 http://zephyrfalcon.org/labs/python_pitfalls.h tml http://zephyrfalcon.org/labs/python_pitfalls.h tml
22
File I/O source code / demo Python 3 introduced ‘with’ can be accessed in __future__ in 2.6
23
Exceptions and Classes for another day just know they exist
24
Tips and Tricks Swap Values Chained comparisons a < b < c is the same as a < b and b < c easy_install - setuptools 0.6c11setuptools 0.6c11 – http://pypi.python.org/pypi pdb - The Python Debugger profile
25
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
26
TODO lambda functions dictionaries ‘’.join() zip() list splicing
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.