Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1862dd1

Browse files
Solutions
1 parent 0bcc6b6 commit 1862dd1

27 files changed

+510
-0
lines changed

‎CH2/Checkpoints

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#2.1
2+
width = 5.5
3+
height = 2
4+
print("area is", width * height)
5+
6+
#2.2
7+
miles = 100
8+
kilometers = miles * 1.609
9+
print("Kilometers = ", kilometers)
10+
11+
#2.3
12+
eval(input("Promote message" ))
13+
14+
#2.4
15+
Causes a runtime error
16+
17+
#2.5
18+
Using continuation symbol \
19+
20+
#2.6
21+
miles --> Valid
22+
Test --> Valid
23+
a+b --> Not Valid
24+
b–a --> Not Valid
25+
4#R --> Not Valid
26+
4ドル --> Not Valid
27+
#44 --> Not Valid
28+
apps --> Valid
29+
if --> Keyword
30+
elif --> Keyword
31+
x --> Valid
32+
y --> Valid
33+
radius--> Valid
34+
35+
#2.7
36+
if the variable is one word ,then it should be small. If the variable is more than
37+
one word, first word should be small and concatenate the rest of the variable name by
38+
Capitalizing first letter of each word(ex: valueOfNumber)
39+
40+
#2.8
41+
assignment should be like this --> variableName = value
42+
43+
#2.9
44+
x = 0, y =o and z = 0
45+
46+
#2.10
47+
a has the value of b, a = 2
48+
b has the old value of a, b =1
49+
50+
#2.11
51+
>>> 42/5 = 8.4
52+
>>> 42//5 = 8
53+
>>> 42%5 = 2
54+
>>> 40%5 = 0
55+
>>> 1%2 = 1
56+
>>> 2%1 = 0
57+
>>> 45+4*4-2 = 59
58+
>>> 45+43%5*(23*3%2) = 48
59+
>>> 5**2 = 25
60+
>>> 5.1**2 = 26.009999999999998
61+
62+
#2.12
63+
Tuesday is the 4th day starting from Saturday, after 100 days it wil be thursday
64+
65+
#2.13
66+
25/4 = 5.25
67+
to be integer value write 25//4
68+
69+
#2.14
70+
4.0 / (3.0 * (r + 34)) – 9 * (a + b * c) + (3.0 + d * (2 + a)) / (a + b * d)
71+
72+
#2.15
73+
m*r**2 or m *r*r
74+
75+
#2.16
76+
a += 4 = 5
77+
a -= 4 = -3
78+
a *= 4 = 4
79+
a /= 4 = 0.25
80+
a //= 4 = 0
81+
a %= 4 =1
82+
a = 56 * a + 6 = 62
83+
84+
#2.17
85+
The fractional part is truncated
86+
using int(value) does not change variable value.
87+
88+
#2.18
89+
value = 4.6 --> correct, 4.6
90+
print(int(value)) --> correct, 4
91+
print(round(value)) --> correct, 5
92+
print(eval("4 * 5 + 2")) --> correct, 22
93+
print(int("04")) --> correct, 4
94+
print(int("4.5")) --> error
95+
print(eval("04")) --> error
96+
97+
#2.19
98+
The UNIX epoch is the time 00:00:00 on January 1, 1970 GMT
99+
100+
#2.20
101+
The total seconds from unix epoch
102+
103+
#2.21
104+
int(time.time())

‎CH2/EX2.1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# (Convert Celsius to Fahrenheit) Write a program that reads a Celsius degree from
2+
# the console and converts it to Fahrenheit and displays the result. The formula for
3+
# the conversion is as follows:
4+
# fahrenheit = (9 / 5) * celsius + 32
5+
6+
celsius = eval(input("Enter a Celsius value: "))
7+
fah = (9 / 5) * celsius + 32
8+
print(celsius, "Celsuis is", fah, "Fahrenheit")

‎CH2/EX2.10.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# (Physics: find runway length) Given an airplane’s acceleration a and take-off
2+
# speed v, you can compute the minimum runway length needed for an airplane to
3+
# take off using the following formula:
4+
# Write a program that prompts the user to enter v in meters/second (m/s) and the
5+
# acceleration a in meters/second squared and displays the minimum runway
6+
# length.
7+
8+
speed, acc = eval(input("Enter speed and acceleration: "))
9+
len = speed ** 2 / (2 * acc)
10+
print("The minimum runway length for this airplane is", len, "meters")

‎CH2/EX2.11.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# (Financial application: investment amount) Suppose you want to deposit a
2+
# certain amount of money into a savings account with a fixed annual interest rate.
3+
# What amount do you need to deposit in order to have 5,000ドル in the account after
4+
# three years? The initial deposit amount can be obtained using the following
5+
# formula:
6+
# Write a program that prompts the user to enter final account value, annual interest
7+
# rate in percent, and the number of years, and displays the initial deposit amount.
8+
9+
finAccVal = eval(input("Enter final account value: "))
10+
monthInterRatePerc = eval(input("Enter annual interest rate in percent: ")) / (100 * 12)
11+
numOfMonths = eval(input("Enter number of years: ")) * 12
12+
initialDepositAmount = finAccVal / (1 + monthInterRatePerc) ** numOfMonths
13+
print("Initial deposit value is", initialDepositAmount)

‎CH2/EX2.12.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# (Print a table) Write a program that displays the following table:
2+
# a b a ** b
3+
# 1 2 1
4+
# 2 3 8
5+
# 3 4 81
6+
# 4 5 1024
7+
# 5 6 15625
8+
print("a b a**b")
9+
print("1 2 ", 1 ** 2)
10+
print("2 3 ", 2 ** 3)
11+
print("3 4 ", 3 ** 4)
12+
print("4 5 ", 4 ** 5)
13+
print("5 6 ", 5 ** 6)

‎CH2/EX2.13.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# (Split digits) Write a program that prompts the user to enter a four-digit integer
2+
# and displays the number in reverse order.
3+
num = int(input("Enter an integer: "))
4+
n1 = num % 10
5+
num //= 10
6+
n2 = num % 10
7+
num //= 10
8+
n3 = num % 10
9+
num //= 10
10+
n4 = num
11+
print(n4)
12+
print(n3)
13+
print(n2)
14+
print(n1)

‎CH2/EX2.14.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# (Geometry: area of a triangle) Write a program that prompts the user to enter the
2+
# three points (x1, y1), (x2, y2), and (x3, y3) of a triangle and displays its area.
3+
# The formula for computing the area of a triangle is
4+
5+
x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: "))
6+
7+
side1 = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
8+
side2 = ((x1 - x3) ** 2 + (y1 - y3) ** 2) ** 0.5
9+
side3 = ((x2 - x3) ** 2 + (y2 - y3) ** 2) ** 0.5
10+
s = (side1 + side2 + side3) / 2
11+
area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5
12+
print("The area of the triangle is", area)
13+

‎CH2/EX2.15.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# (Geometry: area of a hexagon) Write a program that prompts the user to enter the
2+
# side of a hexagon and displays its area. The formula for computing the area of a
3+
# hexagon is Area = where s is the length of a side.
4+
5+
side = eval(input("Enter the side: "))
6+
area = (3 * 3 ** 0.5 * side ** 2) / 2
7+
print("The area of the hexagon is", area)

‎CH2/EX2.16.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# (Physics: acceleration) Average acceleration is defined as the change of velocity
2+
# divided by the time taken to make the change, as shown in the following formula:
3+
# Write a program that prompts the user to enter the starting velocity in
4+
# meters/second, the ending velocity in meters/second, and the time span t in
5+
# seconds, and displays the average acceleration.
6+
v0, v1, t = eval(input("Enter v0, v1, and t: "))
7+
a = (v1 - v0) / t
8+
print("The average acceleration is", a)

‎CH2/EX2.17.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# (Health application: compute BMI) Body mass index (BMI) is a measure of health
2+
# based on weight. It can be calculated by taking your weight in kilograms and
3+
# dividing it by the square of your height in meters. Write a program that prompts
4+
# the user to enter a weight in pounds and height in inches and displays the BMI.
5+
# Note that one pound is 0.45359237 kilograms and one inch is 0.0254 meters.
6+
weight = eval(input("Enter weight in pounds:"))
7+
height = eval(input("Enter height in inches:"))
8+
weight = weight *0.45359237
9+
height = height *0.0254
10+
bmi = weight / height**2
11+
print("BMI is",bmi)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /