Chained Comparison in Python

Level 396 keystrokes

This uses Python's chained comparison operators. The expression 0 <= value <= 100 checks if value is between 0 and 100 inclusive.

Python code

# Returns whether value lies between 0 and 100
1def check_range(value):
# Chained comparison: same as 0 <= value and value <= 100 (both ends included)
2 return 0 <= value <= 100
3
# 0 <= 50 <= 100 holds, so this prints True
4print(check_range(50))
# 150 <= 100 fails, so this prints False
5print(check_range(150))

Output

True
False
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • (× 5
  • )× 5
  • _× 3
  • <× 2
  • =× 2
  • :× 1

More Python examples at level 3

← Python typing practice