Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
#types
>>> x = 5
>>> type(x)
>>> x = "name"
>>> type(x)
>>> y = 3.14
>>> type(y)
>>> type(True)
>>> type("4")
>>> type(4.0)
# operators
>>> 10/3
3.3333333333333335
>>> 10/5
2.0
>>> 10//2
5
>>> 10//3
3
>>> -10//3
-4
>>> 4+5
9
>>> 4+ 5.0
9.0
>>> 2**3
8
>>> 4**0.5
2.0
>>> "4"+ "5"
'45'
>>> "4"*5
'44444'
>>> "4"*5.0
Traceback (most recent call last):
File "", line 1, in
"4"*5.0
TypeError: can't multiply sequence by non-int of type 'float'
>>> 10%2
0
>>> 11%2
1
>>> 3 < 4
True
>>> 3<=4
True
>>> 3 < 4 and 3 == 4
False
#retrieving the rightmost digit of an integer
>>> n = 345
>>> n%10
5
>>> n//10
34
>>> (n//10)%10
4
>>> (n%100)//10
4
#conversions between types
>>> y = 5.0
>>> int(y)
5
>>> int(4.4)
4
>>> int(4.6)
4
>>> int(-4.4)
-4
>>> int ("5")
5
>>> int ("5.0")
Traceback (most recent call last):
File "", line 1, in
int ("5.0")
ValueError: invalid literal for int() with base 10: '5.0'
>>> int ("a")
Traceback (most recent call last):
File "", line 1, in
int ("a")
ValueError: invalid literal for int() with base 10: 'a'
>>> float(4)
4.0
>>> float("5.0")
5.0
>>> float("5.7")
5.7
>>> int("4")
4
>>> int("4.0")
Traceback (most recent call last):
File "", line 1, in
int("4.0")
ValueError: invalid literal for int() with base 10: '4.0'
>>> int(float("4.0"))
4
>>> str(4)
'4'
>>> str(4.6)
'4.6'
>>> str()
''
>>> str(type)
""
>>> course = "intro" +str(2) + "cs"
>>> course
'intro2cs'
#conditionals
>>> ================================ RESTART ================================
>>>
Recitation today! great!
Arctic monkeys
>>> ================================ RESTART ================================
>>>
Recitation today! great!
Eviatar Banai
#python's input() function
>>> s = input("Please enter an integer" )
Please enter an integer41a
>>> s
'41a'
>>> x = int(input("Please enter an integer: " ))
Please enter an integer: 41a
Traceback (most recent call last):
File "", line 1, in
x = int(input("Please enter an integer: " ))
ValueError: invalid literal for int() with base 10: '41a'
>>> x = int(input("Please enter an integer: " ))
Please enter an integer: 41
>>> x
41
>>>
#counting the zeros in an integer
Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: 100
100 has 2 zeros
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: 0
0 has 0 zeros
>>> 0//10
0
>>> 0/10
0.0
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: 0
0 has 1 zeros
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: -10
-10 has 0 zeros
>>> for x in "michal":
print(x)
m
i
c
h
a
l
>>> for x in "michal":
print (x *4)
mmmm
iiii
cccc
hhhh
aaaa
llll
>>> for ttt in "michal":
print (ttt *4)
mmmm
iiii
cccc
hhhh
aaaa
llll
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: 0
0 has 1 zeros
0 has 1 zeros
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: -10
-10 has 0 zeros
-10 has 1 zeros
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: 10.5
Traceback (most recent call last):
File "C:\Personal\Teaching\CS-intro\2016b\recitations\1\cnt_zeros_base.py", line 2, in
orig_num = int(input("Please enter an integer: "))
ValueError: invalid literal for int() with base 10: '10.5'
>>> -10