CHP 2 (PYTHON)
CHP 2 (PYTHON)
Question # 1
In [4]: a1 = 5
a2 = 2
a3 = 12
b1 = -3
b2 = 0.5
b3 = 6
a = 1 + a2
print("a = ", a)
b = b1 + a2
print("b = ", b)
c = a1 / b2
print("c = ", c)
d = a1 // a2
print("d = ", d)
e = a1 * a3
print("e = ", e)
f = b1 / b2 / b3
print("f = ", f)
g = b3 // a1 * b1
print("g = ", g)
h = a1 / a2 * b1
print("h = ", h)
i = a1 * b1 / b2
print("i = ", i)
j = a2 / a1 * b1
print("j = ", j)
k = b1 * a2 / a1
print("k = ", k)
l = b1 * a1 / a2 *b1 / b2 * a1
print("l = ", l)
a = 3
b = -1
c = 10.0
d = 2
e = 60
f = -1.0
g = -3
h = -7.5
i = -30.0
j = -1.2000000000000002
k = -1.2
l = 225.0
Question # 2
In [6]: print("b\\ta\no\ta\to\na\to\ta\n")
b\ta
o a o
a o a
Question # 3
In [8]: m1 = 6.8
print("The value is:", m1)
Question # 5
localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 1/5
4/13/23, 7:31 PM Untitled - Jupyter Notebook
In [11]: a = "3"
b = "5"
c = int(a) + int(b)
a1 = int(a) * 7
b1 = int(b) + 9
print(type(a))
print(type(b))
print(type(c))
print(type(a1))
print(type(b1))
<class 'str'>
<class 'str'>
<class 'int'>
<class 'int'>
<class 'int'>
Question # 6
In [12]: num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
print("Sum:", num1 + num2)
print("Product:", num1 * num2)
print("Difference:", num1 - num2)
print("Quotient:", num1 // num2)
print("Remainder:", num1 % num2)
Question # 7
In [15]: # Take three integers as input from the user
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
# Calculate the sum, average, and product of the numbers
sum = num1 + num2 + num3
average = sum / 3
product = num1 * num2 * num3
# Print the results
print("The sum is:", sum)
print("The average is:", average)
print("The product is:", product)
Question # 8
localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 2/5
4/13/23, 7:31 PM Untitled - Jupyter Notebook
Question # 9
In [17]: base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print("The area of the triangle is:", area)
Question # 10
In [18]: speed = 90 # km/h
time = 7 # hours
distance = speed * time
print("The distance traveled is:", distance, "km")
Question # 11
In [20]: a = float(input("Enter the value of a: "))
s = float(input("Enter the value of s: "))
w = a / (s*(s-a))
x = w**(1/2) * a**(1/2)
t = x / s
print("w =", w)
print("x =", x)
print("t =", t)
Question # 12
In [22]: length_inches = float(input("Enter the length in inches: "))
length_feet = length_inches / 12.0
print("The length in feet is: ", length_feet)
Question # 13
localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 3/5
4/13/23, 7:31 PM Untitled - Jupyter Notebook
In [24]: # Ask the user for the number of passed and failed students
num_passed = int(input("Enter the number of passed students: "))
num_failed = int(input("Enter the number of failed students: "))
# Calculate the total number of students
total_students = num_passed + num_failed
# Calculate the percentages of passed and failed students
percent_passed = (num_passed / total_students) * 100
percent_failed = (num_failed / total_students) * 100
# Print the results
print("Percentage of passed students:" , percent_passed,"%")
print("Percentage of failed students:" , percent_failed,"%")
Question # 14
In [31]: SUGAR_PER_COOKIE = 1.5 / 48
BUTTER_PER_COOKIE = 1 / 48
FLOUR_PER_COOKIE = 3 / 48
CHOCO_PER_COOKIE = 0.5 / 48
# Get the desired number of cookies from the user
num_cookies = int(input("How many cookies do you want to make? "))
# Calculate the required amounts of each ingredient
sugar_needed = num_cookies * SUGAR_PER_COOKIE
butter_needed = num_cookies * BUTTER_PER_COOKIE
flour_needed = num_cookies * FLOUR_PER_COOKIE
choco_needed = num_cookies * CHOCO_PER_COOKIE
# Display the results to the user
print("To make",num_cookies,"cookies, you will need:")
print(sugar_needed, ": cups of sugar")
print(butter_needed,": cups of butter")
print(flour_needed, ": cups of flour")
print(choco_needed, ": cups of Choco powder")
Question # 15
In [35]: food_charge = float(input("Enter the charge for the food: "))
# Calculate the 10% tip
tip = 0.1 * food_charge
# Calculate the 5% sales tax
sales_tax = 0.05 * food_charge
# Calculate the total charges
total_charges = food_charge + tip + sales_tax
# Display the tip, sales tax, and total charges
print("Tip:",tip,"$")
print("Sales Tax: ",sales_tax,"$")
print("Total Charges: ",total_charges,"$")
Question # 16
localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 4/5
4/13/23, 7:31 PM Untitled - Jupyter Notebook
7.179516316957191
localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 5/5