Python Basics
Python Basics
of Contents
Introduction
1.1
Basics
1.2
1.2.1
1.3
Numbers
1.3.1
Strings
1.3.2
Tuples
1.3.3
Lists
1.3.4
Dictionaries
1.3.5
Type Conversions
1.3.6
Indexing
1.3.7
1.4
1.4.1
String formatting
1.4.2
1.4.3
1.5
1.5.1
1.5.2
1.5.3
Control Flow
1.6
1.6.1
1.6.2
1.6.3
1.7
Sorting
1.7.1
[Collection Types]
1.7.2
Functions
1.8
Functions
1.8.1
Decorators
1.8.2
Generators
1.8.3
Short functions
1.8.4
Builtin functions
1.8.5
1.9
1.9.1
Handling Exceptions
1.9.2
Introspection
1.9.3
Leftovers
1.10
Introduction
Acknowledgements
This e-book contains many direct and indirect contributions by Allegra Via, Kaja Milanowska,
Anna Philips, Magdalena Rother and Tomasz Puton.
License
Distributed under the conditions of the Creative Commons Attribution Share-alike License
4.0
Sources of this document can be found on https://www.gitbook.com/book/krother/python-3reference.
Contributions welcome!
Basics
Basics
Python programs
A Python program is simply a text file that contains Python statements. The Python
interpreter reads the file and executes the statements line by line.
All program files should have the extension .py
Only one command per line is allowed.
Variables
Variables are 'named containers' used to store values within Python. Variable names may
be composed of letters, underscores and, after the first position, also digits. Lowercase
letters are common, uppercase letters are usually used for constants like PI .
Variables can be used for calculating in place of the values they contain.
Variable assignments
The operator = is used in Python to define a variable. A Python statement containing an
= is called a variable assignment. The value on the right side of the equal sign will be
Changing variables
You may assign to the same variable name twice:
Basics
In this case, the first value is overwritten by the second assignment. There is no way to
obtain it afterwards.
Python Statements
The lines you are writing all the time are also called Python Statements. A Statement is the
smallest executable piece of code.
So far, you have seen at least three different kinds of statements:
Calculate a number
Put a number into a variable
Print the number from a variable on the screen
Reserved words
Some words like import , for and in are called reserved words. They have a special
meaning in Python, which means that you cannont call a variable for or in . There are 33
reserved words in Python 3.
You can see the complete list with the commands:
import keyword
keyword.kwlist
Basics
Comments
Comments are lines that are not executed. They allow you to document your code to make it
easier to read. Also, you can temporarily disable lines of code. There are different ways to
write comments:
# this is a one-line comment
"""This is also a one-line comment"""
"""
With triple quotes comments can
stretch over multiple lines.
"""
'''triple single quotes work like triple double quotes.'''
Magic Methods
The IPython shell has a number of special commands or Magic Methods. Here are the
most common ones:
%run - execute a program
%paste - paste and execute code from the clipboard
%hist - show recently entered commands
ls - show files in the current directory
pwd - print the current working directory
cd <dir> - change the working directory to <dir>
Data types
Numbers
Numbers
Integer numbers
Numerical values without decimal places are called integers or ints. In Python, integers are
a predefined data type.
>>> a = 42
Floating-point numbers
Numbers with decimal places are called floating-point numbers or floats.
>>> b = 42.0
>>> pi = 3.14159
Arithmetical Operators
The arithmetical symbols like + - * / connecting two numbers are called operators. In
addition to the standard arithmetics a few other operators are available:
a = 7
b = 4
c = a - b
d = a * b
e = a / b
f = a % b # modulo, 3
g = a ** 2 # 49
h = 7.0 // b # floor division, 1.0
If you perform arithmetics with integer numbers, the result is also an integer. If one of the
numbers is a float, the result will also be a float. When you perform a division, the result is
always a floating-point number.
10
Strings
String
Text values are called strings. In Python, strings are defined by single quotes, double
quotes or three quotes of either. The following string assignments are equivalent:
first = 'Emily'
first = "Emily"
first = '''Emily'''
first = """Emily"""
Special characters
Some characters in Python require special attention:
Newline characters - writing \n into a string stands for the beginning of a new line.
Tabulators - writing \t into a string stands for a tabulator.
Backslashes - because the backslash ( \ ) has a special meaning in the above, you
need to write a double backslash ( \\ ) to represent a single backslash.
Language-specific characters - Python 3 stores Unicode characters including
German Umlauts, Chinese and Arab alphabets by default. However, they may be
interpreted in the same way in different environments. Just be a bit careful when using
them.
String concatenation
The operator + also works for strings, only that it concatenates the strings. It does not
matter whether you write the strings as variables or as explicit values.
With first = 'Emily' and last = 'Smith' the following three statements have the same
result:
name = first + last
name = first + "Smith"
name = "Emily" + "Smith"
11
Strings
Using square brackets, any character of a string can be accessed. This is called indexing.
The first character has the index [0] , the second [1] and the fourth has the index [3] .
name[0]
name[3]
With negative numbers, you can access single characters from the end, the index [-1]
being the last, [-2] the second last character and so on:
name[-1]
name[-2]
Note that none of these modify the contents of the string variable.
Creating substrings
Substrings can be formed by applying square brackets with two numbers inside separated
by a colon (slices). The second number is not included in the substring itself.
name = 'Emily Smith'
name[0:5]
name[1:4]
name[6:11]
name[:3]
name[-4:]
String methods
Every string in Python brings a list of functions to work with it. As the functions are contained
within the string they are also called methods. They are used by adding the . to the string
variable followed by the method name.
Below you find a few of the available methods:
Changing case
name = 'Manipulating Strings \n'
name.upper()
name.lower()
12
Strings
The method returns the start index of the match. The result -1 means that no match has
been found.
Replacing substrings
name.replace('Strings','text')
13
Tuples
Tuples
A tuple is a sequence of elements that cannot be modified. They are useful to group
elements of different type.
person = ('Emily', 'Smith', 23)
Indexing tuples
Elements of tuples can be indexed in the same way as lists:
person[0] person[-2] person[1:]
14
Tuples
15
Lists
Lists
A list is a Python data type representing a sequence of elements. You can have lists of
strings:
names = ['Hannah', 'Emily', 'Madiosn', 'Ashley', 'Sarah']
Copying a list
You can use slicing to create a copy:
16
Lists
girls = names[:]
Sorting a list
names.sort()
Counting elements
names = ['Hannah', 'Emily', 'Sarah', 'Emily', 'Maria']
names.count('Emily')
17
Lists
18
Dictionaries
Dictionaries
Dictionaries are an unordered, associative array. They have a set of key/value pairs. They
are very versatile data structures, but more difficult to use than lists if you are new to Python.
As the name implies, dictionaries are good for looking up things, or searching in general.
Creating dictionaries
Python dictionaries are defined with winged brackets. On the left side of each entry is the
key, on the right side the value:
ratios = {
'Alice': 0.75,
'Bob': 0.55,
'Charlie': 0.80
}
Dictionaries
persons = {}
Note that you can use in for the same with a list as well. The dictionary is much faster!
However, there is no stable order unless you sort the keys explicitly:
20
Dictionaries
21
Type Conversions
Type conversions
Converting numbers to strings
If you have an integer or float number i , you can make a string out of it with str(i) :
text = str(2000)
22
Type Conversions
int('5.5')
float(5)
str(5.5)
list("ABC")
tuple([1, 2, 3])
dict([('A', 1), ('B', 2)])
set([1, 2, 2, 3])
23
Indexing
Indexing
Computers and people count differently.
Computers treat an address in memory as the starting point of a body of data. In the same
sense, an index in Python always refers to such a starting point, something that is in
between two objects in memory. We humans in contrast always count the objects
themselves.
This is why the indices used to slice list are a bit intuitive at first, e.g. in:
>>> s = "my fat cat"
>>> s[3:6]
'fat'
The above diagram provides a pracitcal model by which you can deduce indices yourself.
24
25
String formatting
String formatting
Variables and strings can be combined, using formatting characters. This works also within a
print statement. In both cases, the number of values and formatting characters must be
equal. Here are some examples:
print('Hello {}%!'.format('Roger'))
print('Result: {4i}'.format(42))
print('{1:5s} {1:>5s}'.format('one', 'two'))
print('{2} {1}'.format('one', 'two'))
a = 5.099999
b = 2.333333
print('{:6.3f}/{:6.3f}'.format(a, b))
The winged brackets are placeholders for the parameters in the format function. They may
contain special instructions for formatting, consisting of two parts {a:b} . Part a . Both
parts are optional. Options include:
{:xd} an integer with x digits.
{:<xd} a left-formatted integer with x digits.
{x} the x-th parameter.
{:>5s} a right-aligned string of width 5.
{:6.2f} a float number with 6 digits (2 after the dot).
26
Although the input command is rarely seen in big programs, it often helps to write small,
understandable programs, especially while learning Python.
27
28
This pattern goes through a file line by line (the for line). It then chops off the newline
character from each line ( strip ) and finally breaks the line into a list of items ( split ).
produces:
print(s)
"this is text"
29
A relative directory name starts from the current working directory, often the directory in
which your program is started:
data/my_file.txt
or go one directory level up, then move into the folder below:
../data/my_file.txt
On Windows, getting directory names right is a bit cumbersome, because the directory
names easily become long easily. Note that you can use forward slashed to separate
between directories. If you use the backslash \ , you need to write a double backslash \\
(because \ is also used for escape sequences like \n and \t ).
f = open('..\\my_file.txt')
f = open('C:\\Python\\my_file.txt')
Closing files
Closing files in Python is not mandatory but good practice. If you open too many files at the
same time this can be a problem.
f.close()
30
f = open('my_file.txt','w')
f.write(text)
f.close()
Like with reading, the csv and pandas offer more sophisticated ways to write tables.
Appending to a file
31
32
os is the name of a module that is automatically installed with Python. It is simply not kept
gives you a list of all files in the directory my_folder and stores it in the variable y .
Changing directories
With the os module, you can change the current directory:
import os
os.chdir(''..\\python'')
33
import time
t = os.path.getmtime('/home/krother/.bashrc')
gmt = time.gmtime(t)
time.strftime("%Y-%m-%d, %H:%M:%S", gmt)
Overview
The table lists some frequently used functions in os :
function
description
os.listdir(path)
os.remove(path)
removes a file
os.getcwd()
os.path.exists(path)
os.path.isdir(path)
os.path.isfile(path)
os.path.getsize(path)
os.path.getmtime(path)
os.path.split(path)
os.environ[key]
os.system(cmd)
34
There must be an if block, zero or more elif 's and an optional else block:
if name == 'Emily':
studies = 'Physics'
elif name == 'Maria':
studies = 'Computer Science'
elif name == 'Sarah':
studies = 'Archaeology'
else:
studies = '-- not registered yet --'
Code blocks
After an if statement, all indented commands are treated as a code block, and are
executed in the context of the condition.
The next unindented command is executed in any case.
Comparison operators
An if expression may contain comparison operators like:
a == b
a != b
a < b
a > b
a <= b
a >= b
35
a in b
36
or using an interval:
for i in range(10, 17):
print(i)
or backwards:
for i in range(17, 10, -1):
print(i)
37
Indented block
All indented commands after the colon are executed within a for loop. The first unindented
command is executed after the loop finishes.
for i in range(5):
print('inside')
print('also inside')
print('outside')
38
39
number = 0
while input('press [Enter] to continue or [x] to exit') != 'x':
number = number +1
print(number)
Endless loops
With while it is possible to build loops that never stop. Most of the time this happens by
accident. In the following loop, the instruction to decrease a is missing. It runs endlessly:
a = 10
b = 1
while a > 0:
b = 1 - b
print(b)
# a = a - 1
40
Sorting
Sorting
Sorting a list
Let us consider sorting the following data:
cities = [
['Berlin', 1977],
['Helsinki', 1983],
['Warsaw', 2006],
['Poznan', 2008],
['Berlin', 2011]
]
There are two ways to sort a Python list. First, you can sort the values in-place, that means
the list gets changed in the process:
cities.sort()
Second, the sorted() function returns an iterator without modifying the initial list:
for city, year in sorted(cities):
print(city, year)
41
Sorting
When we give a tuple of indices, we can sort first by one, then the other column:
cities.sort(key=itemgetter((1, 0)))
42
Functions
Functions
What is a function?
A function is an autonomous sub-program with local variables, input and output. Functions
help you divide a program into smaller logical portions. It is much easier to write, reuse and
debug a program consisting of many small functions than a single huge blob of code.
In Python, a function definition must be followed by a code block:
def calc_price(fruit, n):
'''Returns the price of fruits.'''
if fruit == 'banana':
return 0.75 * n
print(calc_price('banana', 10))
Warning
Do not use mutable default values (e.g. lists and dictionaries) in the function header. They
make a mess!
43
Functions
For even greater flexibility with function parameters you can also add a list *args for an
unspecified number of extra parameters, or a dictionary **kwargs for keyword parameters.
def func(*args, **kwargs):
print(args[1])
print(kwargs['a'])
func(1, 2, a=3, b=4)
This example may be a bit hard to digest, but these kinds of parameters are used widely.
Return values
A function may return values to the program part that called it.
return 0.75
In any case, the return statement ends the execution of a function. Nothing speaks against
having multiple return statements in a function (although for clarity it should not be too
many).
44
Decorators
Decorators
In general, decorators are functions that manipulate functions. More specifically, a
decorator wraps a function to manage a function call.
Imagine you have the following Python function:
def addition(a, b):
do_something_before()
result = a + b
do_something_after()
return result
You can argue that this does not simplify the code in the first place. Using decorators pays
off in bigger programs, when they are used often, or imported from different modules.
45
Decorators
The lru_cache decorator caches results, so that the second call with the same parameters
is faster. It is useful when your function is fully deterministic.
@functools.lru_cache(maxsize=128, typed=False)
46
Generators
Generators
Generators are "lazy functions". They produce results like normal Python functions, but only
when they are needed. The main purpose of using generators is to save memory and
calculation time when processing big datasets.
A Python generator is indicated by the yield keyword. You cannot have yield and
return in the same function. An example:
def count():
i = 0
print('checkpoint A')
while i < 20:
yield i
i += 1
gen = count()
print('checkpoint B')
print(next(gen))
print(next(gen))
The first call of the generator does nothing yet. Only when next() requests the next value,
the generator function is executed until the yield statement. Then it pauses until the next
yield and so on.
Iterators
The thing returned by a generator is called an iterator. Many functions in Python 3 return
iterators (e.g. range() , enumerate() , zip() ).
Among the things you can do to iterators are:
request values with next .
use them in a for loop.
convert them to lists with list() .
47
Generators
48
Builtin functions
Builtin functions
In Python, there is a basic set of about 30 functions called builtin functions. Many of them
are shortcuts that make your everyday programming a lot easier. Here, the most common
ones are summarized, so that you can check your vocabulary.
Type Conversions
The type conversion functions convert one data type into another. Examples for them are
in an earlier section:
int(x) str(x) dict(x) bool(x)
float(x) list(x) tuple(x) set(x)
Mathematical functions
With abs() you can calculate the absolute value of a number:
>>> abs(-42)
42
The divmod() function calculates a division and a modulo at the same time:
>>> divmod(23, 5)
(4, 3)
49
Builtin functions
Summing up numbers
The sum of a list of integer or float numbers can be calculated by the sum() function.
>>> data = [1, 2, 3, 4]
>>> sum(data)
10
50
Builtin functions
Note that because range() returns an iterator (a kind of lazy on-demand list), you need to
convert it to a list to see the data.
Note that enumerate() produces an iterator. To obtain a list, you need to convert it.
enumerate() is a great shortcut to loops with counter variables:
i = 0
for elem in data:
print(i, elem)
i += 1
becomes simply:
51
Builtin functions
Sorting data
The sorted() function sorts a list or the keys of a dictionary, but does not change the
original data.
>>> sorted(data)
[1, 3, 5, 7]
Reversing data
The reversed() function reverses the order of list elements, but does not change the
original data. It returns an iterator.
>>> data = [3, 5, 1, 7]
>>> list(reversed(data))
[7, 1, 5, 3]
52
Then you can write (e.g. in a second Python file in the same directory):
import names
print(names.FIRST_NAMES)
Packages
For big programs, it is useful to divide up the code among several directories. A directory
from which you can import Python modules is called a package. To create a package that
Python will recognize you need to create a file __init__.py (it may be empty).
For instance, you could have the following files in a package namedata :
namedata/
__init__.py
names.py
53
It is strongly recommended to list the imported variables and functions explicitly and not
write
from names import *
54
Handling Exceptions
Handling Exceptions
The try.. except clause catches errors and allows you to react on them.
# print all cards with even numbers.
cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
for card in cards:
try:
number = int(card)
if number % 2 == 0: # modulo operator
print(card, "is an even card.")
except ValueError:
print (card, "can not be divided")
Normally, int("J") would terminate the program. The except clause allows the program
to finish neatly.
In Python, Exceptions should always be handled explicitly. Using a generic except is
considered a very bad habit, because it makes debugging difficult.
55
Handling Exceptions
56
Introspection
Introspection
Introspection is a feature of Python by which you can examine objects (including variables,
functions, classes, modules) inside a running Python environment (a program or shell
session).
57
Introspection
import time
print help(time.asctime)
help() utilizes the triple-quoted comments called docstrings, so that documentation you
Everything is an object
One consequence of the dynamic typing is that Python can treat everything it manages
technically in the same way. Everything is an object is a common phrase describing how
Python works. There is no fundamental difference between a function and an integer. Many
advanced features of Python are built on this concept.
58
Leftovers
Leftovers
Lists versus Dictionaries
Which is better, lists or dictionaries?
Generally, lists are good for sorting while dictionaries are good for searching.
59