0
0
Pythonprogramming~10 mins

Logical operators in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if both conditions are True using the AND operator.

Python
result = (5 > 3) [1] (2 < 4)
print(result)
Drag options to blanks, or click blank then click option'
Aor
Bnot
Cand
Dxor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'or' instead of 'and' will return True if either condition is True.
2fill in blank
medium

Complete the code to check if at least one condition is True using the OR operator.

Python
result = (10 == 5) [1] (3 < 7)
print(result)
Drag options to blanks, or click blank then click option'
Aor
Bxor
Cnot
Dand
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'and' will return False if any condition is False.
3fill in blank
hard

Fix the error in the code by completing the logical NOT operation.

Python
value = True
result = [1] value
print(result)
Drag options to blanks, or click blank then click option'
Aor
Bnot
Cand
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!' causes a syntax error in Python.
4fill in blank
hard

Fill both blanks to create a condition that checks if x is between 10 and 20 (inclusive).

Python
x = 15
if x [1] 10 [2] x <= 20:
    print("x is between 10 and 20")
Drag options to blanks, or click blank then click option'
A>=
Band
C<
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' for the lower bound.
Using 'or' instead of 'and' will give wrong results.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that includes words longer than 3 letters and converts keys to uppercase.

Python
words = ['cat', 'house', 'dog', 'elephant']
result = [1]: [2] for word in words if len(word) [3] 3
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>' will include shorter words.
Using 'word' as key will not convert to uppercase.