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.
#variable types
>>> x = 5
>>> y = 1.1
>>> name = "michal"
>>> type(x)
>>> type(y)
>>> type(name)
>>> b = True
>>> type(b)
#operators
>>> type("4")
>>> type(4.0)
>>> 10/3
3.3333333333333335
>>> 10/2
5.0
>>> type(10/2)
>>> 10//3
3
>>> 4+5
9
>>> 4 + 5.0
9.0
>>> 9**0.5
3.0
>>> 2**3
8
>>> "4" + "5"
'45'
>>> "4"*5
'44444'
>>> "4"+ 5
Traceback (most recent call last):
File "", line 1, in
"4"+ 5
TypeError: Can't convert 'int' object to str implicitly
>>> "4"*"5"
Traceback (most recent call last):
File "", line 1, in
"4"*"5"
TypeError: can't multiply sequence by non-int of type 'str'
>>> "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
>>> 10%3
1
>>> 3<4
True
>>> 3 < 4 and 3==4
False
>>> 3 < 4 or 3==4
True
>>> 3< 4 and not 3 ==4
True
#finding the rightmost digit of an integer using %
>>> x = 345
>>> x%10
5
>>> (x//10)
34
>>> (x//10)%10
4
#conversions
>>> int(5.0)
5
>>> int(5.1)
5
>>> int(5.6)
5
>>> 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'
>>> float("3.7")
3.7
>>> float(3)
3.0
>>> str(4)
'4'
>>> str(-4)
'-4'
>>> str(-4.0)
'-4.0'
>>> str(4*6)
'24'
>>>
#conditionals - music selector
>>>
Recitation today! great!
Eviatar Banai
>>> ================================ RESTART ================================
>>>
Recitation today! great!
Arctic monkeys
#using the function "input()" + conversions
>>> s = input("Please enter a number: ")
Please enter a number: dajlkasjdlksaj
>>> s
'dajlkasjdlksaj'
>>> type(s)
>>> s = input("Please enter a number ")
Please enter a number 154
>>> s
'154'
>>> int(s)
154
>>> n = int(input("Please enter a number "))
Please enter a number 7865
>>> n
7865
>>> type(n)
>>> n = int(input("Please enter a number "))
Please enter a number hkjhkjdgfd
Traceback (most recent call last):
File "", line 1, in
n = int(input("Please enter a number "))
ValueError: invalid literal for int() with base 10: 'hkjhkjdgfd'
>>>
#counting zeros
>>> ================================ 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
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: -10
-10 has 0 zeros
>>> ================================ 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
Traceback (most recent call last):
File "C:\Personal\Teaching\CS-intro\2016b\recitations\1\cnt_zeros_base.py", line 10, in
while m != 0:
KeyboardInterrupt
>>> ================================ 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: 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
>>>