0% found this document useful (0 votes)
14 views10 pages

Untitled Document

The document contains several Python programs that demonstrate basic programming concepts such as user input, conditional statements, loops, and pattern printing. Key examples include checking driving license eligibility based on age, generating multiplication tables, determining leap years, checking for palindromes, and calculating student grades based on percentage. Each program is accompanied by explanations and sample outputs to illustrate functionality.

Uploaded by

vanshpkp09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Untitled Document

The document contains several Python programs that demonstrate basic programming concepts such as user input, conditional statements, loops, and pattern printing. Key examples include checking driving license eligibility based on age, generating multiplication tables, determining leap years, checking for palindromes, and calculating student grades based on percentage. Each program is accompanied by explanations and sample outputs to illustrate functionality.

Uploaded by

vanshpkp09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Program Exercise

#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.

# Get user input


name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert input to integer

# 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

# Get user input


num = int(input("Enter a number: ")) # Convert input to integer

# Print the multiplication table


print(f"Multiplication Table of {num}:")
for i in range(1, 11): # Loop from 1 to 10
print(f"{num} x {i} = {num * i}")
Explanation

1.​ User Input:


○​ The program asks the user to enter a number.
○​ The input() function takes the user's input as a string.
○​ int(input()) converts the input into an integer and stores it in the variable
num.
2.​ Loop to Print the Table:
○​ A for loop runs from 1 to 10 using range(1, 11).
○​ In each iteration, the program multiplies num by i and prints the result in the
format:​
num x i = result
3.​ Output Display:
○​ The table is displayed with proper formatting.

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

# Check if the year is a leap 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

1.​ User Input:


○​ The program asks the user to enter a year.
○​ int(input()) converts the input into an integer and stores it in the variable
year.
2.​ Leap Year Conditions:
○​ A year is a leap year if:
■​ It is divisible by 4 but not divisible by 100​
OR
■​ It is divisible by 400 (for century years like 2000, 2400, etc.)
○​ The program uses an if statement to check these conditions.
3.​ Output Display:
○​ If the conditions are met, the program prints that the year is a leap year.
○​ Otherwise, it prints that the year is not a leap year.

Sample Outputs

Example 1: Leap Year

Enter a year: 2024


2024 is a leap year.

Example 2: Not a Leap Year


Enter a year: 2023
2023 is not a leap year.
#Here is a Python program to check whether an input number is a palindrome or not.

Python Program

# Get user input


num = int(input("Enter a number: ")) # Convert input to integer

# Convert number to string and check if it matches its reverse


if str(num) == str(num)[::-1]:
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")

Explanation

1.​ User Input:


○​ The program asks the user to enter a number.
○​ int(input()) converts the input into an integer.
2.​ Checking the Palindrome Condition:
○​ Converts the number to a string using str(num).
○​ Reverses the string using slicing [::-1].
○​ Compares the original string with the reversed string.
○​ If they are equal, it prints that the number is a palindrome.
○​ Otherwise, it prints that it is not a palindrome.

Sample Outputs

Example 1: Palindrome Number

Enter a number: 121


121 is a palindrome.

Example 2: Not a Palindrome

Enter a number: 123


123 is not a palindrome.

Example 3: Single-Digit Number

Enter a number: 7
7 is a palindrome.

Example 4: Large Palindrome Number

Enter a number: 12321


12321 is a palindrome.

#Here are Python programs to print both patterns along with detailed explanations.

Pattern
*
* * *
* * * * *
* * *
*

Python Code:

rows = [1, 3, 5, 3, 1] # Define number of stars in each row

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

for i in range(n, 0, -1): # Loop from 5 down to 1

for j in range(1, i + 1): # Loop from 1 to i

print(j, end=" ") # Print numbers in a row

print() # Move to the next line

Explanation:

1.​ We set n = 5, which represents the number of rows.


2.​ A loop from n down to 1 (for i in range(n, 0, -1)) controls the number of
columns in each row.
3.​ A nested loop (for j in range(1, i + 1)) generates numbers from 1 to i.
4.​ The print(j, end=" ") prints numbers in a row without moving to the next line.
5.​ print() is used after each row to move to a new line.

#Here is a Python program to determine a student's grade based on their percentage of


marks.

# Get user input for percentage

percentage = float(input("Enter the percentage of marks obtained: "))

# Determine the grade based on the percentage

if percentage > 90:

grade = "A"

elif percentage >= 80:

grade = "B"

elif percentage >= 70:

grade = "C"

elif percentage >= 60:

grade = "D"

else:

grade = "E"

# Display the result

print(f"Your grade is: {grade}")


Explanation:

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

Enter the percentage of marks obtained: 85

Your grade is: B

Example 2:

Enter the percentage of marks obtained: 72

Your grade is: C

Example 3:

Enter the percentage of marks obtained: 59

Your grade is: E

#Here is a Python program to determine a student's grade based on the


given criteria.

Python Code:
# Get user input for percentage

percentage = float(input("Enter the percentage of marks obtained: "))

# Determine the grade based on the criteria

if percentage > 85:

grade = "A"

elif percentage < 85 and percentage >= 75:

grade = "B"

elif percentage < 75 and percentage >= 50:

grade = "C"

elif percentage > 30 and percentage <= 50:

grade = "D"

else:

grade = "Reappear"

# Display the result

print(f"Your grade is: {grade}")

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:

Enter the percentage of marks obtained: 88

Your grade is: A

Example 2:

Enter the percentage of marks obtained: 80

Your grade is: B

You might also like