Python Programs for Lab - Basics
1. Printing Different Outputs:
print("Hello, world!")
print("Python is fun.")
print("I", "love", "programming")
2. Simple Arithmetic:
# Addition
result = 5 + 3
print("5 + 3 =", result)
# Subtraction
result = 10 - 2
print("10 - 2 =", result)
# Multiplication
result = 4 * 6
print("4 * 6 =", result)
# Division
result = 15 / 3
print("15 / 3 =", result)
3. Demonstrating Operator Use:
x = 10
y=3
# Addition
addition = x + y
1
print("Addition:", addition)
# Subtraction
subtraction = x - y
print("Subtraction:", subtraction)
# Multiplication
multiplication = x * y
print("Multiplication:", multiplication)
# Division
division = x / y
print("Division:", division)
# Modulus (Remainder)
modulus = x % y
print("Modulus:", modulus)
# Exponentiation
base = 2
exponent = 3
result_exp = base ** exponent
print(base, "to the power of", exponent, "is", result_exp)
# Integer Division
numerator = 10
denominator = 3
result_div = numerator // denominator
print(numerator, "divided by", denominator, "using integer division is", result_div)
4. Conversion of Data Types:
# Convert a string to an integer
num_str = "5"
num_int = int(num_str)
2
print("String to Integer:", num_int)
# Convert an integer to a string
num_int = 10
num_str = str(num_int)
print("Integer to String:", num_str)
# Convert a float to an integer
num_float = 7.8
num_int = int(num_float)
print("Float to Integer:", num_int)
5. Reading Input from User and String concatenation:
# Reading user input as a string
user_input = input("Enter your name: ")
print("Hello, " + user_input + "!")
# Reading user input as an integer
age = int(input("Enter your age: "))
print("You are”, age, “ years old.")
6. Using Different Line Terminators:
print("Line 1")
print("Line 2")
print("Line 1", end=" ")
print("Line 2", end="...")
print("Line 3")
a = 10
b = 20
3
c = 30
print(a, b, c) # Default separator is a space
print(a, b, c, sep=', ') # Using a comma and space as separator
7. Demo for a restaurant order program
# Convert a total number of seconds into hours:minutes:seconds (HH:MM:SS)
# Read the total number of seconds from the user
sec = int(input("Enter seconds: "))
# First, find total whole minutes in the seconds
min = sec // 60 # how many full minutes fit in 'sec'
# From those minutes, find how many full hours
hours = min // 60 # how many full hours fit in 'min'
# Minutes left after removing the full hours
newmin = min % 60 # remainder minutes (0..59)
# Seconds left after removing the first set of full minutes
newsec = sec % 60 # remainder seconds (0..59)
# Show result as HH:MM :SS
print(hours, ":", newmin, ":", newsec)
4
8. Demo for a restaurant order program
# Define the prices of each item
mango_price = 2.5
pasta_price = 8.0
cake_price = 5.0
# Get the quantity of each item from the user
mango_quantity = int(input("How many mangos would you like to order? "))
pasta_quantity = int(input("How many plates of pasta would you like to order? "))
cake_quantity = int(input("How many slices of cake would you like to order? "))
# Calculate the total cost
total_cost = (mango_price * mango_quantity) + (pasta_price * pasta_quantity) + (cake_price *
cake_quantity)
# Print the order details and total cost
print("Order Details:")
print(mango_quantity, "Mango(s) at $", mango_price, "each")
print(pasta_quantity, "Plate(s) of Pasta at $", pasta_price, "each")
print(cake_quantity, "Slice(s) of Cake at $", cake_price, "each")
print("Total Cost: $", total_cost)