-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-Python List,Dictionary,Sets etc.py
More file actions
301 lines (256 loc) · 7.44 KB
/
6-Python List,Dictionary,Sets etc.py
File metadata and controls
301 lines (256 loc) · 7.44 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
# ===========================================
# Python Data Structures and Boolean
# ===========================================
# Topics:
# - Boolean
# - Boolean and Logical Operators
# - Lists
# - Comparison operators
# - Dictionaries
# - Tuples
# - Sets
# ===========================================
# Boolean Variables
# ===========================================
# Boolean values are the two constant objects False and True.
# They represent truth values and behave like integers 0 and 1.
print(True, False) #True False
print(type(True)) #Bool
print(type(False)) #Bool
my_str = 'Rupesh123'
print("Is title case:", my_str.istitle()) #True
print(my_str.isalnum()) # check if all chars are numbers/alphabetic
print(my_str.isalpha()) # check if all chars are alphabetic
print(my_str.isdigit()) # test if string contains only digits
print(my_str.istitle()) # test if string contains title case
print(my_str.isupper()) # test if string contains upper case
print(my_str.islower()) # test if string contains lower case
print(my_str.isspace()) # test if string contains spaces
print(my_str.endswith('k')) # test if string ends with 'k'
print(my_str.startswith('R')) # test if string starts with 'R'
True
False
False
True
False
False
False
False
True
# ===========================================
# Boolean and Logical Operators
# ===========================================
print(True and True) #True
print(True and False) #False
print(True or False) #True
print(True or True) #True
str_example = 'Hello World'
my_str = 'Rupesh'
# Note: str_example.isnum() should be str_example.isalnum()
print(my_str.isalpha() or str_example.isalnum())
True
# ===========================================
# Lists
#A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.Each element or value that is inside of a list is called an item.
#Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ]
# ===========================================
# Mutable vs Immutable
str1 = "Rupesh"
print(str1) #Rupesh
print(type(str1)) #str
str1 = "Sharma"
print(str1) #Sharma
# Strings are immutable
# str1[2] = "jk" # This would cause a TypeError
# Lists are mutable
lst = [1, 2, 3, 4, "Rupesh", "Hello"]
print(lst) # 1, 2, 3, 4, "Rupesh", "Hello"
lst[4] = "Sharma"
print(lst[4]) #Sharma
lst = list((1, 2, 3, 4, 5))
print(lst) #[1, 2, 3, 4, 5]
print(type(lst)) #list
for i in lst:
print(i ** 2)
1
4
9
16
25
print("Minimum value:", min(lst)) #1
print(type([]))
lst_example = []
print(type(lst_example))#List
lst = list()
print(type(lst))#List
lst = ['Mathematics', 'chemistry', 100, 200, 300, 204]
print(len(lst)) #6
print(type(lst)) #List
# ===========================================
# Append Method
# ===========================================
lst = [1, 2, 3, 4, 5]
lst.append("Rupesh")
print(lst)
#[1, 2, 3, 4, 5,"Rupesh"]
lst.append(["John", "Bala"])
print(lst)
[1, 2, 3, 4, 5,"Rupesh",["John", "Bala"]]
print(lst[2:6])
# [3, 4, 5,"Rupesh",["John", "Bala"]]
# ===========================================
# Insert Method
# ===========================================
lst.insert(2, "Sharma")
print(lst) #[1, 2, "Sharma", 4, 5,"Rupesh",["John", "Bala"]]
# ===========================================
# Extend Method
# ===========================================
lst = [1, 2, 3, 4, 5, 6]
lst.append([8, 9])
print(lst) #[1, 2, 3, 4, 5, 6,[8, 9]]
lst = [1, 2, 3, 4, 5, 6]
lst.extend([8, 9])
print(lst) # [1, 2, 3, 4, 5, 6,8,9]
# ===========================================
# List Operations
# ===========================================
lst = [1, 2, 3, 4, 5]
print("Sum:", sum(lst)) #15
print("Repetition:", lst * 5)
#[1, 2, 3, 4, 5, 1, 2, 3, 4, 5,1, 2, 3, 4, 5,1, 2, 3, 4, 5,1, 2, 3, 4, 5]
for i in lst:
print(i / 5)
0.2
0.4
0.6
0.8
1.0
# ===========================================
# Pop Method
# ===========================================
print(lst.pop()) # removes last element
print(lst)
print(lst.pop(2)) # removes element at index 2
print(lst)
# ===========================================
# Count, Length, Index, Min, Max
# ===========================================
lst = [1, 1, 2, 3, 4, 5]
print("Count of 1:", lst.count(1))
print("Length:", len(lst))
print("Index of 1 between 1 and 4:", lst.index(1, 1, 4))
print("Min:", min(lst))
print("Max:", max(lst))
# ===========================================
SETS
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python's set class represents the mathematical notion of a set.This is based on a data structure known as a hash table
# ===========================================
## Defining an empy set
set_var= set()
print(set_var)
print(type(set_var))
set()
<class 'set'>
set_var={1,2,3,4,3}
set_var
{1, 2, 3, 4}
set_var={"Avengers","IronMan",'Hitman'}
print(set_var)
type(set_var)
{'IronMan', 'Hitman', 'Avengers'}
set
## Inbuilt function in sets
set_var.add("Hulk")
.
print(set_var)
{'IronMan', 'Hitman', 'Avengers', 'Hulk'}
set1={"Avengers","IronMan",'Hitman'}
set2={"Avengers","IronMan",'Hitman','Hulk2'}
set2.intersection_update(set1)
set2
{'Avengers', 'Hitman', 'IronMan'}
##Difference
set2.difference(set1)
{'Hulk2'}
set2
{'Avengers', 'Hitman', 'Hulk2', 'IronMan'}
## Difference update
set2.difference_update(set1)
print(set2)
{'Hulk2'}
-------------------------------------------------------------------Dictionaries---------------------------------------------------------------------------
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.
dic={}
type(dic)
dict
type(dict())
dict
set_ex={1,2,3,4,5}
type(set_ex)
set
## Let create a dictionary
my_dict={"Car1": "Audi", "Car2":"BMW","Car3":"Mercidies Benz"}
type(my_dict)
dict
##Access the item values based on keys
my_dict['Car1']
'Audi'
# We can even loop throught the dictionaries keys
for x in my_dict:
print(x)
Car1
Car2
Car3
# We can even loop throught the dictionaries values
for x in my_dict.values():
print(x)
Audi
BMW
Mercidies Benz
# We can also check both keys and values
for x in my_dict.items():
print(x)
('Car1', 'Audi')
('Car2', 'BMW')
('Car3', 'Mercidies Benz')
## Adding items in Dictionaries
my_dict['car4']='Audi 2.0'
my_dict
{'Car1': 'Audi', 'Car2': 'BMW', 'Car3': 'Mercidies Benz', 'car4': 'Audi 2.0'}
my_dict['Car1']='MAruti'
my_dict
{'Car1': 'MAruti', 'Car2': 'BMW', 'Car3': 'Mercidies Benz', 'car4': 'Audi 2.0'}
-----------------------------------------------------------------------Nested Dictionary--------------------------------------------------------------------------
car1_model={'Mercedes':1960}
car2_model={'Audi':1970}
car3_model={'Ambassador':1980}
car_type={'car1':car1_model,'car2':car2_model,'car3':car3_model}
print(car_type)
{'car1': {'Mercedes': 1960}, 'car2': {'Audi': 1970}, 'car3': {'Ambassador': 1980}}
## Accessing the items in the dictionary
print(car_type['car1'])
{'Mercedes': 1960}
print(car_type['car1']['Mercedes'])
1960
------------------------------------------------------------------------------------Tuples---------------------------------------------------------------------------------------------
## create an empty Tuples
my_tuple=tuple()
type(my_tuple)
tuple
my_tuple=()
type(my_tuple)
tuple
my_tuple=("Krish","Ankur","John")
my_tuple=('Hello','World')
print(type(my_tuple))
print(my_tuple)
<class 'tuple'>
('Hello', 'World')
type(my_tuple)
tuple
## Inbuilt function
my_tuple.count('Krish')
1
my_tuple.index('Ankur')
1