Session II - Python Basics 2
IMPRS Be Smart Summer School
2023-08-07
my_list = | [ “ali”, | “bob”, | “chiara”] |
0 |
1 |
2 |
my_list = | [ “ali”, | “bob”, | “chiara”] |
-3 |
-2 |
-1 |
You can access a subset of a list using the slice operator :
.
Basic usage
end
is not included in the slice.list
append()
function.pop()
function.pop()
function removes the last item in the list.pop(index)
function removes the item at the specified index.remove()
function.remove()
function removes the first item with the specified value.['ali', 'chiara', 'bob']
+
operator.in
operator.03_lists.ipynb
We have two logical values: True
and False
and
and or
and not
are the logical operators
and
means that both conditions must be true
or
means that at least one condition must be true
not
reverses the logical value
if
statementthe variable is smaller than 50
{ }
or end
statements to indicate the span. Instead the hierarchy/ownership of the statements are determined by indents.Four spaces
is the accepted convention but you can use tab
or the another number of spaces as long as it is consistent.else
statementthe variable is smaller than 50
elif
statementthe variable is between 40 and 50
Ex2
last question)Python creates a range object that is iterable with range()
function.
Then one can loop over it to make calculations.
list_name.append()
to add an item to a list\[ \text{Squares} = \{ x^2 | \text{ for all } x \in \text{my_list} \} \]
[1, 4, 9, 16]
Participant with index 0 chose 15 tokens
Participant with index 1 chose 30 tokens
Participant with index 2 chose 45 tokens
03_lists.ipynb
lists
{key1: value1, key2: value2}
, ordict(key1=value1, key2=value2)
{'ali': 1987, 'bob': 1953, 'chiara': 1980}
1953
{'ali': 1987, 'bob': 1953, 'chiara': 1980, 'dana': 1992}
My name is ali. I live in Istanbul
My name is bob. I live in London
My name is chiara. I live in Rome
04_dictionaries.ipynb
print()
prints the value of the argument to the screenabs()
returns the absolute value of the argumentlen()
returns the length of the argumenttype()
returns the type of the argumentmax()
returns the maximum value of the argumentmin()
returns the minimum value of the argumentround()
rounds the argument to the nearest integersum()
returns the sum of all elements in the argumentsorted()
returns a sorted version of the argumentdef
keyword.37
fruitful
functionsvoid
functionsmodifiers
pure
05_functions.ipynb