Python Institute PCEP-30-01 Certified Entry-Level Python Programmer Online Training
Python Institute PCEP-30-01 Online Training
The questions for PCEP-30-01 were last updated at Nov 23,2024.
- Exam Code: PCEP-30-01
- Exam Name: Certified Entry-Level Python Programmer
- Certification Provider: Python Institute
- Latest update: Nov 23,2024
What is the expected output of the following code?
def func(data):
for d in data[::2]:
yield d
for x in func(‘abcdef’):
print(x, end=”)
- A . bdf
- B . An empty line.
- C . abcdef
- D . ace
D
Explanation:
Topics: def yield for print() with end parameter list slicing
Try it yourself:
def func(data):
for d in data[::2]:
yield d
for x in func(‘abcdef’):
print(x, end=”) # ace
The generator function will return every second element of the passed data.
You develop a Python application for your company.
You have the following code.
def main(a, b, c, d):
value = a + b * c – d
return value
Which of the following expressions is equivalent to the expression in the function?
- A . (a + b) * (c – d)
- B . a + ((b * c) – d)
- C . None of the above.
- D . (a + (b * c)) – d
D
Explanation:
Topics: addition operator multiplication operator
subtraction operator operator precedence
Try it yourself:
def main(a, b, c, d):
value = a + b * c – d # 3
# value = (a + (b * c)) – d # 3
# value = (a + b) * (c – d) # -3
# value = a + ((b * c) – d) # 3
return value
print(main(1, 2, 3, 4)) # 3
This question is about operator precedence
The multiplication operator has the highest precedence and is therefore executed first.
That leaves the addition operator and the subtraction operator
They both are from the same group and therefore have the same precedence.
That group has a left-to-right associativity.
The addition operator is on the left and is therefore executed next.
And the last one to be executed is the subtraction operator
What is the expected behavior of the following program?
try:
print(5/0)
break
except:
print("Sorry, something went wrong…")
except (ValueError, ZeroDivisionError):
print("Too bad…")
- A . The program will cause a SyntaxError exception.
- B . The program will cause a ValueError exception and output the following message: Too bad…
- C . The program will cause a ValueError exception and output a default error message.
- D . The program will raise an exception handled by the first except block.
- E . The program will cause a ZeroDivisionError exception and output a default error message.
A
Explanation:
Topics: try except break SyntaxError ValueError
ZeroDivisionError
Try it yourself:
try:
print(5/0)
# break except:
print("Sorry, something went wrong…")
# except (ValueError, ZeroDivisionError):
# print("Too bad…")
There are two syntax errors:
break can not be used outside of a loop,
and the default except must be last.
Which of the following variable names are illegal? (Select two answers)
- A . TRUE
- B . True
- C . true
- D . and
B, D
Explanation:
Topics: variable names keywords True and
Try it yourself:
TRUE = 23
true = 42
# True = 7 # SyntaxError: cannot assign to True
# and = 7 # SyntaxError: invalid syntax
You cannot use keywords as variable names.
What is the expected output of the following code?
z = y = x = 1
print(x, y, z, sep=’*’)
- A . x*y*z
- B . 111*
- C . x y z
- D . 1*1*1
- E . 1 1 1
- F . The code is erroneous.
D
Explanation:
Topic: multiple assignment print() with sep parameter
Try it yourself:
z = y = x = 1
print(x, y, z, sep=’*’) # 1*1*1
print(x, y, z, sep=’ ‘) # 1 1 1
print(x, y, z) # 1 1 1
The print() function has a sep parameter which stands for separator.
The default value of the sep parameter is a space character.
You can change it to anything you want.
What is the expected output of the following code?
def func(text, num):
while num > 0:
print(text)
num = num – 1
func(‘Hello’, 3)
- A . An infinite loop.
- B . Hello
Hello
Hello - C . Hello
Hello
Hello
Hello - D . Hello
Hello
A
Explanation:
Topics: def while indentation
Try it yourself:
def func(text, num):
while num > 0:
print(text)
num = num – 1
func(‘Hello’, 3) # An infinite loop
The incrementation of num needs to be inside of the while loop.
Otherwise the condition num > 0 will never be False
It should look like this:
def func(text, num):
while num > 0:
print(text)
num = num – 1
func(‘Hello’, 3)
"""
Hello
Hello
Hello
"""
What is the expected output of the following code?
x = True
y = False
z = False
if not x or y:
print(1)
elif not x or not y and z:
print(2)
elif not x or y or not y and x:
print(3)
else:
print(4)
- A . 4
- B . 3
- C . 2
- D . 1
B
Explanation:
Topics: if elif else not and or operator precedence
Try it yourself:
x = True
y = False
z = False
# if not x or y:
# if (not True) or False:
# if False or False:
if False:
print(1)
# elif not x or not y and z:
# elif (not True) or (not False) and False:
# elif False or True and False:
# elif False or (True and False):
# elif False or False:
elif False:
print(2)
# elif not x or y or not y and x:
# elif (not True) or False or (not False) and True:
# elif False or False or True and True:
# elif False or False or (True and True):
# elif False or False or True:
# elif (False or False) or True:
# elif False or True:
elif True:
print(3) # 3
else:
print(4)
There are three operators at work here.
Of them the not operator has the highest precedence, followed by the and operator.
The or operator has the lowest precedence.
What is the expected output of the following code?
x = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def func(data):
res = data[0][0]
for da in data:
for d in da:
if res < d:
res = d
return res
print(func(x[0]))
- A . The code is erroneous.
- B . 8
- C . 6
- D . 2
- E . 4
E
Explanation:
Topics: def for if list
Try it yourself:
x = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def func(data):
print(‘data:’, data) # [[1, 2], [3, 4]]
res = data[0][0] # 1
print(‘res:’, res)
for da in data:
print(‘da:’, da) # [1, 2] -> [3, 4]
for d in da:
print(‘d:’, d) # 1 -> 2 -> 3 -> 4
if res < d:
res = d
return res
print(func(x[0])) # 4
print(func([[1, 7], [3, 4]])) # 7
This function looks for the highest element
in a two dimensional list (or another iterable).
In the beginning the first number data[0][0] gets taken as possible result.
In the inner for loop every number is compared to the possible result.
If one number is higher it becomes the new possible result.
And in the end the result is the highest number.
Which of the following for loops would output the below number pattern?
11111
22222
33333
44444
55555
- A . for i in range(0, 5):
print(str(i) * 5) - B . for i in range(1, 6):
print(str(i) * 5) - C . for i in range(1, 6):
print(i, i, i, i, i) - D . for i in range(1, 5):
print(str(i) * 5)
B
Explanation:
Topics: for range() str() multiply operator string concatenation
Try it yourself:
for i in range(1, 6):
print(str(i) * 5)
"""
11111
22222
33333
44444
55555
"""
print(‘———-‘)
for i in range(0, 5):
print(str(i) * 5)
"""
00000
11111
22222
33333
44444
"""
print(‘———-‘)
for i in range(1, 6):
print(i, i, i, i, i)
"""
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
"""
print(‘———-‘)
for i in range(1, 5):
print(str(i) * 5)
"""
11111
22222
33333
44444
"""
You need range (1, 6)
because the start value 1 is inclusive and the end value 6 is exclusive. To get the same numbers next to each other (without a space between them) you need to make a string and then use the multiply operator string concatenation
The standard separator of the print() function is one space. print(i, i, i, i, i) gives you one space between each number. It would work with print(i, i, i, i, i, sep=”) but that answer is not offered here.
What is the output of the following snippet?
def fun(x, y, z):
return x + 2 * y + 3 * z
print(fun(0, z=1, y=3))
- A . the snippet is erroneous
- B . 9
- C . 0
- D . 3
B
Explanation:
Topics: positional parameter keyword parameter operator precedence
Try it yourself:
def fun(x, y, z):
return x + 2 * y + 3 * z
print(fun(0, z=1, y=3)) # 9
print(0 + 2 * 3 + 3 * 1) # 9
print(0 + (2 * 3) + (3 * 1)) # 9
print(0 + 6 + (3 * 1)) # 9
print(0 + 6 + 3) # 9
print(6 + 3) # 9
print(9) # 9
The function here works fine.
The keyword arguments do not have to be in the correct order among themselves as long as they are all listed after all positional arguments.
And because multiplication precedes addition 9 gets returned and printed.