-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyFirstCode.py
416 lines (326 loc) Β· 7.88 KB
/
myFirstCode.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import random
# This is a comment
"""
This is
multi line
comment
"""
# print function
print("Hello World") #outputs: Hello World
# π΄ Variables
x = 5
y = 2
print(x) # outputs: 5
print(y) # outputs: 2
# Assign multiple values to multiple variables:
a, b, c = "Apple", "Mango", "Banana"
print(a) # Outputs: Apple
print(b) # Outputs: Manga
print(c) # Outputs: Banana
# You can assign One Value to Multiple Variables
x = y = z = "Saturn"
print(x) # outputs: Saturn
print(y) # outputs: Saturn
print(z) # outputs: Saturn
# Unpacking
fruits = ["Orange", "Kiwi", "Watermellon"]
x, y, z = fruits
print(x) # outputs: Orange
print(y) # outputs: Kiwi
print(z) # outputs: Watermellon
# π΄ Concatanation
name = "Aditya"
print("My name is " + name )
# π΄ Data Types
x = 8
print(type(x))
# π΄ Random module
print(random.randrange(1, 10)) #prints random number between 1-10
# π΄ Python Casting
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
print(x) # ouputs: 1
print(y) # ouputs: 2
print(z) # ouputs: 3
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
print(x) # outputs: 1.0
print(y) # outputs: 2.8
print(z) # outputs: 3.0
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
print(x) # outputs: s1
print(y) # outputs: 2
print(z) # outputs: 3.0
# π΄ Strings
a = "Hello"
print(a) # outputs: Hello
# Multiline Strings
a = """This is a multiline string,
come back, I still miss you,
but whocares,
hahaha"""
print(a)
# Strings are Arrays
name = "Aditya"
print(name[1]) # outputs: d
# String length with len() function
favFruit = "Watermellon"
print(len(favFruit)) # outputs: 11
#check if a certain phrase or character is present in a string
txt = "God is watching."
print("NOT" in txt) # outputs: False
#check if a certain phrase or character is NOT present in a string
newTxt = "Saturn is my favourite planet."
print("Earth" not in newTxt) # outputs: True
# Slicing
b = "Hello World!"
print(b[2:5]) # outputs: llo
#slice from the start
print(b[:5]) # outputs: Hello
#slice to the end
print(b[2:]) # outputs: llo World!
#Negative Indexing
print(b[-5:-2]) # outputs: orl
a = "Hello World"
#upper case
print(a.upper()) # outputs: HELLO WORLD
#lower case
print(a.lower()) # outputs: hello world
#remove whitespace
print(a.strip()) #outputs: Hello World
#replace string
print(a.replace("H", "M")) # outputs: Mello World
#split string
print(a.split(",")) # outputs: ['Hello World']
# π΄ String format
age = 19
txt = "My name is Aditya, I am {} years old."
print(txt.format(age)) # outputs: My name is Aditya, I am 19 years old.
# π΄ Booleans
# true or false
print(10 > 9) # Outputs: True
print(10 == 9) # Outputs: False
print(10 < 9) # Outputs: False
print(bool("Hello")) # Outputs: True
print(bool(17)) # Outputs: True
# π΄ Lists
fruits = ["apple", "mango", "banana"]
print(fruits) # outputs: ["apple", "mango", "banana"]
print(fruits[2]) # outputs: banana
print(len(fruits)) # outputs: 3
fruits[0] = "grapes" # first element is "grapes" now
print(fruits) # outputs: ["grapes", "mango", "banana"]
print(type(fruits)) # outputs: <class 'list'>
# sort list -- this sorts the list alphanumerically
fruits.sort()
print(fruits) # outputs: ['banana', 'grapes', 'mango']
mynumbers = [190, 220, 87, 54, 97]
mynumbers.sort()
print(mynumbers) # outputs: [54, 87, 97, 190, 220]
# sort list in decending order
mynumbers.sort(reverse = True)
print(mynumbers) # outputs: [220, 190, 97, 87, 54]
#join two lists
list1 = ["a", "b", "c", "d"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3) # outputs: ['a', 'b', 'c', 'd', 1, 2, 3]
# π΄ Tuples
myTuple = ("apple", "mango", "grapes")
print(myTuple) # outputs: ('apple', 'mango', 'grapes')
print(type(myTuple)) # outputs: <class 'tuple'>
print(myTuple[1]) # outputs: mango
#most of the things are same in tuples and lists
# π΄ Set
mySet = {"apple", "mango", "banana"}
print(mySet) # outputs: {'apple', 'mango', 'banana'}
print(type(mySet)) # outputs: <class 'set'>
# π΄ Dictionaries
myDict = {
"name": "Aditya",
"age": 19,
"city": "Mumbai"
}
print(myDict) # outputs: {'name': 'Aditya', 'age': 19, 'city': 'Mumbai'}
print(myDict["name"]) # outputs: Aditya
print(len(myDict)) # outputs: 3
# nested dictionary
myFamily = {
"child1": {
"name": "Aditya",
"age": 19
},
"child2": {
"name": "Rohan",
"age": 23
}
}
# π΄ If Else
a = 33
b = 200
if a > b:
print("a is greater than b")
else:
print("b is greater than a") # outputs: b is greater than a
# elif
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal") # outputs: a and b are equal
# Short Hand If
if a > b: print("a is greater than b") # outputs: b is greater than a
# Short Hand If Else
print("A") if a > b else print("B") # outputs: B
# And
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True") # outputs: Both conditions are True
# Or
if a > b or a > c:
print("At least one of the conditions is True") # outputs: At least one of the conditions is True
# Not
if not a > b:
print("b is greater than a") # outputs: b is greater than a
# Nested If
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!") # outputs: and also above 20!
else:
print("but not above 20.") # outputs: but not above 20.
# Pass
a = 33
b = 200
if b > a:
pass
# π΄ While Loop
i = 1
while i < 6:
print(i)
i += 1
# break
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
# continue
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
# else
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
# π΄ For Loop
fruits = ["apple", "mango", "grapes"]
for item in fruits:
print(item)
#looping through a string
for x in "banana":
print(x)
# break statement in for loop
fruits = ["apple", "mango", "grapes", "banana"]
for x in fruits:
print(x)
if x == "grapes":
break #only apple mango grapes will be printed
fruits = ["apple", "mango", "grapes", "banana"]
for x in fruits:
if x == "grapes":
break
print(x)
# continue statement
cars = ["Porsche", "Mustang", "GTR"]
for x in cars:
if x == "Mustang":
continue
print(x)
# range() function
for x in range(9):
print(x)
print("---------")
for x in range(4, 9):
print(x)
print("---------")
for x in range(4, 30, 9):
print(x)
# nested for loop:
foods = ["Poha", "Panner", "Kheer"]
taste = ["carbs", "protein", "sugarryy"]
for x in foods:
for y in taste:
print(x, y)
# pass statement
for x in [0, 1, 2]:
pass
# π΄ Functions
# creating a function
def my_func():
print("This is a function")
#calling a function
my_func() # outputs: This is a function
my_func() # outputs: This is a function
#arguments in functions
def sum_fun(x, y):
print(x + y)
sum_fun(2, 6) #outputs: 8
sum_fun(5, 9) #outputs: 14
# arbitrary arguments, *args
def my_function(*kids):
print("The youngest child is " + kids[0])
my_function("Emil", "Tobias", "Linus")
#combine positional-only and keyword-only
def my_function(a, b, /, *, c, d):
print(a + b + c + d)
my_function(5, 6, c = 7, d = 8) #outputs: 26
# π΄ Lambda
x = lambda a : a + 10
print(x(5)) # outputs: 15
y = lambda j, k : j * k
print(y(4, 9)) # outputs: 39
# π΄ Arrays
cars = ["Ford", "Volvo", "BMW"]
print(cars)
x = len(cars)
print(x)
for x in cars:
print(x)
cars.append("Honda")
print(cars)
cars.pop(1)
print(cars)
# π΄ Classes/Objects
#creating a class
class myClass:
x = 5
#use class to create objects
p1 = myClass()
print(p1.x)
# __init__() Function
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
# π΄ User Input
username = input("Enter username: ")
print("Username is: " + username)