Lecture 1
Q1. Write a Python program that checks if a given number is eve
n or odd.
a. When a given number is an integer
b. When a given number is one of an integer or a float, i.e., the
given number may not be an integer. Assume x.0 is a float,
where x is an integer.
Hint: Use the modulo operator (%) to check if the remainder whe
n dividing by 2 is 0 or not.
1st test data’s format
Example 1
Enter a number: 3
Odd
Example 2
Enter a number: 2.6
This number is not an integer.
2nd test data’s format
Example 1
input
3
output
Odd
Example 2
input
2.6
output
This number is not an integer.
Reference Answer (RA)
# Q1-a with 1st format
num = int(input("Enter a number: "))
if (num % 2 == 0):
print("Even")
else:
print("Odd")
# Q1-b with 1st format
num = input("Enter a number: ")
if ("." in num):
print("This number is not an integer.")
else:
num = int(num)
if (num % 2 == 0):
print("Even")
else:
print("Odd")
# Q1-a with 2nd format
num = int(input())
if (num % 2 == 0):
print("Even")
else:
print("Odd")
# Q1-b with 2nd format
num = input()
if ("." in num):
print("This number is not an integer.")
else:
num = int(num)
if (num % 2 == 0):
print("Even")
else:
print("Odd")
Problem Solution Approach (PSA)
* Problem Understanding
This task asks you to write a Python program to determine wheth
er a given number is even or odd under two scenarios:
A. The number is guaranteed to be an integer.
B. The number can be either a float or an integer, and we
must only process it if it is an integer.
The key tool is the modulo operator %, which gives the remainde
r of division. An even number will return 0 when divided by 2 (n
um % 2 == 0), while an odd number will not.
* Approach and Reasoning
Case (Q1-a): Integer Only
● We assume the user will enter a valid integer (e.g., 3, 8, -5).
The logic is simple:
if (num % 2 == 0):
print("Even")
else:
print("Odd")
● This checks divisibility by 2.
Case (Q1-b): Input Could Be a Float
● Here, we can't directly assume the number is an integer.
● The logic:
1. Read the input as a string to check for a decimal
point.
2. If there’s a . in the string, we consider it a float
(e.g., "3.0", "2.6").
3. If there's no ., we can safely convert the string to
an int and check if it’s even or odd.
* Why Not Use float(num).is_integer()?
● Good question! That’s another method, but the RA focuse
s on simple string checking for beginners.
● Using "." in num is a simple way to distinguish input types
without type conversion errors.
* Key Concepts Covered
● input() and type conversion
● if-else condition
● modulo % to determine even/odd
● Basic string checking with in
Q2. Write a program that finds the largest of two numbers entere
d by the user. The answer is output to the third decimal place.
Hint 1: numbers may be float
Hint 2: use if-else only
Hint 3: split()
Hint 4: format() for formating the specified value(s) and insert th
em inside the string's placeholder
1st test data’s format
Example 1
input
25 48
output
48.000
Example 2
input
15.24 31.54
output
31.540
2nd test data’s format
Example 1
input
25, 48
output
48.000
Reference Answer (RA)
# Q2 with 1st format
num1, num2 = input().split()
num1 = float(num1)
num2 = float(num2)
if (num1 >= num2):
print(format(num1, ".3f"))
else:
print(format(num2, ".3f"))
# Q2 with 2nd format
num1, num2 = input().split(",")
num1 = float(num1)
num2 = float(num2)
if (num1 >= num2):
print(format(num1, ".3f"))
else:
print(format(num2, ".3f"))
Problem Solution Approach (PSA)
* Problem Understanding
You are asked to:
● Write a Python program that reads two numbers from the us
er.
● These numbers can be floats or integers.
● The program should compare them and print the larger one.
● The output must be formatted to three decimal places.
* Hints Explained
1. "numbers may be float"
→ Use float() to convert input strings into floating-point nu
mbers.
2. "use if-else only"
→ Don’t use built-in functions like max(); manually com
pare using if.
3. "split()"
→ Used to separate two numbers entered in one line. The s
eparator could be a space (" ") or a comma (","), depending
on the format.
4. "format()"
→ Used to format the output to 3 decimal places, e.g., form
at(num, ".3f") will give 25.000.
* Approach and Reasoning
1. Input: Read a line of input.
2. Split: Use split() to extract two values.
3. Convert: Convert the strings to floats using float().
4. Compare: Use an if-else block to determine the larger
number.
5. Output: Print the larger number formatted to 3 decimal
places using format().
* Extra Tip (Optional Improvement)
To make the program more robust, you can use .strip() to remove
extra whitespace around numbers:
num1, num2 = input().split(",")
num1 = float(num1.strip())
num2 = float(num2.strip())
* Key Concepts Covered
● input(): reads a line from the user.
● split(): splits the string into parts.
● float(): converts a string to a float.
● if-else: basic conditional logic.
● format(num, ".3f"): formats the number to 3 decimal places.
Q3. Create a program that determines if a given year is a leap yea
r, where the given number is a positive integer.
Explanation: Leap years are divisible by 4, but not divisible by 10
0, except when divisible by 400.
Test data’s format
Example 1
input
98
output
Not a leap year
Example 2
input
96
output
Leap year
Example 3
input
1600
output
Leap year
Reference Answer (RA)
# Q3
year = int(input())
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Problem Solution Approach (PSA)
* Problem Understanding
You are asked to create a Python program that checks whether a
given year is a leap year.
* Leap Year Rules (as stated in the problem):
● A year is a leap year if:
1. It is divisible by 4, and
2. It is not divisible by 100,
unless it is also divisible by 400.
In other words:
● Years like 1996, 2004, and 2024 → leap years (divisibl
e by 4, not by 100)
● Years like 1900, 2100 → not leap years (divisible by 1
00 but not by 400)
● Years like 1600, 2000 → leap years (divisible by 400)
* Approach and Reasoning
1. Input: Get a number from the user.
2. Condition Check:
Use the leap year logic:
if (year % 4 == 0 and year % 100 != 0) or (year % 400
== 0)
3. Output: Based on the check, print:
○ "Leap year" or
○ "Not a leap year"
* Key Concepts Covered
● input() and int() for reading and converting user input.
● if-else for decision-making.
● % (modulo operator) to check divisibility.
● Logical operators: and, or, and !=.
Q4. Write a program that checks if a given string contains only di
gits and shows the length of this string.
Explanation: Use the isdigit() and len().
Test data’s format
Example 1
input
fj3hal3113
output
Does not contain only digits, 10
Example 2
input
34209
output
Contains only digits, 5
Example 3
input
0
output
Contains only digits, 1
Reference Answer (RA)
# Q6
string = input()
length = len(string)
if string.isdigit():
print("Contains only digits,", length)
else:
print("Does not contain only digits,", length)
Problem Solution Approach (PSA)
* Problem Understanding
The goal is to write a Python program that:
● Takes a string as input.
● Checks whether the string contains only digits.
● Prints:
○ Whether it contains only digits or not, and
○ The length of the string.
* Approach and Reasoning
1. Input Handling:
○ Use input() to get a string from the user.
2. Check for Digits:
○ Use the string method .isdigit():
■ Returns True if all characters in the string are digi
ts (0–9).
■ Returns False if any character is not a digit (like l
etters, symbols, or spaces).
3. Get String Length:
○ Use len() to calculate how many characters are in the st
ring.
4. Output:
○ Use an if-else statement:
■ If the string contains only digits, print:
"Contains only digits, {length}"
■ Otherwise, print:
"Does not contain only digits, {length}"
* Key Concepts Covered
● input() for reading user input.
● .isdigit() for checking if a string contains only digits.
● len() for counting characters in a string.
● if-else for branching based on condition results.