In [9]:
from pprint import pprint
x=['z','a','w']
print(x)
x.sort()
print (x)
In [36]:
print("Hello", "world!")
In [37]:
x=5
y=-3
print(x, type(x))
print(y, type(y))
In [38]:
x = 5.5
print(type(x))
In [39]:
x=5.0
y=-3.2
z=2.2e6
print(x, type(x))
print(z, type(z))
In [40]:
x = "CS1001.py"
y = 'I love python'
print(x, type(x))
print(y, type(y))
In [41]:
print(type(4), type(4.0), type("4"))
In [42]:
i_love_python = True
python_loves_me = False
print(i_love_python, type(i_love_python))
print(python_loves_me, type(python_loves_me))
Addition:
In [45]:
4 + 5
Out[45]:
In [46]:
x = 5
4 + x
Out[46]:
In [47]:
x = 4.0 + 5
print(x, type(x))
Subtraction:
In [48]:
x - 3
Out[48]:
Multiplication:
In [49]:
x * 3
Out[49]:
Division - float and integral with / and //:
In [50]:
10 / 3, 10 // 3
Out[50]:
Power:
In [52]:
2 ** 3, 2 ** 3.0, 3 ** 2
Out[52]:
Modolu:
In [53]:
10 % 3
Out[53]:
String concatenation using +:
In [55]:
"Hello" + " World"
Out[55]:
String duplication using *:
In [58]:
"Bye" * 2
Out[58]:
Strings vs. numbers:
In [59]:
4 + 5
Out[59]:
In [60]:
"4" + "5"
Out[60]:
In [61]:
"4" + 5
In [62]:
4 + "5"
In [63]:
5 < 4
Out[63]:
In [64]:
5 > 4
Out[64]:
In [65]:
5 >= 4
Out[65]:
In [66]:
4 >= 4
Out[66]:
In [73]:
4 <= 3
Out[73]:
In [67]:
5 == 4
Out[67]:
In [68]:
5 == 5.0
Out[68]:
In [69]:
5 == "5"
Out[69]:
In [70]:
5 != 4
Out[70]:
In [71]:
2 + 2 == 4
Out[71]:
In [72]:
2 => 3
In [77]:
x = 1 / 3
print(x)
x == 0.3333333333333333
Out[77]:
In [83]:
print(not True)
a = 2 == 5
print(not a)
In [84]:
True and True
Out[84]:
In [85]:
True and False
Out[85]:
In [86]:
False and False
Out[86]:
In [87]:
True or True
Out[87]:
In [88]:
True or False
Out[88]:
Use the functions int(), float(), and str() to convert between types (we will talk about functions next time):
In [91]:
x = "6"
print(x, type(x))
x = int("6")
print(x, type(x))
In [92]:
float("1.25")
Out[92]:
In [93]:
str(4)
Out[93]:
In [94]:
int("a")
In [98]:
course = "intro" + str(2) + "cs"
print(course)
print("intro", 2, "cs", sep='')
The if condition formula - replace conditions and statements with meaningful code:
if *condition*:
*statement*
*statement*
...
elif *condition*: # 0 or more elif clauses
*statement*
*statement*
...
else: # optional
*statement*
*statement*
Example:
In [101]:
today = "Monday"
strike = "N"
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 == "Thursday" or \
today == "Friday" or today == "Saturday":
print("no intro to CS")
else:
print("Not a day")
while *condition*:
*statement*
*statement*
Example - count how many times 0 appears in an integer number:
In [102]:
num = 2**100
print(num)
In [103]:
count = 0
while num > 0: #what if we changed to >=0?
if num % 10 == 0:
count = count + 1
num = num // 10
print(count)
for *variable* in *iterable*:
*statement*
*statement*
Example - solve the same problem with a str type instead of int:
In [105]:
num = 2**100
count = 0
for digit in str(num):
#print(digit, type(digit))
if digit == "0":
count = count + 1
print(count)
Builtin solution:
In [106]:
num = 2**100
count = str.count(str(num), "0")
print(count)
We can measure which solution is faster:
In [108]:
%%timeit
num = 2**100
count = 0
while num>0: #what if we changed to >=0?
if num % 10 == 0:
count = count + 1
num = num // 10
In [109]:
%%timeit
num = 2**100
count = 0
for digit in str(num):
if digit == "0":
count = count + 1
In [110]:
%%timeit
num = 2**100
count = str.count(str(num), "0")
The builtin solution is 4 times faster than the for solution which is 3 times faster than the while solution.
while solution will not work for num <= 0while solution will not work for non-numerals (e.g, num = "Cola 0 is awesome!")This notebook is part of the Extended introduction to computer science course at Tel-Aviv University.
The notebook was written using Python 3.2 and IPython 0.13.1.
The code is available at https://raw.github.com/yoavram/CS1001.py/master/recitation1.ipynb.
The notebook can be viewed online at http://nbviewer.ipython.org/urls/raw.github.com/yoavram/CS1001.py/master/recitation1.ipynb.
The notebooks is also available as a PDF at https://github.com/yoavram/CS1001.py/blob/master/recitation1.pdf?raw=true.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.