Lab 3: Math and Strings in Python | CMSC 105 Elementary Programming - Fall 2024

Lab 3: Math and Strings in Python

Program 1: Math and Strings

math_and_strings.py

''' 
Author name: <Your name here>
This program imports the math module and 
does simple math functions and string splices.
'''

# Import the math module to get access to math functions
import math

# Print the results of some math functions.
print(math.sin(2 * math.pi))
print(math.log(16))
print(round(-12.5))

string_val="Introduction to Computing."

# Splice the string and print the output.
print(string_val[:-1])
print(string_val[2:])

Program 2: Compute Interest

interest.py

''' 
Author name: <Your name here>
This program calculates interest.
'''

# Ask the user to enter a the total amount
total_amount = eval(input("Enter total amount: "))  

# Ask the user to enter a the interest rate
interest_rate = eval(input("Enter the interest rate: "))  

# Calculate the interest using the formula
interest = total_amount * interest_rate

# Print the results
print("The interests is", round(interest, 2))

Program 3: Math Library

library.py

''' 
Author name: <Your name here>
This program solves math equations.
'''

#First import the math module
import math

# Ask the user to enter the value for a
a = eval(input("Enter a number: "))  

# Ask the user to enter the value for b
b = eval(input("Enter another number: "))  

# Solve the equation and print the results
result = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
print(round(result, 2))

# Solve the equation and print the results
result = math.pow(a, b) + math.log(a)
print(round(result, 2))

Program 4: Grade Count

grades.py

''' 
Author name: <Your name here>
This program counts the total of students that get specific grades.
'''

string_val = "User1:Grade-A, User2:Grade-B, User3:Grade-C, User4:Grade-A, User5:Grade-B, User6: Grade-A, User7: Grade-C, User8: Grade-B, User9:Grade-A, User10: Grade-A, User11: Grade-C, User12: Grade-A, User13: Grade-B, User14: Grade-C, User15: Grade-A"

# Count the number of "Grade-A" in the string_val
count_of_A = string_val.count("Grade-A")

# Count the number of "Grade-B" in the string_val
count_of_B = string_val.count("Grade-B")

# Count the number of "Grade-C" in the string_val
count_of_C = string_val.count("Grade-C")

# Display the results
print("The total number of A grades is ", count_of_A)
print("The total number of B grades is ", count_of_B)
print("The total number of C grades is ", count_of_C)