1
- #3.1. Expression that evaluates to True in both variables are True
1
+ # 3.1. Expression that evaluates to True if both variables are True
2
2
# and that evaluates to False otherwise
3
+
3
4
x = True
4
5
y = True
5
6
print (x and y )
6
7
7
- #3.2. Expression that evaluates to True if x is False and evaluates
8
+ # 3.2. Expression that evaluates to True if x is False and evaluates
8
9
# to False otherwise
10
+
9
11
x = False
10
12
y = True
11
13
print (not x )
12
14
13
- #3.3. Expression that evaluates to True if at least one of the variables is True
15
+ # 3.3. Expression that evaluates to True if at least one of the variables is True
14
16
# and evaluates to False otherwise
17
+
15
18
x = True
16
19
y = False
17
20
print (x or y )
18
21
19
- #4. Expression that evaluates to True if at most one of the variables is True
22
+ # 4. Expression that evaluates to True if at most one of the variables is True
20
23
# and evaluates to False otherwise
24
+
21
25
full = True
22
26
empty = False
23
27
print (not full or empty )
24
28
25
- #5. True if the light level is less than 0.01 or if the temperature
29
+ # 5. True if the light level is less than 0.01 or if the temperature
26
30
# is above freezing, but not if both condition are true
27
31
28
32
29
33
def automate_camera (light , temperature ):
30
34
return (light < 0.01 ) != (temperature > 0.0 )
31
35
36
+
32
37
automate_camera (0.01 , - 2 )
33
38
34
39
35
40
# exclusive or
41
+
36
42
def automate_camera (light , temperature ):
37
43
if (light < 0.01 ) != (temperature > 0.0 ):
38
44
return True
39
45
else :
40
46
return False
47
+
48
+
41
49
automate_camera (0.00 , - 2 )
42
50
43
- #6.
51
+ # 6.
44
52
45
53
46
- #7. The function returns True if a and b refer to different values and
54
+ # 7. The function returns True if a and b refer to different values and
47
55
# returns False otherwise
56
+
48
57
def different (a , b ):
49
58
return a != b
50
59
60
+
51
61
different (2 , 2 )
52
62
53
63
54
- #8. You are given two float variables, population and land_area.
64
+ # 8. You are given two float variables, population and land_area.
55
65
# a. Write an if statement that will print the population if it is less than
56
66
# 10,000,000
57
67
@@ -60,26 +70,23 @@ def different(a, b):
60
70
land_area = 1222.33
61
71
62
72
if population < 10000000 :
63
- print ("{: f}" . format ( population ) )
73
+ print (f" { population } million" )
64
74
65
75
66
76
# b. Write an if statement that will print the population if it is between
67
77
# 10,000,000 and 35,000,000
68
78
69
- population = 36 .000000
79
+ population = 33 .000000
70
80
land_area = 1222.33
71
81
72
82
if 10.000000 < population < 35.000000 :
73
- print ("{: f}" .format (population ))
74
-
75
- else :
76
- print (None )
83
+ print (f"{ population } millions" )
77
84
78
85
79
86
# c. Write an if statement that will print "Densely populated" if the land
80
87
# density(number of people per unit of area) is greater than 100.
81
88
# d. Write an if statement that will print "Densely populated!" if the land
82
- # density print is greter than 100 and that will print "Sparsely populated"
89
+ # density print is greater than 100 and that will print "Sparsely populated"
83
90
# otherwise.
84
91
85
92
density = 90
@@ -90,19 +97,23 @@ def different(a, b):
90
97
print ("Sparsely populated" )
91
98
92
99
93
- #9. Function convert_tempretures converts temperature t from source units
100
+ # 9. Function convert_temperature converts temperature t from source units
94
101
# to target units using. Function convert_to_celsius convert all source units
95
102
# to celsius, than function convert_from_celsius converts celsius to target
96
103
# units and returns result.
104
+
97
105
def convert_to_celsius (t , source ):
98
106
if source == "F" :
99
- return t - 32 * 5.0 / 9.0
107
+ return ( t - 32 * 5.0 / 9.0 ). __round__ ( 2 )
100
108
elif source == "K" :
101
- return t - 273.15
109
+ return ( t - 273.15 ). __round__ ( 2 )
102
110
elif source == 'C' :
103
111
return t
104
112
105
113
114
+ convert_to_celsius (32 , "K" )
115
+
116
+
106
117
def convert_from_celsius (t , target ):
107
118
if target == "F" :
108
119
return t + 32 * 9.0 / 5.0
@@ -112,6 +123,9 @@ def convert_from_celsius(t, target):
112
123
return t
113
124
114
125
126
+ convert_from_celsius (32 , "F" )
127
+
128
+
115
129
def convert_temperatures (t , source , target ):
116
130
print ('step 1' , t , source , target )
117
131
celsius = convert_to_celsius (t , source )
@@ -120,3 +134,19 @@ def convert_temperatures(t, source, target):
120
134
print ('step 3' , result , celsius , t , source , target )
121
135
return result
122
136
137
+
138
+ # 10. If ph value is below 3.0 - very acidic, between 3 and 7 - acidic
139
+
140
+ user_input = input ("Enter an acidity level: \n " )
141
+
142
+ if len (user_input ) > 0 :
143
+ ph = float (user_input )
144
+ if 3.0 < ph < 7.0 :
145
+ print (f"{ ph } is acidic." )
146
+ elif ph < 3.0 :
147
+ print (f"{ ph } is VERY acidic! Be careful!" )
148
+ else :
149
+ print ("No ph value was given!" )
150
+
151
+
152
+ # 11.
0 commit comments