0% found this document useful (0 votes)
13 views3 pages

Python Programs for Beginners 2025-2026

The document outlines a Python practical program for the academic session 2025-2026, featuring various coding exercises. These include a discount calculator, palindrome checker, time converter, age eligibility checker, number sign checker, number comparison, and day name finder. Each exercise provides a brief description and corresponding Python code to implement the functionality.

Uploaded by

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

Python Programs for Beginners 2025-2026

The document outlines a Python practical program for the academic session 2025-2026, featuring various coding exercises. These include a discount calculator, palindrome checker, time converter, age eligibility checker, number sign checker, number comparison, and day name finder. Each exercise provides a brief description and corresponding Python code to implement the functionality.

Uploaded by

Zidane Syed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Dunes International

Academic Session 2025-2026


Python Practical Program- Solutions

1. Discount Calculator: design a Python program to calculate discounts based on a given


purchase amount?
#Discount Calculator
amount=float(input(' Enter the amount of
the product'))
if amount>=2000 and amount<5000:
billamount=amount-(amount*.10)#10 %
discount
elif amount>=5000 and amount<10000:
billamount=amount-(amount*.20)#20 %
discount
elif amount>10000:
billamount=amount-(amount*.50)#50 %
discount
else:
print('You are not eleigible for discount,
u have to pay',billamount)
print("billamount to be paid", billamount)
2. Palindrome Checker: develop a Python program that checks if a given string is a
palindrome?(eg: palindrome-‘ama’,’malayalam’)
#program to check palindrome
s=input(' enter the string to check palindrom-')
reverse=s[::-1]
if s==reverse:
print(s," is a palindrome")
else:
print(s," is not a palindrome")
3. Time Converter: Write a Python program that converts seconds into hours, minutes, and
seconds.
# Time Converter using if
seconds = int(input("Enter time in seconds: "))

hours = seconds // 3600


remaining = seconds % 3600
minutes = remaining // 60
sec = remaining % 60

print("Hours:", hours)
print("Minutes:", minutes)
print("Seconds:", sec)
4. Age Eligibility Checker: create a Python program to check if a person is eligible to vote
based on age?
# Age Eligibility Checker using if
age = int(input("Enter age: "))

if age >= 18:


print("Eligible to vote")
else:
print("Not eligible to vote")
5. Number Sign Checker: write a Python program that determines the sign of a given
number?
# Number Sign Checker using if
num = float(input("Enter a number: "))

if num > 0:
print("Positive number")
else:
if num < 0:
print("Negative number")
else:
print("Zero")
6. Number Comparison: design a Python program to compare two given numbers?
# Number Comparison using if
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))

if a > b:
print(a, "is greater than", b)
else:
if a < b:
print(b, "is greater than", a)
else:
print("Both numbers are equal")
7. Day Name Finder: create a Python program that takes a day number (1-7) and returns the
corresponding day name?
# Day Name Finder using if-elif
day_num = int(input("Enter day number (1-7): "))

if day_num == 1:
print("Monday")
elif day_num == 2:
print("Tuesday")
elif day_num == 3:
print("Wednesday")
elif day_num == 4:
print("Thursday")
elif day_num == 5:
print("Friday")
elif day_num == 6:
print("Saturday")
elif day_num == 7:
print("Sunday")
else:
print("Invalid day number")

Common questions

Powered by AI

Using simple if-else statements for implementing discount logic, as seen in the Discount Calculator, can be limited by their linear nature and difficulty in scaling. The logic checks ranges sequentially, which works well for a limited number of conditions but becomes cumbersome as more conditions arise. Each new discount tier requires additional elif clauses, increasing code complexity and potential for errors. Additionally, overlapping conditions may not be effectively managed, potentially leading to incorrect discount applications if conditions are not distinctly defined. Refactoring using dictionaries or functions to handle more complex or dynamic discount structures could improve maintainability and scalability .

Input validation is crucial in the Number Sign Checker to ensure the program processes only valid data types and values, reducing errors. For instance, the program assumes numeric input for checking if a number is positive, negative, or zero. If a non-numeric value is given, the program could crash or produce unexpected results. Robust validation checks that inputs are numeric before assignment to variables, preventing type errors and ensuring logical operations' correctness. Protection against erroneous inputs enhances stability and user experience, particularly in dynamic user-entry contexts commonly found in real-world applications .

Nested if-else statements, as used in the Number Sign Checker, can impact readability and maintenance negatively by increasing complexity and logical depth. Each additional nesting level requires more cognitive load to parse and understand, especially for new developers or during quick code reviews, potentially leading to misunderstandings. Maintenance can become more challenging as nested logic may obscure simple modifications or debugging. Flattening the structure into a single if-elif-else sequence can improve clarity by reducing indentation levels, simplifying the comprehension of the program's flow, and consequently easing future updates or enhancements .

The Time Converter function's efficiency can be enhanced by employing Python's built-in libraries and features such as divmod, which combines division and modulus operations in a single step. For instance, instead of performing separate division and modulus operations, divmod can be used as: hours, remaining = divmod(seconds, 3600), and minutes, sec = divmod(remaining, 60). This reduces the number of operations and increases readability. Furthermore, incorporating exception handling could manage incorrect inputs more smoothly, and utilizing functions with parameters could modularize and enhance code reusability, contributing additionally to the program's overall efficiency and maintainability .

Slicing techniques in Python provide a concise and efficient way to reverse a string. The expression s[::-1] reverses the string s in a single step, which is not only more readable but also generally faster as it avoids iterative overhead. In palindrome checking, this advantage becomes apparent as it simplifies the logic, making the code more intuitive. Traditional loops require initializing variables, iterating through elements, and manually appending characters, increasing complexity and the risk of errors .

In the Day Name Finder implementation, the control structure uses a series of if-elif statements to match a number to a day of the week. It expects inputs from 1 to 7, and each number maps to a specific day. For inputs outside this range, the else clause captures unexpected values and outputs 'Invalid day number'. This approach ensures that regardless of the input, the program handles all cases, preventing undefined behavior. However, it implicitly relies on user input to adhere to expected ranges, highlighting the importance of input validation to preclude invalid values before processing .

Chained conditionals, such as if-elif-else structures, provide a clear, efficient method for checking mutually exclusive conditions compared to separate if statements. In the context of day name assignments where each number corresponds uniquely to a day, chained conditionals ensure only the first true condition executes, optimizing performance by eliminating redundant checks. Separate if statements would check every condition, potentially evaluating unnecessary statements, leading to inefficient processing. Moreover, chained conditionals enhance readability by logically grouping related conditions, making the structure more intuitive and cohesive, which is critical for maintenance and debugging .

Modular arithmetic allows extraction of remainder values, facilitating time conversions in a structured way. To convert seconds into hours, minutes, and seconds, it involves dividing the total seconds by 3600 to find the hours: hours = seconds // 3600. The remainder from this division is used to determine the remaining seconds: remaining = seconds % 3600. Minutes are then extracted by dividing the remaining seconds by 60 (minutes = remaining // 60), with the final remainder representing the remaining seconds (sec = remaining % 60). This method efficiently segments total seconds into component time units using basic integer division and modulus operations .

Control structures such as if-else statements in Python can be used to make decisions based on conditions. In the context of determining voting eligibility, a program can use an if statement to check if a person's age is 18 or older: if age >= 18, the program prints 'Eligible to vote', otherwise it prints 'Not eligible to vote'. This illustrates how control structures direct the program flow based on conditional statements, allowing it to perform different actions depending on whether the condition is true or false .

The Python program for comparing two numbers uses if-else control structures to exemplify decision-making processes by evaluating conditions and directing the program flow based on these evaluations. First, it compares two numbers, a and b. If a is greater than b, it executes the block of code associated with this condition, printing that a is greater. If not, it checks if b is greater and prints accordingly; if neither condition is true, indicating equality, it proceeds to the else statement. This mirrors basic decision-making steps where multiple potential outcomes lead to different actions or outputs according to specified conditions .

You might also like