1+ ########################## List(Array) ##########################
2+
3+ # empty list
4+ my_list = []
5+
6+ # list of integers
7+ my_list = [1 , 2 , 3 , 4 , 5 ]
8+
9+ # list with mixed datatypes
10+ my_list = [1 , "Hello" , 3.4 , 2 , 'World' ]
11+
12+ # nested list
13+ my_list = ["mouse" , [8 , 4 , 6 ], ['a' ], ['x' , 'y' , 'z' ]]
14+
15+
16+ # Index if list
17+ my_list = ['H' , 'L' , 'L' , 'O' ]
18+ print (my_list [0 ])
19+ # Output : H
20+ print (my_list [3 ])
21+ # Output: O
22+ print (my_list [- 1 ])
23+ # Output: O
24+ print (my_list [- 0 ])
25+ # Output: H
26+ print (my_list [- 4 ])
27+ # Output: H
28+
29+
30+ # Nested List
31+ n_list = ["Happy" , [2 ,0 ,1 ,5 ]]
32+
33+ # Nested indexing
34+
35+ print (n_list [0 ][1 ])
36+ # Output: a
37+
38+ print (n_list [1 ][3 ])
39+ # Output: 5
40+
41+ num_list = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
42+ print (num_list [0 :3 ])
43+ # Output : [1, 2, 3]
44+ print (num_list [2 :])
45+ # Output : [3, 4, 5, 6, 7, 8, 9]
46+ print (num_list [:3 ])
47+ # Output: [1, 2, 3]
48+ print (num_list [:- 1 ])
49+ # Output: [1, 2, 3, 4, 5, 6, 7, 8]
50+
51+ # Inverse the list
52+ print (num_list [::- 1 ])
53+ # Output : [9, 8, 7, 6, 5, 4, 3, 2, 1]
54+ print (* reversed (num_list )) # 9 8 7 6 5 4 3 2 1
55+
56+ ############ How to store value into the list
57+ my_list = [] # declare the empty list
58+ n = int (input ('Enter the size of list : ' ))
59+ for i in range (n ):
60+ a = int (input ())
61+ my_list .append (a )
62+
63+ print (my_list )
64+ ''' Input and Output:
65+ Enter the size of list : 5
66+ 1
67+ 2
68+ 3
69+ 4
70+ 5
71+ [1, 2, 3, 4, 5]
72+ '''
73+ #You can store value this way:
74+ my_list = list (map (int , input ().split ())) # after new line value store will be ended
75+ print (my_list )
76+ '''
77+ Input:
78+ 7 8 9 6 5 4
79+
80+ Output:
81+ [7, 8, 9, 6, 5, 4]
82+ '''
83+
84+
85+ my_list = [5 , 3 , 4 , 1 , 2 ]
86+ # sort the list
87+ my_list .sort ()
88+ print (my_list ) # [1, 2, 3, 4, 5]
89+
90+ my_list = [3 , 4 , 2 , 5 , 1 ]
91+ my_list .remove (4 )
92+ print (my_list ) # [3, 2, 5, 1]
93+
94+
95+ ##Insert an element at specific index in a list and return updated list
96+ a = [1 , 2 , 4 ]
97+ a .insert (2 , 3 ) # insert 3 in index 2
98+ b = a [:]
99+ print (b ) # [1, 2, 3, 4]
100+
101+ ### or
102+ a = [1 , 2 , 4 ]
103+ b = a [:2 ] + [3 ] + a [2 :]
104+ print (b ) # [1, 2, 3, 4]
105+
106+ ''' This note has taken from this side : https://www.programiz.com/python-programming/list
107+
108+ append() - Add an element to the end of the list
109+ extend() - Add all elements of a list to the another list
110+ insert() - Insert an item at the defined index
111+ remove() - Removes an item from the list
112+ pop() - Removes and returns an element at the given index
113+ clear() - Removes all items from the list
114+ index() - Returns the index of the first matched item
115+ count() - Returns the count of number of items passed as an argument
116+ sort() - Sort items in a list in ascending order
117+ reverse() - Reverse the order of items in the list
118+ copy() - Returns a shallow copy of the list
119+
120+ '''
121+
122+ list_arr = [1 , 7 , 4 , 5 , 9 , 6 ]
123+ list_arr .extend ([33 , 777 ])
124+ print (list_arr )
125+
126+
127+ list_arr = [1 , 7 , 4 , 5 , 9 , 6 ]
128+ list_arr .pop ()
129+ print (list_arr ) # [1, 7, 4, 5, 9]
130+ list_arr .pop (1 )
131+ print (list_arr ) # [1, 4, 5, 9]
132+
133+ list_arr = [1 , 7 , 4 , 5 , 9 , 6 ]
134+ list_arr .clear ()
135+ print (list_arr ) # []
136+
137+
138+ list_arr = [1 , 7 , 4 , 5 , 9 , 6 ]
139+ idx = list_arr .index (9 )
140+ print (idx ) # 4
141+
142+
143+ list_arr = [1 ,1 ,1 ,2 ,3 ,2 ,5 ]
144+ print (list_arr .count (1 )) # 3
145+
146+
147+ list_arr = [1 , 7 , 4 , 5 , 9 , 6 ]
148+ copy_list_arr = list_arr .copy ()
149+ print (copy_list_arr ) # [1, 7, 4, 5, 9, 6]
150+
151+
152+ ## Find largest number from the list
153+ list_arr = [1 ,3 ,4 ,8 ,9 ,77 ]
154+ largest = max (list_arr )
155+ print (largest ) # 77
156+
157+
158+ ## Find small number from the list
159+ list_arr = [1 ,3 ,4 ,8 ,9 ,77 ]
160+ small = min (list_arr )
161+ print (small ) # 1
162+
163+
164+ # delete entire list
165+ #del my_list
166+
167+ # NameError: name 'my_list' is not defined
168+ #print(my_list)
169+
170+
171+ ########################### String ###############################
172+ '''
173+ H e l l o
174+ 0 1 2 3 4
175+ -5 -4 -3 -2 -1
176+
177+ '''
178+
179+ # all of the following are equivalent
180+ my_string = 'Hello'
181+ print (my_string )
182+
183+ my_string = "Hello"
184+ print (my_string )
185+
186+ my_string = '''Hello'''
187+ print (my_string )
188+
189+ # triple quotes string can extend multiple lines
190+ my_string = """Hello, welcome to
191+ the world of Python"""
192+ print (my_string )
193+
194+ '''
195+ ### Output :
196+ Hello
197+ Hello
198+ Hello
199+ Hello, welcome to
200+ the world of Python
201+ '''
202+
203+
204+ my_string = "Hello"
205+ print (my_string [0 ]) # H
206+
207+ my_string = "Hello "
208+ my_string1 = "World!"
209+ string = my_string + my_string1
210+ print (string ) # Hello World!
211+
212+
213+ my_string = "I Love You"
214+ if 'I' in my_string :
215+ print ("Ok" ) # Ok
216+ else :
217+ print ("Not Ok!" )
218+
219+ my_string = "I Love You"
220+ if 'Love' in my_string :
221+ print ("Ok" ) # Ok
222+ else :
223+ print ("Not Ok!" )
224+
225+
226+ # Length of a string
227+ my_string = "Rezwan"
228+ print (len (my_string )) # 6
229+
230+
231+ print ('{0}, {1}, {2}' .format ("Rezwanul" , "Haque" , "Rezwan" )) # Rezwanul, Haque, Rezwan
232+
233+ print ('{}, {}, {}' .format ("Rezwanul" , "Haque" , "Rezwan" )) # Rezwanul, Haque, Rezwan
234+
235+ my_string = "I Love You"
236+ STr = my_string .replace ("Love" ,"Hate" )
237+ print (STr ) # I Hate You
238+
239+ ### Does Python have a string 'contains' substring method? (https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method)
240+ s = "This be a string"
241+ if s .find ("is" ) == - 1 :
242+ print ( "No 'is' here!" )
243+ else :
244+ print ("Found 'is' in the string." ) # Found 'is' in the string.
245+
246+
247+ '''Important Link:
248+ (1) Is there a way to substring a string in Python? [ https://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python ]
249+ (2) Generator Expressions vs. List Comprehension [ https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehension ]
250+
251+ '''
252+
253+
254+ '''
255+ ** You are now able to solve following online-judge problems.
256+ ------------------------------------------------------------
257+ (1) CodeForces | A. Straight «A»
258+ (2) Timus | 1000. A+B Problem
259+ (3) URI | 1026 - To Carry or not to Carry
260+ (4) HackerRank | String Split and Join
261+ (5) Spoj | ADDREV - Adding Reversed Numbers
262+ (6) CodeChef | Devu and an Array
263+ (7) HackerRank | Find a string
264+ (8) UVa | 11799 - Horror Dash
265+ (9) UVa | 10370 - Above Average
266+ (10) AtCoder | D - Walk and Teleport
267+ '''
0 commit comments