-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
342 lines (252 loc) · 6.33 KB
/
Copy pathapp.py
File metadata and controls
342 lines (252 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# ------------------------ Varibale ------------------------ #
price = 10
rating = 4.4
name = "Abhishek"
is_published = True
print(price)
# Ques: We check in a patient named John Smith. He's 20 years old and is a new patient
full_name = "John Smith"
age = 20
is_new_Patient = True
# ------------------------ Receiving Input ------------------------ #
name = input("What is yout name? ")
print("Hi " + name)
# Ques: Ask two questions: person's name and favourite color. Then, print a message like "Mosh like blue"
name = input("What is yout name? ")
favourite_color = input("What is yout favourite color? ")
print(name + " likes " + favourite_color)
# ------------------------ Type Conversion ------------------------ #
birth_year = input("Birth year: ")
print(type(birth_year))
age = 2025 - int(birth_year)
print(type(age))
print(age)
# Ques: Ask a user their weight (in pounds), convert it to kilograms and print on the terminal
weight_lbs = float(input("Weight (lbs): "))
weight_kg = weight_lbs * 0.45
print(weight_kg)
# ------------------------ String ------------------------ #
course = '''
Hi John
Here is our first email to you
Thank you
The Support team
'''
course = "Python for Beginners"
another = course[:]
print(course[:])
print(another)
# ------- Formatted String ------- #
first = "John"
last = "Smith"
# John [Smith] is a coder
message = first + " [" + last + "] is a coder"
msg = f"{first} [{last}] is a coder"
print(msg)
print(message)
# ------- String Methods ------- #
course = "Python for Beginners"
print(len(course))
print(course.upper())
print(course.lower())
print(course.find("Beginners"))
print(course.replace("Beginners", "Absolute Beginners"))
print(course.replace("P", "J"))
print("python" in course)
# ------------------------ Arithmetic Operations ------------------------ #
print(10**3)
x = 10
x = x + 3
x += 3
x = (10 + 3) * 2**2
print(x)
# exponentiation 2**3
# multiplication or division
# addition or subtraction
x = (2 + 3) * 10 - 3
print(x)
import math
print(math.ceil(2.9))
print(math.floor(2.9))
print(math.cbrt(2))
x = 2.9
print(round(x))
print(abs(-2.9))
# ------------------------ conditionals Statement ------------------------ #
# if it`s hot
# It`s a hot day
# Drink plenty of water
# otherwise if it`s cold
# It`s a cold day
# Wear warm clothes
# otherwise
# It`s a lovely day
is_hot = False
is_cold = False
if is_hot:
print("It`s a hot day")
print("Drink plenty of water")
elif is_cold:
print("It`s a cold day")
print("Wear warm clothes")
else:
print("It`s a lovely day")
print("Enjoy your day")
price = 1000000
has_good_credit = True
if has_good_credit:
down_payment = 0.1 * price
else:
down_payment = 0.2 * price
print(f"Down payment: ${down_payment}")
# # ------- Logical operators ------- #
has_high_income = True
has_good_credit = True
if has_high_income or has_good_credit:
print("Eligible for loan")
has_good_credit = True
has_criminal_record = False
if has_good_credit and not has_criminal_record:
print("Eligible for loan")
temperature = 35
if temperature > 30:
print("It`s a hot day")
else:
print("It`s not a hot day")
# # ------- Comparison operators ------- #
name = "Abhishek Kumar Singh"
if len(name) < 3:
print("Name must be at least 3 characters")
elif len(name) > 50:
print("Name can be a maximum of 50 characters")
else:
print("Name looks good!")
weight = int(input("Weight: "))
unit = input("(L)bs or (K)g: ")
if unit.upper() == "L":
converted = weight * 0.45
print(f"You are {converted} kilos")
else:
converted = weight / 0.45
print(f"You are {converted} pounds")
# ------------------------ Looping Statement ------------------------ #
i = 1
while i <= 5:
print("* " * i)
i = i + 1
print("Done")
# Q: Number Guessing
secret_number = 9
guess_count = 0
guess_limi = 3
while guess_count < guess_limi:
guess = int(input("Guess: "))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
print("Soory, you failed!")
# Q: Building Car game
command = ""
started = False
while command.lower() != "quit":
command = input("> ")
if command == "start":
if started:
print("Car is already started...")
else:
started = True
print("Car started...")
elif command == "stop":
if not started:
print("car is already stopped!")
else:
started = False
print("car stopped.")
elif command == "help":
print(
"""
start - to start the car
stop - to stop the car
quit - to quit
"""
)
elif command == "quit":
break
print("Sorry, I don`t understand that!")
# ------- for loop ------- #
for item in range(5, 10, 2):
print(item)
prices = [10, 20, 30]
total = 0
for price in prices:
total += price
print(f"Total: {total}")
for x in "Python":
print(x)
for x in ["a", "b", "c"]:
print(x)
for x in range(0, 10, 2):
print(x)
print(type(range(5)))
print([1, 2, 3, 4, 5])
names = ["John", "Mary"]
for name in names:
if name.startswith("J"):
print("Found")
break
else:
print("Not found")
# ------- while loop ------- #
guess = 0
answer = 5
while answer != guess:
guess = int(input("Guess: "))
else:
pass
# ------------------------ Function ------------------------ #
def increment(number: int, by: int = 1) -> tuple:
return (number, number + by)
print(increment(2, by=3))
print(increment(2))
# ------- args ------- #
def multiply(*list):
total = 1
for number in list:
total *= number
return total
print(multiply(2, 3, 4, 5))
def save_user(**user):
print(user["name"])
save_user(id=1, name="admin")
# ------- Scope ------- #
message = "a"
def greet():
global message
message = "b"
# print(message)
greet()
print(message)
# ------- Debugging ------- #
def multiply(*numbers):
total = 1
for number in numbers:
total *= number
return total
print("start")
print(multiply(1, 2, 3))
print("finish")
# Q: fizz_buzz Problem
def fizz_buzz(input):
if (input % 3 == 0) and (input % 5 == 0):
return "Fizz Buzz"
if input % 3 == 0:
return "Fizz"
if input % 5 == 0:
return "Buzz"
return input
print(fizz_buzz(15))
print(fizz_buzz(3))
print(fizz_buzz(5))
print(fizz_buzz(30))