#!/usr/bin/env python
# coding: utf-8
#
cs1001.py , Tel Aviv University, Fall 2017-2018
#
#
# # Recitation 1
# We have seen 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 had also solved an excercise of counting the number of zeros in a number inputted by the user.
#
# #### Takeaways:
#
#
# - Try to "play" with types, operators, structures, and built-in functions in order to understand the basics of Python.
# - To tackle programming exercises, try to first imagine what you want the solution to do intuitively and then find ways to implement it.
# - Read the instructions for the submission of home assignments and submit your code as a .py file.
# - Test your code before you submit.
#
#
# ## Types and Variables
# In[23]:
x = 3
y = 3.14
c = "intro2CS"
b = True
type(x), type(y), type(c), type(b)
# ## Operators
# In[4]:
x + y
# In[5]:
c + "amir"
# In[63]:
"4" + "5"3 < 4 and 3==4
# In[6]:
c * "amir"
# In[7]:
c * 2
# In[8]:
c * 2.5
# In[9]:
c * 2.0
# In[10]:
2 + 2.0
# In[11]:
2**0.5
# In[12]:
2**2.0
# In[13]:
2.0**2
# In[14]:
10%2
# In[15]:
109%10
# In[17]:
9//2 # rounding down
# In[18]:
3 < 4
# In[19]:
3 == 4
# In[20]:
3 != 4
# In[21]:
True and False
# In[64]:
3 < 4 and 3==4
# In[59]:
-2%10
# In[24]:
type(3 < 4)
# ## Conversion Functions
# In[25]:
int("4")
# In[26]:
int(4.0)
# In[27]:
int("4.0")
# In[65]:
int(4.99) # int() always rounds down
# In[28]:
float(4)
# In[29]:
float("3.14")
# In[30]:
float("3")
# In[31]:
str(43)
# In[32]:
str(True)
# In[35]:
course = "intro" + str(2) + "cs"
course
# ## If-else structure
# In[36]:
today = "Monday"
strike = "No"
my_recitation = "Monday"
if today == "Sunday":
print("Shvizut Yom Alef")
if strike == "Y":
print("Stay home")
else:
print("Lecture in intro to CS!")
elif today == "Wednesday":
print("Another lecture in intro to CS!")
elif today==my_recitation:
print("Go to recitation!")
elif today=="Monday" or today=="Tuesday" or today=="Friday" or today=="Saturday":
print("no intro to CS")
else:
print("Not a day")
# ## Another If-elif-else example
# Two conditional structures:
# In[61]:
n=100
if n > 90 and n%2 == 0:
print("hi")
if n > 99:
print("bye")
else:
print("something")
# Now there is only one conditional structure:
# In[1]:
n=100
if n > 90 and n%2 == 0:
print("hi")
elif n > 99:
print("bye")
else:
print("something")
# ## the function input()
# In[66]:
x = input(" please input an integer: ")
x
# In[67]:
x = input(" please input an integer: ")
int(x)
# ## Introducing Loops!
# ### Simple While Loop
# In[45]:
a = 0
while a < 10:
print(a)
a+=1
print("value of a: " + str(a))
# ### Counting the number of zeros in a given integer (solution 1)
# In[68]:
num = int(input("please input an integer: "))
m = num
cnt = 0
while num > 0: # if we change to >= we get an infinite loop
if num % 10 == 0:
cnt += 1
num = num // 10
print(m, cnt)
# ### Simple For Loop
# In[50]:
for c in "amir":
print(c)
# ### Counting the number of zeros in a given integer (solution 2)
# In[52]:
num = input("please input an integer: ")
cnt = 0
for digit in num:
if digit == "0":
cnt += 1
print(num, cnt)
# ### Counting the number of zeros in a given integer (solution 3)
# In[54]:
num = input("please input an integer: ")
cnt = str.count(num, "0")
print(num, cnt)
# ## Simple Function Example
# In[58]:
def amir(a):
return a+1 # as opposed to print which will not return the value
x = amir(10)
x * 2