1
- #1: what does the following code do?
1
+ # 1: what does the following code do?
2
+
3
+
2
4
def a (b , c , d ):
3
5
pass
4
6
# The 'def' statement defines a function.
5
7
# The 'pass' statement is a null operation.
6
8
7
9
8
- #2: what is the output of the following code?
10
+ # 2: what is the output of the following code?
11
+
12
+
9
13
print (type ([1 , 2 ]))
10
14
# 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?
12
19
13
20
14
- #3: what gets printed?
15
21
def f ():
16
22
pass
23
+
24
+
17
25
print (type (f ()))
18
26
# The argument to the type() call is a return value of
19
27
# a function call, which returns None
20
28
21
29
22
- #4: what should the below code print?
30
+ # 4: what should the below code print?
23
31
print (type (1J ))
24
- #<class 'complex'> or j means complex number
32
+ # <class 'complex'> or j means complex number
25
33
# you can put ‘j’ or ‘J’ after a number to make it imaginary,
26
34
# so you can write complex literals
27
35
28
- #5: what is the output of the following code?
36
+ # 5: what is the output of the following code?
29
37
print (type (lambda : None ))
30
38
# <class 'function'>
31
39
# 'lambda arguments: expression' yields a function object
32
40
33
41
34
- #6: what is the output of the below program?
42
+ # 6: what is the output of the below program?
35
43
a = [1 , 2 , 3 , None , (), [], ]
36
44
print (len (a ))
37
45
# 6
38
46
# The trailing comma in the list is ignored, the rest are legitimate values
39
47
40
- #7: what gets printed?
48
+ # 7: what gets printed?
41
49
print (type (1 / 2 ))
42
50
# <class 'float'>
43
51
# division of an integer by another integer yields a float
44
52
45
- #8: What gets printed?
53
+ # 8: What gets printed?
46
54
d = lambda p : p * 2
47
55
t = lambda p : p * 3
48
56
x = 2
49
57
x = d (x )
50
58
x = t (x )
51
59
x = d (x )
52
60
print (x )
53
- # 24
54
- # start with 2, multiply by 2, multiply by 3, multipy by 2
55
61
56
- #10: What gets printed?
62
+ # 10: What gets printed?
57
63
nums = set ([1 , 1 , 2 , 3 , 3 , 3 , 4 ])
58
64
print (len (nums ))
59
65
# nums is a set, so only unique values are retained.
60
66
61
- #11: What gets printed?
67
+ # 11: What gets printed?
68
+
62
69
x = True
63
70
y = False
64
71
z = False
@@ -70,7 +77,8 @@ def f():
70
77
# yes
71
78
# AND is higher precedence than OR in python and is evaluated first
72
79
73
- # #12: What gets printed?
80
+ # 12: What gets printed?
81
+
74
82
x = True
75
83
y = False
76
84
z = False
@@ -87,36 +95,39 @@ def f():
87
95
# NOT has first precedence, then AND, then OR
88
96
89
97
90
- #14: What gets printed?
98
+ # 14: What gets printed?
99
+
91
100
counter = 1
92
101
93
102
94
- def doLotsOfStuff ():
103
+ def do_lots_of_stuff ():
95
104
global counter
96
- for i in (1 , 2 , 3 ):
105
+ for num in (1 , 2 , 3 ):
97
106
counter += 1
98
107
99
- doLotsOfStuff ()
108
+
109
+ do_lots_of_stuff ()
100
110
print (counter )
111
+
101
112
# 4
102
113
# the counter variable being referenced in the function is the global
103
114
# variable defined outside of the function. Changes to the variable in
104
115
# the function affect the original variable.
105
116
106
- #16: What gets printed?
117
+ # 16: What gets printed?
107
118
print ("\x48 \x49 !" )
108
119
# HI!
109
120
# \x is an escape sequence that means the following 2 digits ares
110
121
# a hexadicmal number encoding a character.
111
122
112
- #17: What gets printed?
123
+ # 17: What gets printed?
113
124
print (0xA + 0xa )
114
125
# 20
115
126
# 0xA and 0xa are both hexadecimal integer literals representing the decimal
116
127
# value 10. Their sum is 20.
117
128
118
129
119
- #18: What gets printed?
130
+ # 18: What gets printed?
120
131
class Parent :
121
132
def __init__ (self , param ):
122
133
self .v1 = param
@@ -126,13 +137,14 @@ class Child(Parent):
126
137
def __init__ (self , param ):
127
138
self .v2 = param
128
139
140
+
129
141
obj = Child (11 )
130
142
print (obj .v1 + " " + obj .v2 )
131
143
# AttributeError: child instance has no attribute 'v1'. self.v1 was never
132
144
# created as a variable since the parent __init__ was not explicitly called.
133
145
134
146
135
- #19: What following python function will return?
147
+ # 19: What following python function will return?
136
148
def my_cool_func (a , b , c ):
137
149
if a > (b + c ):
138
150
return a
@@ -141,6 +153,7 @@ def my_cool_func(a, b, c):
141
153
else :
142
154
return c
143
155
156
+
144
157
my_cool_func (5 , 3 , 2 )
145
158
my_cool_func (7 , 4 , 4 )
146
159
my_cool_func (3 , 5 , 9 )
@@ -149,7 +162,7 @@ def my_cool_func(a, b, c):
149
162
# 9 c
150
163
151
164
152
- #20
165
+ # 20
153
166
for i in range (2 ):
154
167
print (i )
155
168
@@ -160,25 +173,25 @@ def my_cool_func(a, b, c):
160
173
# The default beginning of a range is 0. The range will include the beginning
161
174
# of the range and all numbers up to but not including the end of range(4,5)
162
175
163
- #21
176
+ # 21
164
177
lst = [3 , 4 , 7 , 1 , 1 ]
165
178
result = 0
166
179
for i in range (len (lst )):
167
180
result += lst [i ]
168
181
print (result )
169
182
# 16
170
183
171
- #22 Tuples
184
+ # 22 Tuples
172
185
lst = [("a" , 1 ), ("b" , 2 ), ("c" , 3 )]
173
186
for letter , number in lst :
174
187
print ('{"letter=number"}' .format (* lst ))
175
188
176
- #23 Dictionary
189
+ # 23 Dictionary
177
190
d = {"cat" : "no" ,
178
191
"dog" : "yes" }
179
192
print ("fox" in d )
180
193
181
- #24
194
+ # 24
182
195
a = 10
183
196
b = 7
184
197
if a < 7 :
@@ -192,7 +205,7 @@ def my_cool_func(a, b, c):
192
205
else :
193
206
print ("end" )
194
207
195
- #25 String
208
+ # 25 String
196
209
g = "I'm cat"
197
210
print (g .replace ("cat" , "fox" ))
198
211
0 commit comments