0% found this document useful (0 votes)
20 views5 pages

CHP 2 (PYTHON)

Python solved exercise pdf

Uploaded by

hamzamughal1549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

CHP 2 (PYTHON)

Python solved exercise pdf

Uploaded by

hamzamughal1549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/13/23, 7:31 PM Untitled - Jupyter Notebook

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)

The value is: 6.8

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)

Enter the first integer: 5


Enter the second integer: 2
Sum: 7
Product: 10
Difference: 3
Quotient: 2
Remainder: 1

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)

Enter the first number: 47


Enter the second number: 25
Enter the third number: 5
The sum is: 77
The average is: 25.666666666666668
The product is: 5875

Question # 8

localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 2/5
4/13/23, 7:31 PM Untitled - Jupyter Notebook

In [16]: import math



# Take user input for the radius of the circle
radius = float(input("Enter the radius of the circle: "))

# Calculate area and circumference using the radius
area = math.pi * radius ** 2
circumference = 2 * math.pi * radius

# Print the results
print("The area of the circle is:", area)
print("The circumference of the circle is:", circumference)

Enter the radius of the circle: 47


The area of the circle is: 6939.778171779853
The circumference of the circle is: 295.3097094374406

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)

Enter the base of the triangle: 8


Enter the height of the triangle: 5.7
The area of the triangle is: 22.8

Question # 10
In [18]: speed = 90 # km/h
time = 7 # hours

distance = speed * time

print("The distance traveled is:", distance, "km")

The distance traveled is: 630 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)

Enter the value of a: 5


Enter the value of s: 10
w = 0.1
x = 0.7071067811865476
t = 0.07071067811865475

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)

Enter the length in inches: 7.8


The length in feet is: 0.65

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,"%")

Enter the number of passed students: 50


Enter the number of failed students: 8
Percentage of passed students: 86.20689655172413 %
Percentage of failed students: 13.793103448275861 %

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")

How many cookies do you want to make? 15


To make 15 cookies, you will need:
0.46875 : cups of sugar
0.3125 : cups of butter
0.9375 : cups of flour
0.15625 : 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,"$")

Enter the charge for the food: 100


Tip: 10.0 $
Sales Tax: 5.0 $
Total Charges: 115.0 $

Question # 16

localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 4/5
4/13/23, 7:31 PM Untitled - Jupyter Notebook

In [36]: import math


x = 8
y = -2
z = 4
A = 8
B = 2
C = 3
D = -9
d = math.fabs(A*x + B*y + C*z + D) / math.sqrt(A**2 + B**2 + C**2)
print(d)

7.179516316957191

localhost:8888/notebooks/Desktop/Untitled.ipynb?kernel_name=python3 5/5

You might also like