PCEP Module 3 Flashcards

1
Q

==

A

Returns True if operands’ values are equal, and False otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

!=

A

returns True if operands’ values are NOT equal, and False otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

>

A

True if the left operand’s value is greater than the right operand’s value, and False otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

<

A

True if the left operand’s value is less than the right operand’s value, and False otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

> =

A

True if the left operand’s value is greater than or equal to the right operand’s value, and False otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

<=

A

True if the left operand’s value is less than or equal to the right operand’s value, and False otherwis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if-else

A

x = 10

if x < 10: # Condition
print(“x is less than 10”) # Executed if the condition is True.

else:
print(“x is greater than or equal to 10”) # Executed if the condition is False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of…
x = 5
y = 10
z = 8

print(x > y)
print(y > z)

A

False
True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the output of…
x = 10

if x == 10:
print(x == 10)
if x > 5:
print(x > 5)
if x < 10:
print(x < 10)
else:
print(“else”)

A

True
True
else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of…
x = “1”

if x == 1:
print(“one”)
elif x == “1”:
if int(x) > 1:
print(“two”)
elif int(x) < 1:
print(“three”)
else:
print(“four”)
if int(x) == 1:
print(“five”)
else:
print(“six”)

A

four
five

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the output of…
x = 1
y = 1.0
z = “1”

if x == y:
print(“one”)
if y == int(z):
print(“two”)
elif x == y:
print(“three”)
else:
print(“four”)

A

one
two

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

While loop infinite loop

A

while True:
print(“Stuck in an infinite loop.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

While loop “counter”

A

counter = 5
while counter > 2:
print(counter)
counter -= 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

for loop, in elements

A

word = “Python”
for letter in word:
print(letter, end=”*”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

for loop, in range

A

for i in range(1, 10):
if i % 2 == 0:
print(i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

break

A

stops the loop immediately

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

continue

A

stops the current iteration of the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

can loops have an “else” clause?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

range(start, stop, step)

A

“start” is an optional parameter specifying the starting number of the sequence (0 by default)

“stop” is an optional parameter specifying the end of the sequence generated (it is not included),
and

“step” is an optional parameter specifying the difference between the numbers in the sequence (1 by default.)

20
Q

Output?
for i in range(3):
print(i, end=” “)

A

0 1 2

21
Q

Output?
for i in range(6, 1, -2):
print(i, end=” “)

A

6, 4, 2

22
Q

Create a for loop that counts from 0 to 10, and prints odd numbers to the screen. Use the skeleton below:

A

for i in range(0, 11):
if i % 2 != 0:
print(i)

23
Q

Create a while loop that counts from 0 to 10, and prints odd numbers to the screen:

A

x = 1
while x < 11:
if x % 2 != 0:
print(x)
x += 1

24
Q

Create a program with a for loop and a break statement. The program should iterate over characters in an email address, exit the loop when it reaches the @ symbol, and print the part before @ on one line.

A

for ch in “john.smith@pythoninstitute.org”:
if ch == “@”:
break
print(ch, end=””)

25
Q

Create a program with a for loop and a continue statement. The program should iterate over a string of digits, replace each 0 with x, and print the modified string to the screen.

A

for digit in “0165031806510”:
if digit == “0”:
# Line of code.
# Line of code.
# Line of code.

26
Q

What is the output of the following code?

n = 3

while n > 0:
print(n + 1)
n -= 1
else:
print(n)

A

4
3
2
0

27
Q

What is the output of the following code?

n = range(4)

for num in n:
print(num - 1)
else:
print(num)

A

-1
0
1
2
3

28
Q

What is the output of the following code?

for i in range(0, 6, 3):
print(i)

A

0
3

29
Q

Write a nested list

A

my_list = [1, ‘a’, [“list”, 64, [0, 1], False]]

30
Q

What is the output of the following snippet?

lst = [1, 2, 3, 4, 5]
lst_2 = []
add = 0

for number in lst:
add += number
lst_2.append(add)

print(lst_2)

A

[1, 3, 6, 10, 15]

31
Q

Iterate through a list

A

my_list = [“white”, “purple”, “blue”, “yellow”, “green”]

for color in my_list:
print(color)

32
Q

check the list’s length

A

my_list = [“white”, “purple”, “blue”, “yellow”, “green”]
print(len(my_list)) # outputs 5

33
Q

bitwise “and”

A

if both operands are true, the condition is true

34
Q

bitwise “or”

A

any of the operands are true

35
Q

bitwise “not”

A

eturns false if the result is true, and returns true if the result is false

36
Q

15 in binary

A

0000 1111

37
Q

16 in binary

A

0001 0000

38
Q

y=15
y&raquo_space; 1 = 8 (bitwise right shift)

A

0000 1000

39
Q

y=15
x=16
x & y = 0
(bitwise and)

A

0000 0000

40
Q

x=16
˜ x = 240*
(bitwise not)

A

1111 0000

41
Q

y=15
x=16
x ^ y = 31
(bitwise xor)

A

0001 1111

42
Q

y=15
y &laquo_space;3 =
(bitwise left shift)

A

1000 0000

43
Q

What is the output of the following snippet?

x = 4
y = 1

a = x & y
b = x | y
c = ~x # tricky!
d = x ^ 5
e = x&raquo_space; 2
f = x &laquo_space;2

print(a, b, c, d, e, f)

A

0 5 -5 1 1 16

44
Q

What is the output of the following snippet?

x = 1
y = 0

z = ((x == y) and (x == y)) or not(x == y)
print(not(z))

A

False

45
Q

FUnction bs Method

A

A method is owned by the data it works for, while a function is owned by the whole code.