Why Python
Learn Python The Hard Way, 2nd Edition

Data Structure - List (Array), Dictionary (Hash)
Flow Control
Doc String
Coding Style
Functional Programming

How can we determine the version of python?

python -V
/usr/bin/env python -V

Why should we use Python or not?

  1. Python provides generators, both as expressions and from functions. Generators allow for iterative processing of things, one item at a time. This doesn't seem so fancy, until you start to realize that normal iterative processing of a list. A list takes memory. A really big list takes a lot of memory. This becomes particularly handy is when you have a long chain of processes you need to apply to a set of data. Generators allow you to grab source data one item at a time, and pass each through the full processing chain. Using the generator-based migration tool collective.transmogrifier I can read data from the old site, make complex, interdependent updates to the data as it is being processed, and then create and store objects in the new site all in constant memory. For applications where you are dealing with even larger data sets, this sort of tool can be indespensible. David Beazley has a great slide deck online that provides some very compelling examples of using generators for system tasks. Take a look and see what sparks start flying in your imagination!
  2. Python is Fast. Python is an interpreted language, so it is inherently slow, but some amazing work has been done over the past years to improve Python's performance. The PyPy project aims to speed up Python as a whole (and is doing a great job of it). Numba can offer amazing speedups simply by adding decorators to code you already have. If you want to do high-performance computing, Python is a viable option today.
  3. Python is Broad. Python has been around for quite some time now, and is used in nearly every field of endeavour. A reddit thread asked the question how do you use Python at work and the answers show tasks ranging from systems automation, testing, and ETL to gaming, CGI and web development. Disney uses Python to help power their creative process. Mozilla uses Python to explore their extensive code base and releases tons of open source packages built in python. Checking PyPi, the public package index for Python, shows 40 thousand add-ons available, across nearly 300 listed topic categories. Basically, if you want to do something in Python, chances are pretty good someone else already has, and you don't need to start from scratch.
  4. Scientific Computing. Much of Python's popularity is in fields like scientific computing. Advanced systems like NumPy and SciPy were started not by teams of software engineers, but by domain experts building the tools they needed to get a job done.

What are different implementations of Python?

  1. CPython: offers clean integration with code that is written in C, so implementing wrappers around C libraries is relatively simple.
  2. Jython: offers deep integration with Java code
  3. Iron Python: works with C# and .NET code
  4. PyObjc: write python code using ObjectiveC toolkits
  5. pyjs: offers to compile your python to JavaScript

How can we launch the python shell?

Just open a terminal, type 'python' and hit the Enter key.

How can we quit the python shell?


How can we print out a line of text to the standard output with python?

Use the print function:

print "Hello World!"

How can we use UTF-8 characters in our python scripts?

If you are from another country, and you get errors about ASCII encodings, then put this at the top of your Python scripts:

# -*- coding: utf-8 -*-

It will fix them so that you can use Unicode UTF-8 in your scripts without a problem.

If you use \U or \u then you'll need to use a unicode string in u'\U0001F47E'. Put a u in front of the '' (single-quotes) or "" (double-quotes).

How can we comment our a line of code in python?

Put the # character at the beginning of the line.

How can we comment out multiple lines of code in python?

Put the # character at the beginning of each line.

What is the assignment operator in python?

It is the equal sign.

cars = 100

How can we do string interpolation in python?

my_name = 'XYZ'
print "Let's talk about %s." % my_name

What are the format characters in python?

  1. %s: string
  2. %d: digit (number)
  3. %r: raw

How can we round a floating point number?

You can use the round() function like this: round(1.7333).

Can we print multiple items at once in python?


print "Here are the days: ", days

What is the HEREDOC format in python?

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

What is the point of putting a comma at the end of a print line in python?

This tells python not to print a newline.

print "How old are you?",
age = raw_input()

The above code prints out the question without the newline. The second line just read the input from the user.

How can we get input from the user with python?

Use the raw_input function:

age = raw_input()

How can we read in a number from the user?

x = int(raw_input())

What is the difference between input() and raw_input()?

The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it.

What is the purpose of pydoc?

It is a documentation tool for python:

pydoc raw_input

will display the documentation for the raw_input function.

pydoc open
pydoc file
pydoc os
pydoc sys

Can we put raw_input on the same line with the print statement?

No. We cannot put raw_input on the same line with the print statement as:

print "How old are you?" , raw_input()

If you do this, python will error.

How can we access parameters that are passed in via the command line?

from sys import argv
script, first, second, third = argv
python stuff things that
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License