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.
>>> x = 3
>>> x
3
>>> type(x)
>>> y = 3.14
>>> type(y)
>>> b = True
>>> type(b)
>>> name = "michal"
>>> type(name)
>>> type(4)
>>> type(4.0)
>>> type("4.0")
>>> 10/3
3.3333333333333335
>>> 10/2
5.0
>>> 10//3
3
>>> 10.0//3
3.0
>>> 4 + 5
9
>>> 4 + 5.0
9.0
>>> 4.0 + 5
9.0
>>> 2**3
8
>>> 9**0.5
3.0
>>> 10%2
0
>>> 11%2
1
>>> 10.5%2
0.5
>>> 10.0%2
0.0
>>> 10%2.0
0.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'
>>> "4"*"5"
Traceback (most recent call last):
File "", line 1, in
"4"*"5"
TypeError: can't multiply sequence by non-int of type 'str'
>>> 3<4
True
>>> 3<4 and 4<5
True
>>> 3<4 and 3==4
False
>>> n = 345
>>> n%10
5
>>> (n//10)%10
4
>>> int(3.14)
3
>>> 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("5.0")
5.0
>>> str(4)
'4'
>>> course = "intro" + str(2) + "CS"
>>> course
'intro2CS'
#The output of running conditional_music.py:
>>> ================================ RESTART ================================
>>>
Beginning of the week
Leonard Cohen
#Trying out the function: input
>>> s = input("please enter a name:")
please enter a name:michal
>>> s
'michal'
>>> s = input("please enter a number:")
please enter a number:56
>>> s
'56'
>>> n = int(input("please enter a number:"))
please enter a number:56
>>> n
56
>>>
#counting the number of zeros in a given integer (working with loops)
#Testing solution 1 (with a while loop)
This program will tell you how many 0's are in an integer
Please enter an integer: 3405
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: 3405
3405 has 1 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
Traceback (most recent call last):
File "C:\Personal\Teaching\CS-intro\2016a\Recitations\1\cnt_zeroes_base.py", line 10, in
if num%10 == 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
#an example for using a for loop, works when we loop over items in a collection (in this case, a string)
>>> for item in "michal":
print(item)
m
i
c
h
a
l
#An int is not a collection, therefore the following code will cause a runtime error
>>> for item in 30:
print(item)
Traceback (most recent call last):
File "", line 1, in
for item in 30:
TypeError: 'int' object is not iterable
#Testing solution 2 as well (with a for loop, after converting the number to string)
>>> ================================ 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: 100
100 has 2 zeros
100 has 2 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 ================================
#Testing solution 3 as well (with str.count after converting the number to string)
>>> ================================ RESTART ================================
>>>
This program will tell you how many 0's are in an integer
Please enter an integer: -10
-10 has 0 zeros (solution 1)
-10 has 1 zeros (solution 2)
-10 has 1 zeros (solution 3)
>>> num = -10
>>> str.count(num, "0")
Traceback (most recent call last):
File "", line 1, in
str.count(num, "0")
TypeError: descriptor 'count' requires a 'str' object but received a 'int'
>>> str.count(str(num), "0")
1
>>> str(num).count("0")
1
>>>