0% found this document useful (0 votes)
12 views4 pages

CHP 2 (Exercise)

Python chapter 2

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)
12 views4 pages

CHP 2 (Exercise)

Python chapter 2

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/ 4

Untitled12 - Jupyter Notebook http://localhost:8889/notebooks/Untitled12.ipynb?kernel_name=python3#Exercise-3.

18#

Name: BILAL

Python Chapter 3(Exercise)

Exercise 3.1
In [3]:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")

Enter a number: 47
47 is odd

Exercise 3.2
In [5]: year = int(input("Enter a year: "))

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):


print(year, "is a leap year")
else:
print(year, "is not a leap year")

Enter a year: 2002


2002 is not a leap year

Exercise 3.3
In [6]: month = int(input("Enter the month number: "))

if month < 1 or month > 12:


print("Invalid month number")
elif month <= 3:
print("The month is in the first quarter of the year")
elif month <= 6:
print("The month is in the second quarter of the year")
elif month <= 9:
print("The month is in the third quarter of the year")
else:
print("The month is in the fourth quarter of the year")

Enter the month number: 12


The month is in the fourth quarter of the year

Exercise 3.4
In [7]: mass = 0.79 # given mass of potassium dichromate
molar_mass = 294 # molar mass of potassium dichromate

# calculate the number of moles


moles = mass / molar_mass

# print the result


print("The number of moles of potassium dichromate is:", moles, "mol")

The number of moles of potassium dichromate is: 0.0026870748299319728 mol

Exercise 3.9
In [8]: num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# check if num2 is a multiple of num1


if num2 % num1 == 0:
print(num2, "is a multiple of", num1)
else:
print(num2, "is not a multiple of", num1)

Enter first number: 47


Enter second number: 5
5 is not a multiple of 47

1 of 4 25/03/2023, 2:24 am
Untitled12 - Jupyter Notebook http://localhost:8889/notebooks/Untitled12.ipynb?kernel_name=python3#Exercise-3.18#

Exercise 3.12
In [12]: x = float(input("Enter the x-coordinate of the point: "))
y = float(input("Enter the y-coordinate of the point: "))

if x == 0 and y == 0:
print("The point lies on the origin.")
elif x == 0:
print("The point lies on the y-axis.")
elif y == 0:
print("The point lies on the x-axis.")
elif x > 0 and y > 0:
print("The point lies in the first quadrant.")
elif x < 0 and y > 0:
print("The point lies in the second quadrant.")
elif x < 0 and y < 0:
print("The point lies in the third quadrant.")
else:
print("The point lies in the fourth quadrant.")

Enter the x-coordinate of the point: 5


Enter the y-coordinate of the point: 2
The point lies in the first quadrant.

Exercise 3.13
In [15]: # function to determine if three points are collinear
def is_collinear(x1, y1, x2, y2, x3, y3):
# use the formula for the slope of a line
slope = (y2 - y1) * (x3 - x2) - (y3 - y2) * (x2 - x1)
return slope == 0

# take user input for three points


x1, y1 = map(float, input("Enter point 1 (x,y): ").split(","))
x2, y2 = map(float, input("Enter point 2 (x,y): ").split(","))
x3, y3 = map(float, input("Enter point 3 (x,y): ").split(","))

# determine if the points are collinear


if is_collinear(x1, y1, x2, y2, x3, y3):
print("The three points are collinear.")
else:
print("The three points are not collinear.")

Enter point 1 (x,y): 5,3


Enter point 2 (x,y): 4,7
Enter point 3 (x,y): 3,1
The three points are not collinear.

Exercise 3.14
In [16]: a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))

if a == b or b == c or c == a:
print("The triangle is an isosceles triangle")
else:
print("The triangle is not an isosceles triangle")

Enter the length of side a: 47


Enter the length of side b: 47
Enter the length of side c: 47
The triangle is an isosceles triangle

Exercise 3.15

2 of 4 25/03/2023, 2:24 am
Untitled12 - Jupyter Notebook http://localhost:8889/notebooks/Untitled12.ipynb?kernel_name=python3#Exercise-3.18#

In [18]: import math

# Take input of point (x,y)


x = float(input("Enter x coordinate of point: "))
y = float(input("Enter y coordinate of point: "))

# Calculate distance of point from origin


distance = math.sqrt(x**2 + y**2)

# Check if point is inside, outside, or on the unit circle


if distance < 1:
print("The point ({}, {}) lies inside the unit circle.".format(x, y))
elif distance == 1:
print("The point ({}, {}) lies on the unit circle.".format(x, y))
else:
print("The point ({}, {}) lies outside the unit circle.".format(x, y))

Enter x coordinate of point: 4


Enter y coordinate of point: 7
The point (4.0, 7.0) lies outside the unit circle.

Exercise 3.16
In [24]: a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
c = int(input("Enter the third integer: "))

# Check if any of the integers are zero


if a == 0 or b == 0 or c == 0:
print("Invalid input: all integers must be nonzero.")
else:
# Sort the integers in ascending order
sides = [a, b, c]
sides.sort()

# Check if the integers could form a right triangle


if sides[0]**2 + sides[1]**2 == sides[2]**2:
print("These integers could form the side lengths of a right triangle.")
else:
print("These integers could not form the side lengths of a right triangle.")

Enter the first integer: 5


Enter the second integer: 5
Enter the third integer: 5
These integers could not form the side lengths of a right triangle.

Exercise 3.17
In [20]: weight= int(input("Enter weight in kg :"))
if weight<=2:
print("The Shipping charges for weight",weight,"kg is Rs. 200")
elif weight<=6:
print("The Shipping charges for weight",weight,"kg is Rs. 400")
elif weight<=10:
print("The Shipping charges for weight",weight,"kg is Rs. 550")
else:
print("The Shipping charges for weight",weight,"kg is Rs. 700")

Enter weight in kg :47


The Shipping charges for weight 47 kg is Rs. 700

Exercise 3.18
In [22]: # Get the date from the user
date = input("Enter a date in mm/dd/yy format: ")

# Split the date into its components


month, day, year = date.split("/")

# Calculate the product of the month and day


product = int(month) * int(day)

# Check if the year is equal to the product minus 2000


if int(year) == product - 2000:
print(date, "is a magic date!")
else:
print(date, "is not a magic date.")

Enter a date in mm/dd/yy format: 12/10/02


12/10/02 is not a magic date.

3 of 4 25/03/2023, 2:24 am
Untitled12 - Jupyter Notebook http://localhost:8889/notebooks/Untitled12.ipynb?kernel_name=python3#Exercise-3.18#

4 of 4 25/03/2023, 2:24 am

You might also like