Why Python
Learn Python The Hard Way, 2nd Edition

Code samples
Machine Learning
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

What is the purpose of PyPI?

Python Package Index (PyPI) is a repository of software for the Python programming language. It is the equivalent of CPAN. PyPI helps you find and install software developed and shared by the Python community. Package authors use PyPI to distribute their software.


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