1- #1: what does the following code do?
1+ # 1: what does the following code do?
2+
3+
24def a (b , c , d ):
35 pass
46# The 'def' statement defines a function.
57# The 'pass' statement is a null operation.
68
79
8- #2: what is the output of the following code?
10+ # 2: what is the output of the following code?
11+
12+
913print (type ([1 , 2 ]))
1014# Lists are formed by placing a comma-separated
11- #list of expressions in square brackets
15+ # list of expressions in square brackets
16+
17+
18+ # 3: what gets printed?
1219
1320
14- #3: what gets printed?
1521def f ():
1622 pass
23+
24+
1725print (type (f ()))
1826# The argument to the type() call is a return value of
1927# a function call, which returns None
2028
2129
22- #4: what should the below code print?
30+ # 4: what should the below code print?
2331print (type (1J ))
24- #<class 'complex'> or j means complex number
32+ # <class 'complex'> or j means complex number
2533# you can put ‘j’ or ‘J’ after a number to make it imaginary,
2634# so you can write complex literals
2735
28- #5: what is the output of the following code?
36+ # 5: what is the output of the following code?
2937print (type (lambda : None ))
3038# <class 'function'>
3139# 'lambda arguments: expression' yields a function object
3240
3341
34- #6: what is the output of the below program?
42+ # 6: what is the output of the below program?
3543a = [1 , 2 , 3 , None , (), [], ]
3644print (len (a ))
3745# 6
3846# The trailing comma in the list is ignored, the rest are legitimate values
3947
40- #7: what gets printed?
48+ # 7: what gets printed?
4149print (type (1 / 2 ))
4250# <class 'float'>
4351# division of an integer by another integer yields a float
4452
45- #8: What gets printed?
53+ # 8: What gets printed?
4654d = lambda p : p * 2
4755t = lambda p : p * 3
4856x = 2
4957x = d (x )
5058x = t (x )
5159x = d (x )
5260print (x )
53- # 24
54- # start with 2, multiply by 2, multiply by 3, multipy by 2
5561
56- #10: What gets printed?
62+ # 10: What gets printed?
5763nums = set ([1 , 1 , 2 , 3 , 3 , 3 , 4 ])
5864print (len (nums ))
5965# nums is a set, so only unique values are retained.
6066
61- #11: What gets printed?
67+ # 11: What gets printed?
68+
6269x = True
6370y = False
6471z = False
@@ -70,7 +77,8 @@ def f():
7077# yes
7178# AND is higher precedence than OR in python and is evaluated first
7279
73- # #12: What gets printed?
80+ # 12: What gets printed?
81+
7482x = True
7583y = False
7684z = False
@@ -87,36 +95,39 @@ def f():
8795# NOT has first precedence, then AND, then OR
8896
8997
90- #14: What gets printed?
98+ # 14: What gets printed?
99+
91100counter = 1
92101
93102
94- def doLotsOfStuff ():
103+ def do_lots_of_stuff ():
95104 global counter
96- for i in (1 , 2 , 3 ):
105+ for num in (1 , 2 , 3 ):
97106 counter += 1
98107
99- doLotsOfStuff ()
108+
109+ do_lots_of_stuff ()
100110print (counter )
111+
101112# 4
102113# the counter variable being referenced in the function is the global
103114# variable defined outside of the function. Changes to the variable in
104115# the function affect the original variable.
105116
106- #16: What gets printed?
117+ # 16: What gets printed?
107118print ("\x48 \x49 !" )
108119# HI!
109120# \x is an escape sequence that means the following 2 digits ares
110121# a hexadicmal number encoding a character.
111122
112- #17: What gets printed?
123+ # 17: What gets printed?
113124print (0xA + 0xa )
114125# 20
115126# 0xA and 0xa are both hexadecimal integer literals representing the decimal
116127# value 10. Their sum is 20.
117128
118129
119- #18: What gets printed?
130+ # 18: What gets printed?
120131class Parent :
121132 def __init__ (self , param ):
122133 self .v1 = param
@@ -126,13 +137,14 @@ class Child(Parent):
126137 def __init__ (self , param ):
127138 self .v2 = param
128139
140+
129141obj = Child (11 )
130142print (obj .v1 + " " + obj .v2 )
131143# AttributeError: child instance has no attribute 'v1'. self.v1 was never
132144# created as a variable since the parent __init__ was not explicitly called.
133145
134146
135- #19: What following python function will return?
147+ # 19: What following python function will return?
136148def my_cool_func (a , b , c ):
137149 if a > (b + c ):
138150 return a
@@ -141,6 +153,7 @@ def my_cool_func(a, b, c):
141153 else :
142154 return c
143155
156+
144157my_cool_func (5 , 3 , 2 )
145158my_cool_func (7 , 4 , 4 )
146159my_cool_func (3 , 5 , 9 )
@@ -149,7 +162,7 @@ def my_cool_func(a, b, c):
149162# 9 c
150163
151164
152- #20
165+ # 20
153166for i in range (2 ):
154167 print (i )
155168
@@ -160,25 +173,25 @@ def my_cool_func(a, b, c):
160173# The default beginning of a range is 0. The range will include the beginning
161174# of the range and all numbers up to but not including the end of range(4,5)
162175
163- #21
176+ # 21
164177lst = [3 , 4 , 7 , 1 , 1 ]
165178result = 0
166179for i in range (len (lst )):
167180 result += lst [i ]
168181print (result )
169182# 16
170183
171- #22 Tuples
184+ # 22 Tuples
172185lst = [("a" , 1 ), ("b" , 2 ), ("c" , 3 )]
173186for letter , number in lst :
174187 print ('{"letter=number"}' .format (* lst ))
175188
176- #23 Dictionary
189+ # 23 Dictionary
177190d = {"cat" : "no" ,
178191 "dog" : "yes" }
179192print ("fox" in d )
180193
181- #24
194+ # 24
182195a = 10
183196b = 7
184197if a < 7 :
@@ -192,7 +205,7 @@ def my_cool_func(a, b, c):
192205else :
193206 print ("end" )
194207
195- #25 String
208+ # 25 String
196209g = "I'm cat"
197210print (g .replace ("cat" , "fox" ))
198211
0 commit comments