# Recitation 1: Python basics¶

We discussed some basic types and operations, conversion functions, the function input and several structures that Python has to offer: if-else, while loops, and for loops. We also solved an excercise of counting the number of zeros in a number that is given as an input by the user.

Takeaways:

1. Try to "play" with types, operators, structures, and built-in functions in order to understand the basics of Python.
2. To tackle programming exercises, try to first imagine what you want the solution to do intuitively and then find ways to implement it.
3. Read the instructions for the submission of home assignments and submit your code as a .py file.
4. Test your code before you submit.

## IDLE¶

We saw a demonstration on how to use IDLE (needed for homework coding part). Don't forget to run the test() function that is already given in the skeleton file.

In order to open a .py file in Script mode, rightclick the file and select "Edit with IDLE".

### Working with notebooks¶

Read the following instructions if you are interested in working with Jupyter notebooks.

Code for printing several outputs in one cell (not part of the recitation):

In [4]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"


Types: int, float, string, bool

## Types and Variables¶

In [5]:
x = 3
y = 3.14
name = "michal"
b = True
print(type(x))
print(type(y))
print(type(name))
print(type(b))
type(x)

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

Out[5]:
int

## Operators¶

In [6]:
type("4")

Out[6]:
str
In [7]:
10 / 3

Out[7]:
3.3333333333333335
In [8]:
10 // 3

Out[8]:
3
In [9]:
10 // 2.0

Out[9]:
5.0
In [ ]:


In [10]:
4 + 5.0

Out[10]:
9.0
In [11]:
2 ** 0.5

Out[11]:
1.4142135623730951
In [12]:
9 ** 0.5

Out[12]:
3.0
In [13]:
"4" + "5"

Out[13]:
'45'
In [14]:
"4" * 3

Out[14]:
'444'
In [15]:
"4" * "5"

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-ab0f866df984> in <module>()
----> 1 "4" * "5"

TypeError: can't multiply sequence by non-int of type 'str'
In [16]:
10 % 2

Out[16]:
0
In [17]:
2 % 10

Out[17]:
2
In [18]:
(-2) % 10

Out[18]:
8
In [19]:
3 < 4

Out[19]:
True
In [20]:
3 < 4 and 3 > 5

Out[20]:
False

Task: find the rightmost digit in a given number, using the operators mentioned above:

In [21]:
x = 345
# rightmost digit
x % 10
# second rightmost digit
(x // 10) % 10

Out[21]:
5
Out[21]:
4

Operator precedence:

## Conversions¶

In [22]:
int(3.14)

Out[22]:
3
In [25]:
int("3")

Out[25]:
3
In [26]:
float("3")

Out[26]:
3.0
In [27]:
float("3.14")

Out[27]:
3.14
In [28]:
str(3)

Out[28]:
'3'
In [29]:
str(3.14)

Out[29]:
'3.14'

## If-else structure¶

In [37]:
hungry = True
today = "Thursday"
tv_night =  "Thursday" #"Sunday"
last_episode_seen = 5

if hungry:
print("Better find something to eat...")
if today == "Friday":
print("Go to a restaurant")
else:
print("Cook something")
elif today == tv_night:
print("Watch episode number", last_episode_seen + 1, "of your favorite show")
elif today == "Thursday":
print("Go out for drinks!")
elif today == "Monday" or today == "Wednesday":
print("Workout")
elif today == "Friday" or today == "Tuesday" or today == "Saturday":
print("Go to sleep early :(")
else:
print("That's not a real day")

Better find something to eat...
Cook something


## Another If-elif-else example¶

In [38]:
n = 100
if n % 2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("end of if structure")

hi
bye
end of if structure

In [39]:
n = 101
if n % 2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("end of if structure")

bye
end of if structure

In [40]:
n = 100
if n % 2 == 0:
print("hi")
elif n > 2:
print("bye")
else:
print("something")
print("end of if structure")

hi
end of if structure


## The function input()¶

In [32]:
s = input("Please enter an integer: ")
print(s, type(s))

Please enter an integer: 3000
3000 <class 'str'>

In [33]:
num = int(input("Please enter an integer: "))
print(num, type(num))

Please enter an integer: 3000
3000 <class 'int'>

In [34]:
num = int(input("Please enter an integer: "))
print(num, type(num))

Please enter an integer: michal

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-dedbc0dd1f23> in <module>()
----> 1 num = int(input("Please enter an integer: "))
2 print(num, type(num))

ValueError: invalid literal for int() with base 10: 'michal'
In [ ]:
num = int(input("Please enter an integer: "))
print(num, type(num))


## Introducing loops!¶

### While loop example: print all integers between 0 to 9¶

In [41]:
i = 0
while i < 10:
print(i)
i = i + 1
print("out of loop, value of i is", i)

0
1
2
3
4
5
6
7
8
9
out of loop, value of i is 10


### Counting the number of zeros in a given integer (solution 1)¶

In [43]:
print("This program will tell you how many 0's are in an integer")
orig_num = int(input("Please enter an integer: "))

num = orig_num
cnt = 0

while num > 0:
if num % 10 == 0:
cnt = cnt + 1 #cnt += 1
num = num // 10 #num //= 10

print(orig_num, "has", cnt, "0-s")

This program will tell you how many 0's are in an integer
0 has 0 0-s

In [ ]:
print("This program will tell you how many 0's are in an integer")
orig_num = int(input("Please enter an integer: "))

num = orig_num
cnt = 0

if num == 0:
cnt = 1

while num > 0:
if num%10 == 0:
cnt = cnt + 1 # cnt += 1
num = num // 10 #num //= 10

print (orig_num, "has", cnt , "0-s")


### For loop example:¶

In [44]:
for x in "michal":
print(x)
print(type(x))
print(x * 4)

m
<class 'str'>
mmmm
i
<class 'str'>
iiii
c
<class 'str'>
cccc
h
<class 'str'>
hhhh
a
<class 'str'>
aaaa
l
<class 'str'>
llll


### Counting the number of zeros in a given integer (solution 2)¶

In [45]:
print("This program will tell you how many 0's are in an integer")
num_str = input("Please enter an integer: ")

cnt = 0

for digit in num_str:
if digit == "0":
cnt += 1

print (num_str, "has", cnt, "0-s")

This program will tell you how many 0's are in an integer
1025 has 1 0-s

In [47]:
print("This program will tell you how many 0's are in an integer")
num_str = input("Please enter an integer: ")
cnt = 0

for digit in num_str:
if digit == "0":
cnt = cnt + 1

print(num_str, "has", cnt, "0-s")

This program will tell you how many 0's are in an integer
-10 has 1 0-s


### Counting the number of zeros in a given integer (solution 3)¶

In [48]:
num_str = input("Please enter an integer: ")
cnt = str.count(num_str, "0")
#cnt = num_str.count("0")
print (num_str, "has", cnt, "0's")

Please enter an integer: 10034
10034 has 2 0's

In [ ]: