Bird
Raised Fist0
Pythonprogramming~10 mins

Logical operators in conditions in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is positive and less than 10.

Python
if num > 0 [1] num < 10:
    print("Number is between 1 and 9")
Drag options to blanks, or click blank then click option'
Aand
Bor
Cnot
Dxor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'or' instead of 'and' which allows numbers outside the range.
Using 'not' which negates a condition incorrectly.
2fill in blank
medium

Complete the code to check if a character is a vowel or a digit.

Python
if char in 'aeiou' [1] char.isdigit():
    print("It's a vowel or a digit")
Drag options to blanks, or click blank then click option'
Aand
Bxor
Cnot
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'and' which requires both conditions to be true.
Using 'not' which negates a condition incorrectly.
3fill in blank
hard

Fix the error in the condition to check if a number is not between 5 and 15.

Python
if num < 5 [1] num > 15:
    print("Number is outside 5 to 15")
Drag options to blanks, or click blank then click option'
Aand
Bor
Cxor
Dnot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'and' which would never be true because a number can't be less than 5 and greater than 15 at the same time.
4fill in blank
hard

Fill both blanks to create a dictionary of words with length greater than 3 and containing letter 'a'.

Python
result = {word: len(word) for word in words if len(word) [1] 3 [2] 'a' in word}
Drag options to blanks, or click blank then click option'
A>
B<
Cand
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for length comparison.
Using 'or' which includes words that don't meet both conditions.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and values for words longer than 4 or containing 'e'.

Python
result = [1]: [2] for word in words if len(word) [3] 4 or 'e' in word
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(word)' as a key or value instead of the word itself.
Using '<' instead of '>' in the condition.

Practice

(1/5)
1. Which logical operator in Python means all conditions must be true for the whole expression to be true?
easy
A. xor
B. and
C. not
D. or

Solution

  1. Step 1: Understand the meaning of 'and'

    The and operator returns true only if both conditions are true.
  2. Step 2: Compare with other operators

    or needs only one true condition, not reverses truth, and xor is not a Python keyword.
  3. Final Answer:

    and -> Option B
  4. Quick Check:

    All conditions true = and [OK]
Hint: Remember: 'and' means all must be true [OK]
Common Mistakes:
  • Confusing 'and' with 'or'
  • Thinking 'not' means all true
  • Using 'xor' which is not a Python keyword
2. Which of the following is the correct syntax to check if variable x is NOT equal to 10 and variable y is greater than 5?
easy
A. if x != 10 and y > 5:
B. if x <> 10 and y > 5:
C. if x != 10 & y > 5:
D. if x != 10 or y > 5:

Solution

  1. Step 1: Check the inequality operator

    != is the correct 'not equal' operator in Python; <> is invalid syntax.
  2. Step 2: Check logical operator usage

    and correctly combines two conditions; & is a bitwise operator and or changes logic.
  3. Final Answer:

    if x != 10 and y > 5: -> Option A
  4. Quick Check:

    Correct inequality and 'and' syntax [OK]
Hint: Use '!=' for not equal and 'and' to combine conditions [OK]
Common Mistakes:
  • Using '<>' instead of '!='
  • Using '&' instead of 'and'
  • Using 'or' when 'and' is needed
3. What will be the output of this code?
age = 20
has_id = False
if age >= 18 and has_id:
    print('Allowed')
else:
    print('Denied')
medium
A. SyntaxError
B. Allowed
C. Denied
D. No output

Solution

  1. Step 1: Evaluate each condition

    age >= 18 is True since 20 >= 18, but has_id is False.
  2. Step 2: Apply 'and' operator

    True and False is False, so the else block runs.
  3. Final Answer:

    Denied -> Option C
  4. Quick Check:

    True and False = False [OK]
Hint: Both conditions must be true for 'and' to print Allowed [OK]
Common Mistakes:
  • Assuming one True is enough with 'and'
  • Confusing 'and' with 'or'
  • Ignoring boolean value of variables
4. Find the error in this code snippet:
if not x > 10 or y < 5
    print('Check passed')
medium
A. Missing colon ':' after condition
B. Wrong use of 'not' operator
C. Incorrect indentation
D. Using 'or' instead of 'and'

Solution

  1. Step 1: Check syntax of if statement

    Python requires a colon ':' at the end of the if condition line.
  2. Step 2: Review other parts

    The use of not and or is correct; indentation is not shown as wrong here.
  3. Final Answer:

    Missing colon ':' after condition -> Option A
  4. Quick Check:

    if statements need ':' [OK]
Hint: Always put ':' after if condition [OK]
Common Mistakes:
  • Forgetting ':' after if
  • Misusing 'not' operator
  • Wrong indentation without colon
5. You want to check if a number n is either less than 0 or greater than 100, but NOT equal to -10. Which condition correctly uses logical operators?
hard
A. if n < 0 or (n > 100 and n != -10):
B. if n < 0 and n > 100 or n != -10:
C. if not (n < 0 or n > 100) and n == -10:
D. if (n < 0 or n > 100) and n != -10:

Solution

  1. Step 1: Understand the condition requirements

    We want numbers less than 0 or greater than 100, but exclude -10.
  2. Step 2: Analyze each option

    if (n < 0 or n > 100) and n != -10: correctly groups the or condition and excludes -10 with and n != -10. if n < 0 or (n > 100 and n != -10): excludes -10 only when >100, not <0 (e.g., n=-10 is true). if not (n < 0 or n > 100) and n == -10: reverses logic and checks for equal -10. if n < 0 and n > 100 or n != -10: mixes and/or incorrectly.
  3. Final Answer:

    if (n < 0 or n > 100) and n != -10: -> Option D
  4. Quick Check:

    Group or, then exclude -10 with and [OK]
Hint: Group or conditions, then exclude with and [OK]
Common Mistakes:
  • Wrong grouping of conditions
  • Misplacing 'not' or 'and'
  • Confusing '!=' with '=='