An expression is a piece of Python code that has a value. Values can be numbers (integer or floating-point), strings, lists, dictionaries, etc. If you type an expression in a Jupyter Notebook, there will be a line that starts with Out[X]
and displays the value of the expression.
2+4
(2+3)*7
A statement does not return a value. It is usually there to get something done. The print
statement has the effect of printing the value of the expression you pass to it. Notice that there is no Out[X]
line here. For Python 2 users, notice that print
now needs parentheses around the expression.
print(2+3)
Strings are one type of values in Python that is very important in CL. You can use lots of operations on strings, including concatenation, duplication, and indexing of substrings at particular positions or ranges.
"hello"
"hello " + "world"
"hello" * 3
"hello"[1]
"hello"[-1]
"hello"[1:-1]
"hello"[1:]
Variables are names for places in your computer's memory. You can assign a value v to a variable; then the name of the variable becomes an expression whose value is v.
In Python, you can assign a new value to a variable even if it already has a value. Subsequent uses of the variable in an expression will yield the new value. The new value may have a different type than the old value (e.g. string vs. integer). You cannot use a variable before you have assigned a value to it.
x = 27
x
x = 28
x
x = "hallo"
x
y
Another important datatype is lists, i.e. sequences of arbitrary length. In Python, the different elements of the list can have different types. You can change the contents of a list by appending to its end or by overwriting a slice of the list with another list. You can iterate over the elements of a list, e.g. with a for
loop.
L = [1,2,"hallo", None]
L
L.append(5)
L
len(L)
L[4]
M = L[:5]
M
M[0] = 27
M[2:5] = ["new element"]
M
L
for element in L:
print(element)
Lists are ordered sequences of values. To find out if some value appears in the list, Python must search for the value from left to right; this can be computationally expensive. By contrast, sets are unordered bags of values. Checking whether a value is in the set is dramatically faster than in a list (constant vs. linear runtime). Because sets are unordered, expressions like s[0]
are meaningless. You can still iterate over the elements of a set, but the order in which they will be iterated is unspecified.
5 in L
long_list = list(range(100000000))
len(long_list)
-1 in long_list
big_set = set(long_list)
len(big_set)
-1 in big_set
x = set([5, 1, 2, "hallo", 3, 2, 4, 5])
x
x[0]
327 in x
for element in x:
print(element)
You can put values into dictionaries under specific keys. Both keys and values can be of arbitrary types (but keys must be hashable, so e.g. you can't use lists as dictionary keys). You can add and overwrite entries in a dictionary.
d = { "foo": 2, "bar": -2}
d
d["alksjdlaksjd"] = 19
d
d["alksjdlaksjd"]
d["foo"]
"bar" in d
Lists, such as [1,2,3]
are mutable: You can add, delete, and replace elements to an existing list object. A closely related datatype is the tuple (1,2,3)
, which behaves mostly like a list, but is immutable, i.e. its contents cannot be modified. Tuples can be useful to make sure you don't accidentally modify the elements. Also, keys in dictionaries must be immutable, so you can use tuples, but not list as keys in dictionaries.
a = (1,2,3)
a[0]
a[0] = 5
d = {}
d[(1,2,3)] = 6
d
d[ [1,2,3] ] = 7
To create a tuple with one element, use the syntax with the trailing comma below; simply enclosing an expression in brackets does not create a tuple, it simply brackets the expression.
(1,)
(1)
You can assign a tuple (or list) of values to a tuple (or list) of variables. This assigns the first value to the first variable, the second to the second, and so on.
(a,b,c) = (1,2,3)
a
b
Define functions to package pieces of code that you want to use repeatedly. Functions can return values. They can return "multiple values" using comma notation; in reality, return a,b
creates a tuple (a,b)
and returns the tuple.
def add(x,y):
return x+y+1
add(3, 5)
def fibonacci(n):
if n <= 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
fibonacci(1)
fibonacci(2)
def f(x):
return x, x+1
one, two = f(7)
one
two
List comprehensions are a succinct, readable, and efficient way of iterating over a list, set, or dictionary; computing a value for each element; and returning a list or dictionary of the resulting values. They are very nifty and powerful.
[x*x for x in [1,2,3,4]]
[fibonacci(x) for x in [0,1,2,3,4,5,6]]
[fibonacci(x) for x in range(7)]