1+ # Variables
2+ # Assign1
3+ x = "Orange" ; y = z = "Banana"
4+ print (x )
5+ print (y )
6+ print (z )
7+ #Orange
8+ #Banana
9+ #Banana
10+
11+ # Assign2
12+ x = 5 ; y = "John"
13+ print (type (x ))
14+ #<class 'int'>
15+ print (y , "is" , x )
16+ #John is 5
17+
18+ # Assign3
19+ def myfunc ():
20+ print ("Name is " + y )
21+ myfunc ()
22+ #Name is John
23+
24+
25+
26+ # Numbers
27+ # Random
28+ import random
29+ print (random .randrange (1 , 10 ))
30+ #1 to 10
31+ x = int (2.8 ) # y will be 2
32+ y = float (2.8 ) # y will be 2.8
33+ z = float ("3" ) # z will be 3.0
34+ print ("x:" , x , " y:" , y , " z:" , z )
35+ #x: 2 y: 2.8 z: 3.0
36+
37+
38+
39+ # String
40+ # Multiline
41+ x = """Lorem ipsum dolor sit amet,
42+ consectetur adipiscing elit,
43+ sed do eiusmod tempor incididunt
44+ ut labore et dolore magna aliqua."""
45+ print (x )
46+
47+ # Basic Print
48+ x = "Hello, Python!"
49+ print (x [1 ])
50+ #e
51+ print (x [2 :5 ])
52+ #llo
53+
54+ # Each word on new Line
55+ for x in "banana" :
56+ print (x )
57+ #b
58+ #a
59+ #n
60+ #a
61+ #n
62+ #a
63+
64+ # Advanced output
65+ x = "Hello, World!"
66+ print (len (x ))
67+ #13
68+ print (x .upper ())
69+ #HELLO, WORLD!
70+ y = x + x
71+ print (y )
72+ #Hello, World!Hello, World!
73+
74+ txt = "The best things in life are free!"
75+ print ("free" in txt )
76+ #True
77+ if "expensive" not in txt :
78+ print ("No, 'expensive' is NOT present." )
79+ #No, 'expensive' is NOT present.
80+
81+ # Multi variable
82+ quantity = 3
83+ itemno = 567
84+ price = 49.95
85+ myorder = "I want {} pieces of item {} for {} dollars."
86+ print (myorder .format (quantity , itemno , price ))
87+ #I want 3 pieces of item 567 for 49.95 dollars.
88+
89+ x = """\' 1 \\ 2 \n 3 \t 4"""
90+ print (x )
91+ #'1 \2
92+ #3 4
93+
94+
95+
96+ # List, Tuple & Set
97+ thislist = ["apple" , "banana" ]
98+
99+ #basic print
100+ print (thislist [- 1 ])
101+ #banana
102+ print (thislist [0 :2 ])
103+ #['apple', 'banana']
104+ print (thislist [0 :])
105+ #['apple', 'banana']
106+
107+ #add
108+ thislist .insert (9 , "watermelon" )
109+ print (thislist )
110+ #['apple', 'banana', 'watermelon']
111+ thislist .append ("cherry" )
112+ print (thislist )
113+ #['apple', 'banana', 'watermelon', 'cherry']
114+
115+ #extend
116+ tropical = ["pineapple" , "papaya" ]
117+ thislist .extend (tropical )
118+ print (thislist )
119+ #['apple', 'banana', 'watermelon', 'cherry', 'pineapple', 'papaya']
120+
121+ #remove
122+ thislist .remove ("banana" )
123+ print (thislist )
124+ #['apple', 'watermelon', 'cherry', 'pineapple', 'papaya']
125+ thislist .clear ()
126+ print (thislist )
127+ #[]
128+
129+ #more
130+ thislist = ["apple" , "banana" , "cherry" ]
131+ for x in thislist :
132+ print (x )
133+ #apple
134+ #banana
135+ #cherry
136+ i = 0
137+ while i < len (thislist ):
138+ print (thislist [i ])
139+ i = i + 1
140+ #apple
141+ #banana
142+ #cherry
143+
144+ #sort
145+ thislist = [100 , 50 , 65 , 82 , 23 ]
146+ thislist .sort (reverse = True )
147+ print (thislist )
148+ #[100, 82, 65, 50, 23]
149+
150+ #copy list
151+ thislist = ["apple" , "banana" , "cherry" ]
152+ mylist = thislist .copy ()
153+ print (mylist )
154+
155+ #join list
156+ list1 = ["a" , "b" , "c" ]
157+ list2 = [1 , 2 , 3 ]
158+ list3 = list1 + list2
159+ print (list3 )
160+ #['a', 'b', 'c', 1, 2, 3]
161+
162+
163+ # Tuples(Cant be changed)
164+ thistuple = ("apple" , "banana" , "cherry" )
165+ print (thistuple )
166+ #('apple', 'banana', 'cherry')
167+ print (len (thistuple ))
168+ #3
169+ print (thistuple [1 ])
170+ #banana
171+
172+ #remove
173+ thistuple = ("apple" , "banana" , "cherry" )
174+ y = list (thistuple )
175+ y .remove ("apple" )
176+ thistuple = tuple (y )
177+ print (thistuple )
178+
179+ #multiply tuple
180+ fruits = ("apple" )
181+ mytuple = fruits * 2
182+ print (mytuple )
183+ #appleapple
184+
185+ #intersection
186+ x = {"apple" , "banana" , "cherry" }
187+ y = {"google" , "microsoft" , "apple" }
188+ x .intersection_update (y )
189+ print (x )
190+ #{'apple'}
191+
192+
193+
194+ # Dictionary
195+ thisdict = {
196+ "brand" : "Ford" ,
197+ "model" : "Mustang" ,
198+ "year" : 1964 ,
199+ "year" : 2020
200+ }
201+ #forced change
202+ thisdict ["year" ] = 2018
203+
204+ #update
205+ thisdict .update ({"color" : "red" })
206+
207+ #pop-delete
208+ thisdict .pop ("model" )
209+
210+ print (thisdict )
211+ #{'brand': 'Ford', 'year': 2018, 'color': 'red'}
212+
213+ print (thisdict ["brand" ])
214+ #Ford
215+
216+ print (len (thisdict ))
217+ #3
218+
219+
220+
221+ #LOOP
222+ #IF-Else
223+ a = 200
224+ b = 33
225+ if b > a :
226+ print ("b is greater than a" )
227+ elif a == b :
228+ print ("a and b are equal" )
229+ else :
230+ print ("a is greater than b" )
231+ #a is greater than b
232+
233+
234+ #While
235+ i = 4
236+ while i < 6 :
237+ print (i )
238+ i += 1
239+ #4
240+ #5
241+
242+ #continue
243+ fruits = ["apple" , "banana" , "cherry" ]
244+ for x in fruits :
245+ if x == "banana" :
246+ continue
247+ print (x )
248+ #apple
249+ #cherry
250+
251+ #range
252+ for x in range (2 ):
253+ print (x )
254+ #0
255+ #1
256+
257+ #nested Loop
258+ x = ["1" , "2" , "3" ]
259+ y = ["4" , "5" , "6" ]
260+ for a in x :
261+ for b in y :
262+ print (a ,b )
263+
264+
265+
266+ #Functions
267+ def my_function (fname , lname ):
268+ print (fname + "-" + lname )
269+
270+ my_function ("Emil" , "Refsnes" )
271+ #Emil-Refsnes
272+
273+ def my_function (c1 ,c2 ,c3 ):
274+ print ("Lowest number is-" + c1 )
275+ print ("followed by-" + c2 + c3 )
276+ my_function (c1 = "2" , c2 = "3" , c3 = "5" )
277+ #Lowest number is-2
278+ #followed by-35
279+
280+ #lambda
281+ x = lambda a : a + 10
282+ print (x (5 ))
283+ #15
284+
285+ def myfunc (n ):
286+ return lambda a : a * n
287+ mydoubler = myfunc (2 )
288+ print (mydoubler (11 ))
289+ #22
290+
291+
292+
293+ #Classes & Objects
294+ class Person :
295+ def __init__ (self , name , age ):
296+ self .name = name
297+ self .age = age
298+
299+ p1 = Person ("John" , 36 )
300+ #modify object
301+ p1 .age = 40
302+ print (p1 .age )
303+ #40
304+
305+ #delete object
306+ del p1 .name
307+ #error!
308+
309+
310+
311+
312+ #Inheritance
313+ class Person :
314+ def __init__ (self , fname , lname ):
315+ self .firstname = fname
316+ self .lastname = lname
317+
318+ def printname (self ):
319+ print (self .firstname , self .lastname )
320+
321+ #Use the Person class to create an object, and then execute the printname method:
322+
323+ x = Person ("John" , "Doe" )
324+ x .printname ()
0 commit comments