CompSci Blogs

August 2023 to June 2024

View the Project on GitHub IshanCornick/new_student

19 September 2023

Input/Output

# This is program with dictionary
# Create a dictionary to store information about a person
person = {
    "first_name": "Abdullah",
    "last_name": "Khanani",
    "age": 15,
    "city": "Los Angeles"
}
# Accessing values in the dictionary
print("First Name:", person["first_name"])
print("Last Name:", person["last_name"])
print("Age:", person["age"])
print("City:", person["city"])
# Modifying values in the dictionary
person["city"] = "San Diego"  # Update the city
# Adding a new key-value pair to the dictionary
person["country"] = "USA"
# Accessing the updated values
print("Updated City:", person["city"])
print("Country:", person["country"])
# Deleting a key-value pair from the dictionary
del person["country"]
# Checking if a key exists in the dictionary
if "country" in person:
    print("Country:", person["country"])  # This will not be executed because "country" was deleted
else:
    print("Country not found.")
First Name: Abdullah
Last Name: Khanani
Age: 15
City: Los Angeles
Updated City: San Diego
Country: USA
Country not found.
# This is program with Output
# Input: Length and width of the rectangle
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 = length * width
# Output the result
print(f"The area of the rectangle with length {length} and width {width} is {area} square units.")
The area of the rectangle with length 6.0 and width 90.0 is 540.0 square units.
# Program with Input and Output
# Get user input
user_name = input("Enter your name: ")
# Display a greeting
print("Hello, " + user_name + "! Welcome to the interactive program.")
# Get a number from the user
user_age = input("Enter your age: ")
# Convert the input to an integer
try:
    user_age = int(user_age)
except ValueError:
    print("Invalid age input. Please enter a valid number.")
else:
    # Perform some logic based on the age
    if user_age < 18:
        print("You are a minor.")
    elif user_age >= 18 and user_age < 65:
        print("You are an adult.")
    else:
        print("You are a senior citizen.")
Hello, Ishan! Welcome to the interactive program.
You are a minor.
# Program with List
# Create an empty list
numbers = []
# Get the number of elements in the list from the user
num_elements = int(input("Enter the number of elements in the list: "))
# Get the elements from the user and add them to the list
for i in range(num_elements):
    element = float(input(f"Enter Number {i + 1}: "))
    numbers.append(element)
# Display the list
print("List of numbers:", numbers)
# Calculate and display the sum of the numbers
total = sum(numbers)
print("Sum of numbers:", total)
# Calculate and display the average of the numbers
average = total / len(numbers)
print("Average of numbers:", average)
# Find and display the maximum and minimum values in the list
maximum = max(numbers)
minimum = min(numbers)
print("Maximum value:", maximum)
print("Minimum value:", minimum)
List of numbers: [5.0, 9.0]
Sum of numbers: 14.0
Average of numbers: 7.0
Maximum value: 9.0
Minimum value: 5.0
# Program with Iteration
# Get user input for the number
n = int(input("Enter a number: "))
# Initialize variables
factorial = 1
i = 1
# Calculate factorial using a while loop
while i <= n:
    factorial *= i
    i += 1
# Display the result
print(f"{n}! = {factorial}")
6! = 720
# Program with a Function to perform mathematical and/or a statistical calculations
import statistics
def calculate_statistics(numbers):
    # Calculate mean
    mean = sum(numbers) / len(numbers)
    # Calculate median
    median = statistics.median(numbers)
    # Calculate standard deviation
    std_deviation = statistics.stdev(numbers)
    return mean, median, std_deviation
# Example list of numbers
data = [15, 45, 69, 88, 25, 57, 73, 31, 99]
# Calculate statistics using the function
mean, median, std_deviation = calculate_statistics(data)
# Display the results
print("Data:", data)
print("Mean:", mean)
print("Median:", median)
print("Standard Deviation:", std_deviation)
Data: [15, 45, 69, 88, 25, 57, 73, 31, 99]
Mean: 55.77777777777778
Median: 57
Standard Deviation: 28.981795052143415
# Program with a selection/condition
# Get user input for a number
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
    print(f"{number} is an even number.")
else:
    print(f"{number} is an odd number.")
6 is an even number.
# Finish with a Program with Purpose
# Define a function to calculate the square of a number
def calculate_square(number):
    square = number ** 2
    return square
# Get user input for a number
try:
    number = float(input("Enter a number: "))
except ValueError:
    print("Invalid input. Please enter a valid number.")
else:
    # Calculate the square using the function
    result = calculate_square(number)
    # Display the result
    print(f"The square of {number} is {result}.")
The square of 5.0 is 25.0.
# This is program with dictionary
# Create a dictionary to store information about a person
person = {
    "first_name": "Abdullah",
    "last_name": "Khanani",
    "age": 15,
    "city": "Los Angeles"
}
# Accessing values in the dictionary
print("First Name:", person["first_name"])
print("Last Name:", person["last_name"])
print("Age:", person["age"])
print("City:", person["city"])
# Modifying values in the dictionary
person["city"] = "San Diego"  # Update the city
# Adding a new key-value pair to the dictionary
person["country"] = "USA"
# Accessing the updated values
print("Updated City:", person["city"])
print("Country:", person["country"])
# Deleting a key-value pair from the dictionary
del person["country"]
# Checking if a key exists in the dictionary
if "country" in person:
    print("Country:", person["country"])  # This will not be executed because "country" was deleted
else:
    print("Country not found.")
First Name: Abdullah
Last Name: Khanani
Age: 15
City: Los Angeles
Updated City: San Diego
Country: USA
Country not found.
# This is program with Output
# Input: Length and width of the rectangle
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 = length * width
# Output the result
print(f"The area of the rectangle with length {length} and width {width} is {area} square units.")
The area of the rectangle with length 6.0 and width 90.0 is 540.0 square units.
# Program with Input and Output
# Get user input
user_name = input("Enter your name: ")
# Display a greeting
print("Hello, " + user_name + "! Welcome to the interactive program.")
# Get a number from the user
user_age = input("Enter your age: ")
# Convert the input to an integer
try:
    user_age = int(user_age)
except ValueError:
    print("Invalid age input. Please enter a valid number.")
else:
    # Perform some logic based on the age
    if user_age < 18:
        print("You are a minor.")
    elif user_age >= 18 and user_age < 65:
        print("You are an adult.")
    else:
        print("You are a senior citizen.")
Hello, Ishan! Welcome to the interactive program.
You are a minor.
# Program with List
# Create an empty list
numbers = []
# Get the number of elements in the list from the user
num_elements = int(input("Enter the number of elements in the list: "))
# Get the elements from the user and add them to the list
for i in range(num_elements):
    element = float(input(f"Enter Number {i + 1}: "))
    numbers.append(element)
# Display the list
print("List of numbers:", numbers)
# Calculate and display the sum of the numbers
total = sum(numbers)
print("Sum of numbers:", total)
# Calculate and display the average of the numbers
average = total / len(numbers)
print("Average of numbers:", average)
# Find and display the maximum and minimum values in the list
maximum = max(numbers)
minimum = min(numbers)
print("Maximum value:", maximum)
print("Minimum value:", minimum)
List of numbers: [5.0, 9.0]
Sum of numbers: 14.0
Average of numbers: 7.0
Maximum value: 9.0
Minimum value: 5.0
# Program with Iteration
# Get user input for the number
n = int(input("Enter a number: "))
# Initialize variables
factorial = 1
i = 1
# Calculate factorial using a while loop
while i <= n:
    factorial *= i
    i += 1
# Display the result
print(f"{n}! = {factorial}")
6! = 720
# Program with a Function to perform mathematical and/or a statistical calculations
import statistics
def calculate_statistics(numbers):
    # Calculate mean
    mean = sum(numbers) / len(numbers)
    # Calculate median
    median = statistics.median(numbers)
    # Calculate standard deviation
    std_deviation = statistics.stdev(numbers)
    return mean, median, std_deviation
# Example list of numbers
data = [15, 45, 69, 88, 25, 57, 73, 31, 99]
# Calculate statistics using the function
mean, median, std_deviation = calculate_statistics(data)
# Display the results
print("Data:", data)
print("Mean:", mean)
print("Median:", median)
print("Standard Deviation:", std_deviation)
Data: [15, 45, 69, 88, 25, 57, 73, 31, 99]
Mean: 55.77777777777778
Median: 57
Standard Deviation: 28.981795052143415
# Program with a selection/condition
# Get user input for a number
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
    print(f"{number} is an even number.")
else:
    print(f"{number} is an odd number.")
6 is an even number.
# Finish with a Program with Purpose
# Define a function to calculate the square of a number
def calculate_square(number):
    square = number ** 2
    return square
# Get user input for a number
try:
    number = float(input("Enter a number: "))
except ValueError:
    print("Invalid input. Please enter a valid number.")
else:
    # Calculate the square using the function
    result = calculate_square(number)
    # Display the result
    print(f"The square of {number} is {result}.")
The square of 5.0 is 25.0.