Download Introduction to Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
• 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
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.
…
Syntax
• Indentation
• Colon on the line before indented block
• No Braces
Types of Quotes
• Double quote – “string”
• Single quotes – ‘string’
• Triple quotes –
Let’s Dive In!
• Hello World
• A more interesting Hello World
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
C++ <=> Python
C++
Python
NULL
None
true
True
false
False
!
not
&&
and
||
or
while(true);
while(True): pass
for
• iterate through stuff
for var in iterable:
do_something
in
• Check for membership
• Iterating through lists (as in the ‘for’ example)
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.
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)
Functions
• Functions are objects too
• Can return multiple values using tuple unpacking
• Can pass in arbitrary number of elements
Useful Builtin Functions
•
•
•
•
•
•
•
•
len()
range() and xrange()
enumerate()
map()
zip()
any() and all()
dir()
http://docs.python.org/py3k/library/functions.ht
ml
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
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
Modules (Importing)
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
Exceptions and Classes
• for another day
• just know they exist
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
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