Untitled Document
Untitled Document
#Here is a simple Python program that takes the user's name and age as input and checks if
they are eligible to apply for a driving license.
# Check eligibility
if age >= 18:
print(f"Hello {name}, you are eligible to apply for a driving
license.")
else:
print(f"Sorry {name}, you must be at least 18 years old to apply
for a driving license.")
Explanation:
1. The program first asks the user to enter their name and stores it in the variable name.
2. It then asks for the age, converts it into an integer using int(), and stores it in age.
3. Using an if statement, the program checks whether age is 18 or greater.
4. If the condition is true, it prints a message confirming eligibility.
5. Otherwise, it prints a message stating that the user is not eligible.
# Here is a Python program that prints the multiplication table of a number entered by the user,
along with an explanation and sample output.
Python Program
Sample Output
Enter a number: 7
Multiplication Table of 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
# Here is a simple Python program to check whether a year entered by the user is a leap year
or not.
Python Program
# Get user input
year = int(input("Enter a year: ")) # Convert input to integer
Explanation
Sample Outputs
Python Program
Explanation
Sample Outputs
Enter a number: 7
7 is a palindrome.
#Here are Python programs to print both patterns along with detailed explanations.
Pattern
*
* * *
* * * * *
* * *
*
Python Code:
for r in rows:
print("* " * r) # Print '*' r times
Explanation:
1. We define a list rows = [1, 3, 5, 3, 1], which contains the number of * in each
row.
2. We use a for loop to iterate through the values in rows.
3. In each iteration, we print * repeated r times, separated by spaces using "* " * r.
4. This generates the desired pattern
Pattern
1 2 3 4 5
1 2 3 4
1 2 3
1 2
Python Code:
n = 5 # Number of rows
Explanation:
grade = "A"
grade = "B"
grade = "C"
grade = "D"
else:
grade = "E"
1. The program asks the user to input the percentage of marks.
2. The input is converted to a float to handle decimal values.
3. Conditional statements (if-elif-else) check the percentage:
○ Above 90% → Grade A
○ 80% to 90% → Grade B
○ 70% to 80% → Grade C
○ 60% to 70% → Grade D
○ Below 60% → Grade E
4. The final grade is printed as output.
Sample Outputs:
Example 1
Example 2:
Example 3:
Python Code:
# Get user input for percentage
grade = "A"
grade = "B"
grade = "C"
grade = "D"
else:
grade = "Reappear"
Explanation:
1.User Input:
○ The program asks the user to enter the percentage.
○ float(input()) is used to accept decimal values.
2.Conditional Statements (if-elif-else):
○ Above 85% → Grade A
○ Between 75% and 85% → Grade B
○ Between 50% and 75% → Grade C
○ Between 30% and 50% → Grade D
○ Below 30% → Reappear
3.Output is displayed based on the calculated grade.
Sample Outputs:
Example 1:
Example 2: