PCEP Module 3 Flashcards
==
Returns True if operands’ values are equal, and False otherwise
!=
returns True if operands’ values are NOT equal, and False otherwise
>
True if the left operand’s value is greater than the right operand’s value, and False otherwise
<
True if the left operand’s value is less than the right operand’s value, and False otherwise
> =
True if the left operand’s value is greater than or equal to the right operand’s value, and False otherwise
<=
True if the left operand’s value is less than or equal to the right operand’s value, and False otherwis
if-else
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.
What is the output of…
x = 5
y = 10
z = 8
print(x > y)
print(y > z)
False
True
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”)
True
True
else
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”)
four
five
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”)
one
two
While loop infinite loop
while True:
print(“Stuck in an infinite loop.”)
While loop “counter”
counter = 5
while counter > 2:
print(counter)
counter -= 1
for loop, in elements
word = “Python”
for letter in word:
print(letter, end=”*”)
for loop, in range
for i in range(1, 10):
if i % 2 == 0:
print(i)
break
stops the loop immediately
continue
stops the current iteration of the loop
can loops have an “else” clause?
yes