Skip to content

Instantly share code, notes, and snippets.

@miquelvir
Created March 16, 2025 22:28
Show Gist options
  • Save miquelvir/65e5572f9d9348a247912f3387cb090e to your computer and use it in GitHub Desktop.
Save miquelvir/65e5572f9d9348a247912f3387cb090e to your computer and use it in GitHub Desktop.
devQuest - 16/03/2025 23:27

Exercise 1

Write a program that determines whether a year is a leap year. Prompt the user to enter a year and output whether it is a leap year.

Teacher solution

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
  print("The year", year, "is a leap year")
else:
  print("The year "+str(year)+" is not a leap year")  # another way to format a message with variables

Test cases

These test cases have been created with AI, cross check anything important.

Leap year divisible by 4 and not by 100

Input

2024

Expected output

The year 2024 is a leap year

Non-leap year

Input

2023

Expected output

The year 2023 is not a leap year

Year divisible by 100 but not by 400

Input

1900

Expected output

The year 1900 is not a leap year

Year divisible by 400

Input

2000

Expected output

The year 2000 is a leap year

Explanation

This explanation has been created with AI, cross check anything important.

The program starts by asking the user to enter a year. The input() function reads the year as text, and int() converts it to a whole number.

year = int(input("Enter a year: "))

The code then checks if the entered year is a leap year using an if statement. A year is a leap year if it's divisible by 4 but not divisible by 100, OR if it's divisible by 400.

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:

year % 4 == 0 checks if the year is divisible by 4 (remainder is 0). year % 100 != 0 checks if the year is NOT divisible by 100. year % 400 == 0 checks if the year is divisible by 400.

(year % 4 == 0 and year % 100 != 0) or year % 400 == 0

If the condition in the if statement is true (the year is a leap year), the program prints a message saying that the year is a leap year. It uses commas to separate the different parts of the output string.

print("The year", year, "is a leap year")

If the condition in the if statement is false (the year is not a leap year), the program executes the else block and prints a message saying that the year is not a leap year. Here the code constructs a string to print using + for concatenation and str() to convert the integer year to a string.

else:
  print("The year "+str(year)+" is not a leap year")

Practice exercises

These exercises have been created with AI, cross check anything important.

  • similar Write a program that asks the user for a number and prints whether it is even or odd.
  • moderate Write a program that takes three numbers as input and prints the largest of the three.
  • challenge Write a program that determines if a given date (day, month, year) is valid. Consider leap years when validating the date. The program should output “Valid Date” or “Invalid Date.”
  • challenge Write a program that takes a year as input and determines the day of the week for January 1st of that year. (Hint: You might need to research Zeller's Congruence or another algorithm for determining the day of the week.)

Exercise 2

Write a program that takes a character as input and determines whether it is a lowercase letter, an uppercase letter, a number, or a symbol, and then prints an appropriate message to the console.

Teacher solution

char = input('Enter a character: ')

if char >= 'a' and char <= 'z':
    print('The letter', char, 'is lowercase')
elif char >= 'A' and char <= 'Z':
    print('The letter', char, 'is uppercase')
elif char >= '0' and char <= '9':
    print(char, 'is a number')
else:
    print(char, 'is a symbol')

Test cases

These test cases have been created with AI, cross check anything important.

Lowercase letter

Input

a

Expected output

The letter a is lowercase

Uppercase letter

Input

A

Expected output

The letter A is uppercase

Number

Input

1

Expected output

1 is a number

Symbol

Input

#

Expected output

# is a symbol

Explanation

This explanation has been created with AI, cross check anything important.

This line prompts the user to enter a character from the keyboard, and stores the input (which will be a string of length 1) into a variable named char.

char = input('Enter a character: ')

This line checks if the character char is a lowercase letter. It does this by comparing the character's ASCII value to the ASCII values of 'a' and 'z'. If char is within that range, it means it's a lowercase letter.

if char >= 'a' and char <= 'z':

If the condition in the if statement is true (i.e., the character is lowercase), this line prints a message to the console indicating that the entered character is a lowercase letter, along with the character itself.

print('The letter', char, 'is lowercase')

If the first if condition is false (meaning the character is not lowercase), the program checks if it's an uppercase letter. It works similarly to the lowercase check, but compares against the ASCII values of 'A' and 'Z'.

elif char >= 'A' and char <= 'Z':

If the elif condition is true (i.e., the character is uppercase), this line prints a message to the console that the entered character is an uppercase letter along with the character itself.

print('The letter', char, 'is uppercase')

If neither the if nor the elif conditions are true (meaning the character is neither lowercase nor uppercase), the program checks if it's a digit (0-9).

elif char >= '0' and char <= '9':

If the character is a digit, this line prints a message indicating that the character is a number, along with the character itself.

print(char, 'is a number')

If none of the previous conditions are met (meaning the character is not a lowercase letter, an uppercase letter, or a digit), this else block is executed.

else:

This line prints a message indicating that the character is a symbol, along with the character itself. This covers any character that is not a letter or a number.

print(char, 'is a symbol')

Practice exercises

These exercises have been created with AI, cross check anything important.

  • similar Write a program that asks the user for a character and checks if it is a vowel (a, e, i, o, u) or a consonant. Consider both uppercase and lowercase letters.
  • moderate Write a program that takes a string as input and determines if it's a valid identifier in Python. A valid identifier starts with a letter (A-Z or a-z) or an underscore (_), followed by letters, numbers, or underscores. It should not start with a number.
  • challenge Write a program that takes a string as input and checks if it's a palindrome, ignoring case and non-alphanumeric characters. A palindrome reads the same forwards and backward. For example, 'Racecar' and 'A man, a plan, a canal: Panama' are palindromes.
  • challenge Write a program that accepts a string as input and encodes it using a Caesar cipher. The Caesar cipher shifts each letter by a fixed number of positions down the alphabet. Allow the user to input the string and the shift value. Handle wrapping around the alphabet (e.g., shifting 'z' by 1 should result in 'a'). Maintain the case of the letters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment