Programs in Python
Program to show the use of various arithmetic operators.
# Assigning values to variables
a = 10
b=3
# Demonstrating arithmetic operators
print("Values: a =", a, ", b =", b)
print("Addition (a + b):", a + b)
print("Subtraction (a - b):", a - b)
print("Multiplication (a * b):", a * b)
print("Division (a / b):", a / b)
print("Floor Division (a // b):", a // b)
print("Modulus (a % b):", a % b)
print("Exponentiation (a * b):", a * b)
Program to show various data types.
# Assigning values to variables
name = "Alice"
age = 25
height = 5.6 # height in feet
is_student = True
# Displaying the values of variables
print("Name:", name)
print("Age:", age)
print("Height:", height, "feet")
print("Is a student:", is_student)
# Performing operations using variables
age_next_year = age + 1
height_in_inches = height * 12
print("\nNext year,", name, "will be", age_next_year, "years old.")
print(name, "is", height_in_inches, "inches tall.")
Program to show the use of various functions in list for accessing,
modifying, indexing etc.
# Creating a list
fruits = ["Apple", "Banana", "Cherry", "Date"]
# Accessing elements
print("Fruits List:", fruits)
print("First Fruit:", fruits[0]) # Accessing the first element
print("Last Fruit:", fruits[-1]) # Accessing the last element
# Modifying elements
fruits[1] = "Blueberry"
print("\nUpdated Fruits List:", fruits)
# Adding elements
[Link]("Elderberry") # Adding at the end
print("\nAfter Adding a Fruit:", fruits)
# Removing elements
[Link]("Cherry") # Removing by value
print("\nAfter Removing a Fruit:", fruits)
# Iterating through the list
print("\nList of Fruits:")
for fruit in fruits:
print(fruit)
Program to illustrate the structure of list in python.
# Creating a list
fruits = ["Apple", "Banana", "Cherry", "Date"]
# Accessing elements
print("Fruits List:", fruits)
print("First Fruit:", fruits[0]) # Accessing the first element
print("Last Fruit:", fruits[-1]) # Accessing the last element
# Modifying elements
fruits[1] = "Blueberry"
print("\nUpdated Fruits List:", fruits)
# Adding elements
[Link]("Elderberry") # Adding at the end
print("\nAfter Adding a Fruit:", fruits)
# Removing elements
[Link]("Cherry") # Removing by value
print("\nAfter Removing a Fruit:", fruits)
# Iterating through the list
print("\nList of Fruits:")
for fruit in fruits:
print(fruit)
# Checking length of the list
print("\nNumber of Fruits:", len(fruits))
Program to show the all the data types in python.
# Integer
age = 25
print(“Integer:”)
print(“Value:”, age, “| Type:”, type(age))
# Float
height = 5.8
print(“\nFloat:”)
print(“Value:”, height, “| Type:”, type(height))
# String
name = “Alice”
print(“\nString:”)
print(“Value:”, name, “| Type:”, type(name))
# Boolean
is_student = True
print(“\nBoolean:”)
print(“Value:”, is_student, “| Type:”, type(is_student))
# List
fruits = [“Apple”, “Banana”, “Cherry”]
print(“\nList:”)
print(“Value:”, fruits, “| Type:”, type(fruits))
# Tuple
coordinates = (10, 20)
print(“\nTuple:”)
print(“Value:”, coordinates, “| Type:”, type(coordinates))
# Dictionary
person = {“name”: “Bob”, “age”: 30}
print(“\nDictionary:”)
print(“Value:”, person, “| Type:”, type(person))
# Set
unique_numbers = {1, 2, 3, 4}
print(“\nSet:”)
print(“Value:”, unique_numbers, “| Type:”, type(unique_numbers))
# NoneType
nothing = None
print(“\nNoneType:”)
print(“Value:”, nothing, “| Type:”, type(nothing))
#Program to show various functions in python.
# Defining a function
def greet(name):
print("Hello, " + name + "!")
# Defining a function that returns a value
def add_numbers(a, b):
return a + b
# Calling the functions
greet("Alice") # Calling greet function
greet("Bob")
result = add_numbers(5, 3) # Calling add_numbers function
print("\nSum of 5 and 3 is:", result)
#Program to check if a number is positive, negative, or zero
# Input from the user
number = float(input("Enter a number: "))
# Using if-else statements to check the number
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
# Program to print the first 10 natural numbers
print("First 10 Natural Numbers:")
for i in range(1, 11): # Loop from 1 to 10
print(i)
# Program to print numbers from 1 to 5 using a while loop
count = 1 # Initialize the counter
print("Numbers from 1 to 5:")
while count <= 5: # Loop while count is less than or equal to 5
print(count)
count += 1 # Increment the counter
# Function to add two numbers
def add(a, b):
return a + b
# Function to subtract two numbers
def subtract(a, b):
return a - b
# Function to multiply two numbers
def multiply(a, b):
return a * b
# Function to divide two numbers
def divide(a, b):
if b != 0:
return a / b
else:
return "Error: Division by zero"
# Main function to call other functions
def main():
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print("\nResults:")
print("Addition:", add(num1, num2))
print("Subtraction:", subtract(num1, num2))
print("Multiplication:", multiply(num1, num2))
print("Division:", divide(num1, num2))
# Run the main function
main()