Top Banner
Introduction to Python (for C++ programmers)
22

Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Jan 15, 2016

Download

Documents

Erika Robinson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Introduction to Python

(for C++ programmers)

Page 2: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 3: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 4: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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.• …

Page 5: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Syntax

• Indentation• Colon on the line before indented block• No Braces

Page 6: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Types of Quotes

• Double quote – “string”• Single quotes – ‘string’

• Triple quotes –

Page 7: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Let’s Dive In!

• Hello World• A more interesting Hello World

Page 9: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

C++ <=> PythonC++ Python

NULL None

true True

false False

! not

&& and

|| or

while(true); while(True): pass

Page 10: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

for

• iterate through stuff

for var in iterable:do_something

Page 11: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

in

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

Page 12: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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.

Page 13: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 14: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Functions

• Functions are objects too

• Can return multiple values using tuple unpacking• Can pass in arbitrary number of elements

Page 15: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Useful Builtin Functions

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

.html

Page 16: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 17: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 18: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Modules (Importing)

Page 19: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 20: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

Exceptions and Classes

• for another day• just know they exist

Page 21: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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

Page 22: Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.

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