#!/usr/bin/env python
# coding: utf-8
# # CS1001.py, Tel-Aviv University, Spring 2019
# # 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)
# ## Operators
# In[6]:
type("4")
# In[7]:
10 / 3
# In[8]:
10 // 3
# In[9]:
10 // 2.0
# In[ ]:
# In[10]:
4 + 5.0
# In[11]:
2 ** 0.5
# In[12]:
9 ** 0.5
# In[13]:
"4" + "5"
# In[14]:
"4" * 3
# In[15]:
"4" * "5"
# In[16]:
10 % 2
# In[17]:
2 % 10
# In[18]:
(-2) % 10
# In[19]:
3 < 4
# In[20]:
3 < 4 and 3 > 5
# 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
# Operator precedence:
#
#
# ## Conversions
# In[22]:
int(3.14)
# In[25]:
int("3")
# In[26]:
float("3")
# In[27]:
float("3.14")
# In[28]:
str(3)
# In[29]:
str(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")
# ## 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")
# In[39]:
n = 101
if n % 2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("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")
# ## The function input()
# In[32]:
s = input("Please enter an integer: ")
print(s, type(s))
# In[33]:
num = int(input("Please enter an integer: "))
print(num, type(num))
# In[34]:
num = int(input("Please enter an integer: "))
print(num, type(num))
# 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)
# ### 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")
# 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)
# ### 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")
# 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")
# ### 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")
# In[ ]: