Overview and Learning Objectives
This unit introduces basic concepts used in Python programming while we assume that you are familiar with general programming and have some experience with other programming languages such as MATLAB, C/C++, or Java. We start with basic Python variables and operators and then introduce compound data types such as lists, tuples, sets, and dictionaries. It is important that you understand the conceptual difference between these constructs and the Python syntax to encode them. Using variables and data types in Python can differ significantly from the other programming languages, often resulting in seemingly surprising programming errors. Therefore, we will briefly discuss how to convert data types in Python. Furthermore, we point out the difference between deep and shallow copies, a delicate and important topic that is a common cause of errors, especially when passing variables to functions. As said, we only touch on these topics by giving concrete examples. To get familiar with the Python syntax, we recommend that you do the small exercises on list (Exercise 1) and dictionary manipulations (Exercise 2). For more comprehensive tutorials, we refer to the following sources:
- The Python Tutorial introduces basic concepts and features of the Python language and system.
- The Scipy Lecture Notes introduce the scientific Python ecosystem including libraries such as NumPy, Scipy, and Matplotlib.
- The FMP Nootbooks are a collection of Python notebooks on Fundamentals of Music Processing (FMP).
Basic Facts¶
- Python is an interpreted (not compiled), open-source, multi-platform programming language.
- There exist several modules for scientific computing (e.g.
numpy
,scipy
,matplotlib
,librosa
) in Python. - Python uses indentation (and not brackets or
end
-commands) to separate blocks of code. - Comment lines start with the character
#
. - Useful functions for help:
- Invoke the built-in help system:
help()
. - List objects in namespace:
dir()
. - Show global and local variables:
globals()
,locals()
.
Variables and Basic Operators¶
Let us start with some basic facts on Python variables:
- Variables do not need to be declared; neither their type.
- Variables are created automatically when they are first assigned.
- A variable name may contain letters (
a
,b
, ...,Y
,Z
) and the underscore (_
). - Variable names are case sensitive.
- All but the first character can also be positive integer numbers.
- Usually, one uses lower case letters and underscores to separate words.
A string is given in single ticks ('
) or double ticks ("
). If there is no other reason, we recommend single ticks. The following code assigns a string to a variable and prints it using the print
-command.
string_variable1 = 'Welcome to the Python Tutorial'
print(string_variable1)
This is how you can print some basic math:
n = 3
print('n + 1 =', n + 1)
print('n - 1 =', n - 1)
print('n * 2 =', n * 2)
print('n / 2 =', n / 2)
print('n ^ 2 =', n ** 2)
Division always results in a floating-point number, even if the number is divisible without remainder. (Note that there are differences between Python 2 and Python 3 in using /
). If the result should be an integer (e.g., when using it as an index), one may use the //
operator. The %
yields the remainder.
n = 8
print('Normal division:', n / 2)
print('Integer division:', n // 2)
print('Normal division:', n / 5)
print('Integer division:', n // 5)
print('Remainder of integer division:', n % 5)
For re-assigning a variable, one may use the following conventions:
n = 7
n += 11
print('Addition:', n)
n *= 2
print('Multiplication:', n)
n /= 18
print('Division:', n)
n **= 0.5
print('Exponentiation:', n)
If you want more control over the formatting of your printed output, we recommend using the f-String format convention.
print(f'This is how to print a number: {17}.')
string_variable2 = 'ABCD'
print(f'This is how to print a string: {string_variable2}.')
print(f'This is how to print a number (enforcing six digits): {1234:6d}.')
print(f'This is how to print a floating point number with three decimals: {3.14159265359:6.3f}.')
Lists and Tuples¶
The basic compound data types in Python are lists and tuples. A list is enclosed in square brackets and a tuple is enclosed in round brackets. Both are indexed with square brackets (with indexing starting with $0$). The len
function gives the length of a tuple or a list.
var_lis = ['I', 'am', 'a', 'list']
var_tup = ('I', 'am', 'a', 'tuple')
print(var_lis)
print(var_tup)
print(var_lis[0], var_tup[1], 'generated from',
var_tup[2], var_tup[3], 'and', var_lis[2], var_lis[3])
print(len(var_tup))
print(len(var_lis))
print(type(var_lis))
print(type(var_tup))
What is the difference between a list and a tuple? Tuples are immutable objects (i.e., their state cannot be modified after they are created) and a bit more efficient. Lists are more flexible. Here are some examples for list operations:
var_list = [1, 2, 3]
print('Print list:', var_list)
var_list[0] = -1
print('Alternate item:', var_list)
var_list.append(10)
print('Append item:', var_list)
var_list = var_list + ['a', '12', [13, 14]]
print('Concatenate two lists:', var_list)
print('Last element of list: ', var_list[-1])
print('Remove an item by index and get its value:', var_list.pop(2))
var_list.remove([13, 14])
print('Remove an item by value:', var_list)
del(var_list[2:4])
print('Remove items by slice of indices:', var_list)
One can index a list with start, stop, and step values ([start:end:step]
). Note that, in Python, the last index value is end-1
. Negative indices are possible with -1
referring to the last index. When not specified, start
refers to the first item, end
to the last item, and step
is set to $1$.
var_list = [11, 12, 13, 14, 15]
print('var_list =', var_list)
print('var_list[0:3] =', var_list[0:3])
print('var_list[1:3] =', var_list[1:3])
print('var_list[-1] =', var_list[-1])
print('var_list[0:4:2] =', var_list[0:4:2])
print('var_list[0::2] =', var_list[0::2])
print('var_list[::-1] =', var_list[::-1])
The following examples shows how the elements of a list or tuple can be assigned to variables (called unpacking):
var_list = [1, 2]
[a, b] = var_list
print(a, b)
var_tup = (3, 4)
[c, d] = var_tup
print(c, d)
Leaving out brackets, tuples are generated.
t = 1, 2
a, b = t
print(t)
print(a, b)
The range
-function can be used to specify a tuple or list of integers (without actually generating these numbers). A range can then be converted into a tuple or list.
print(range(9))
print(range(1, 9, 2))
print(list(range(9)))
print(tuple(range(1, 9, 2)))
print(list(range(9, 1, -1)))
Boolean Values¶
Boolean values in Python are True
and False
. Here are some examples for basic comparisons:
a = 1
b = 2
print(a < b)
print(a <= b)
print(a == b)
print(a != b)
The bool
function converts an arbitrary value into a boolean value. Here, are some examples:
print(bool('a'))
print(bool(''))
print(bool(1))
print(bool(0))
print(bool(0.0))
print(bool([]))
print(bool([4, 'hello', 1]))
Sets¶
There are also other data types in Python, which we want to mention here. In the following, we introduce sets, which are unordered collections of unique elements. Furthermore, we apply some basic set operations.
s = {4, 2, 1, 2, 5, 2}
print('Print the set s:', s)
print('Union of sets:', {1, 2, 3} | {2, 3, 4})
print('Intersection of sets:', {1, 2, 3} & {2, 3, 4})
s.add(7)
print('Adding an element:', s)
s.remove(2)
print('Removing an element:', s)
Dictionaries¶
Another convenient data type are dictionaries, which are indexed by keys (rather than by a range of numbers as is the case for lists or arrays). The following code cell gives an example and introduces some basic operations.
dic = {'a': 1 , 'b': 2, 3: 'hello'}
print('Print the dictionary dic:', dic)
print('Print the keys of dic:', list(dic.keys()))
print('Access the dictionary via a key:', dic['b'])
print('Print the values of the dictionary:', list(dic.values()))
Python Type Conversion¶
Python offers many different numerical types and methods for type conversion. The standard numeric types in Python are int
, float
, and complex
. The function type()
can be used to identify the type of a variable. One can use the methods int()
, float()
, and complex()
to convert from one type to another. This is demonstrated in the following code cell.
- Type conversions from
float
toint
may result in some rounding. - One cannot directly convert from the type
complex
toint
orfloat
.
a = 1
print('a =', a, type(a))
b = float(a)
print('b =', b, type(b))
c = 2.2
print('c =' , c, type(c))
d = int(c)
print('d =', d, type(d))
e = complex(d)
print('e =', e, type(e))
f = complex(a, c)
print('f =', f, type(f))
Shallow and Deep Copy Operations¶
Dealing with objects such as lists, tuples, dictionaries, or sets can be trickier as one may think. In particular, using the assignment operator =
may only create a pointer from variable to an object. As a result, two variables may point to the same object, which may lead to unexpected modifications. This effect is illustrated by the following example.
a = [1, 2, 3]
print('a = ', a, ', id(a) = ', id(a))
b = a
b[0] = 0
b.append(4)
print('b = ', b, ', id(b) = ', id(b))
print('a = ', a, ', id(a) = ', id(a))
This example shows that the assignment b = a
does not create a copy of the list. The variables a
and b
point to the same object, whose identifier (unique integer) is revealed by the function id()
. To create copies of an object, one can use the python module copy
, which is imported by the statement import copy
. There are different types of copies called shallow copy and deep copy, which become important when dealing with compound objects (objects whose elements are again objects such as lists of lists).
- A shallow copy creates a new compound object. However, if the entries are again compound objects, only links are created.
- A deep copy creates a new compound object, where new objects are created for all entries (and recursively for their entries).
The two cases are illustrated by the subsequent example.
import copy
a = [[1, 2, 3], [4, 5, 6]]
b = copy.copy(a)
b[0] = 0
print('a = ', a)
print('b = ', b)
b[1][0] = 0
print('a = ', a)
print('b = ', b)
a = [[1, 2, 3], [4, 5, 6]]
c = copy.deepcopy(a)
c[1][0] = 0
print('a = ', a)
print('c = ', c)
Exercises and Results¶
import libpcp.python
show_result = True
- In the following, we assume that a student is specified by a list containing a student ID (integer), last name (string), and first name (string). Create a list of students, which contains the following entries:
[123, 'Meier', 'Sebastian']
,[456, 'Smith', 'Walter']
. (Note that this becomes a list of lists.) - Add to this list the student
[789, 'Wang', 'Ming']
. - Print out the student list in reversed order (descending student IDs).
- Print out only the first name of the second student in this list.
- Print the length of the list (using the
len
function). - Make a deep copy of the list and then remove the first and second student from the copied list (using the
del
statement). Furthermore, change the student ID of the remaining student from789
to777
. Check if the original list has been modified.
#<solution>
# Your Solution
#</solution>
libpcp.python.exercise_list(show_result=show_result)
- Again, we assume that a student is specified by student ID (integer), last name (string), and first name (string). Create a dictionary, where a key corresponds to the student ID and a value to a list of the last name and first name. Start with a dictionary with key
123
and value['Meier', 'Sebastian']
as well as key456
and value['Smith', 'Walter']
. - Add the student with key
789
and value['Wang', 'Ming']
. - Print out a list of all keys of the dictionary.
- Print out a list of all values of the dictionary.
- Print out the last name of the student with key
456
. - Remove the student with key
456
(using thedel
statement). - Print the length of the dictionary (using the
len
function).
#<solution>
# Your Solution
#</solution>
libpcp.python.exercise_dict(show_result=show_result)