script_7.py - Leap Year Checker
Code
#!/usr/bin/env python3
"""Leap year checker"""
year = int(input("Enter a year: "))
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
Explanation
Line 4: year = int(input("Enter a year: "))
- input() returns string type (str)
- int() converts to integer type (int)
- year stores the integer value
Line 6: if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
- Complex boolean expression with multiple operators
- % is modulo operator (returns remainder)
- year % 4 == 0 checks if divisible by 4
- and requires both sides to be True
- Parentheses () control order of evaluation
- != is "not equal to" operator
- year % 100 != 0 checks if NOT divisible by 100
- or returns True if EITHER side is True
- year % 400 == 0 checks if divisible by 400
- Leap year logic: divisible by 4, unless divisible by 100 (but if divisible by 400, still a leap year)
Line 7: print(f"{year} is a leap year")
- f-string embeds the year variable
- {} placeholder gets replaced with variable value
Line 8: else:
- Handles all non-leap years