Python Basic Code Easy to Learn
1) Area of a rectangle given its length and width:
# Calculate the area of a rectangle
# Hashnode format
area = length * width
return area
# Get the length and width of the rectangle from the user.
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculate the area of the rectangle.
area = calculate_area(length, width)
# Print the area of the rectangle.
print("The area of the rectangle is:", area)
2) Python program to greet a user with their name and age
# Greet a user with their name and age
# Hashnode format
def greet_user(name, age):
print(f"Hello, {name}! You are {age} years old.")
# Get the user's name and age from the user.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
# Greet the user.
greet_user(name, age)
Example output:
3) Python program to check if a number is even or odd
# Check if a number is even or odd
# Get the number to check from the user.
number = int(input("Enter a number: "))
# Check if the number is even or odd.
if is_even(number):
print("The number is even.")
else:
print("The number is odd.")
Example output:
4) Python program to find the maximum and minimum values in a list
# Program to find the maximum and minimum values in a list
numbers = [int(A) for A in input("Enter a list of numbers separated by space: ").split()]
maximum_value = max(numbers)
minimum_value = min(numbers)
print(f"Maximum value: {maximum_value}")
print(f"Minimum value: {minimum_value}")
Example output: