0% found this document useful (0 votes)
4 views

Python programs

Uploaded by

sasa0757b
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)
4 views

Python programs

Uploaded by

sasa0757b
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/ 3

HHS School System

Practice python programs


1- Basic Input/Output - Take user's name, age and weight as input and display the output with appropriate text
start it by displaying 'User Profile' as header.

# Display header
print("User Profile")
print("-----------")

# Take user input


name = input("Enter your name: ")
age = input("Enter your age: ")
weight = input("Enter your weight (in kg): ")

# Display the collected information


print("\nHere's your profile:")
print(f"Name: {name}")
print(f"Age: {age} years")
print(f"Weight: {weight} kg")

2- Dollar To PKR conversion - Take the amount in dollars as input and convert it into PKR. Display the answer
with appropriate output. Note: Dollar rate per rupee shall be assigned as CONSTANT.

# Constants
USD_TO_PKR_RATE = 278.50 # 1 USD = 278.50 PKR (you can update this rate as needed)

# Display header
print("Dollar to PKR Converter")
print(".......................")

# Take user input


usd_amount = float(input("Enter amount in USD: ")) # float is used for decimal data

# Convert to PKR
pkr_amount = usd_amount * USD_TO_PKR_RATE

# Display the result


print(pkr_amount)
3- Basic calculator - Take two numbers and a mathematical operator (+, -, *, /) as input from user and perform
the operation based on inputs given. Display the answer with appropriate output and in case of multiply or
divide, it should be displayed up till 3 decimal places.

# Calculator with all basic operations using if conditions

print("Basic Calculator")
print("----------------")
print("Operations: + (Add), - (Subtract), * (Multiply), / (Divide)")

# Take input from user


num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

# Initialize result
result = 0

# Perform calculation based on operator


if operator == '+':
result = num1 + num2
print(f"\nResult: {num1} + {num2} = {result}")
elif operator == '-':
result = num1 - num2
print(f"\nResult: {num1} - {num2} = {result}")
elif operator == '*':
result = num1 * num2
print(f"\nResult: {num1} * {num2} = {result:.3f}") # Show 3 decimal places
elif operator == '/':
if num2 == 0:
print("\nError: Cannot divide by zero!")
else:
result = num1 / num2
print(f"\nResult: {num1} / {num2} = {result:.3f}") # Show 3 decimal places
else:
print("\nError: Invalid operator! Please use +, -, *, or /")

4- Even/Odd - Take a number as input and display as output, if it is EVEN or ODD.

# Even or Odd Checker


print("Even or Odd Checker")
print("------------------")
# Take user input
number = int(input("Enter a number: "))
# Check if even or odd
if number % 2 == 0:
print(f"\n{number} is an EVEN number.")
else:
print(f"\n{number} is an ODD number.")
5- DIV, MOD and RANDOM - Take two numbers as input, first be the dividend and second be the divisor. Display
the output by applying DIV MOD function to it, along with an output of generating a random number

import random # Import random module for generating random numbers

print("DIV, MOD, and RANDOM Calculator")


print("------------------------------")

# Take user inputs


dividend = int(input("Enter dividend (number to divide): "))
divisor = int(input("Enter divisor (number to divide by): "))

# Compute DIV (integer division) and MOD (remainder)


div_result = dividend // divisor # Integer division
mod_result = dividend % divisor # Modulo operation

# Generate a random number between 1 and 100


random_number = random.randint(1, 100)

# Display results
print("\nResults:")
print(f"DIV ({dividend} // {divisor}) = {div_result}")
print(f"MOD ({dividend} % {divisor}) = {mod_result}")
print(f"Random number (1-100): {random_number}")

6- Week calculator - Take a day as input from user and display


the output as per the conditions given below;
print("Week Calculator")
print("--------------")

# Take user input (case-insensitive)


day = input("Enter a day (e.g., Mon, Tuesday, etc.): ")

# Check the day and classify


if day in ["mon", "monday"]:
print("\nOutput: Start of the week")
elif day in ["tue", "tuesday", "wed", "wednesday", "thu", "thursday"]:
print("\nOutput: Midweek")
elif day in ["fri", "friday", "sat", "saturday", "sun", "sunday"]:
print("\nOutput: Weekend")
else:
print("\nError: Invalid day entered! Please try again.")

You might also like