└── DATA TYPES.py /DATA TYPES.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # List Data Structure 5 | 6 | # In[25]: 7 | 8 | 9 | # COUNT : is used to show how many time a number or and integer is repeated in a list 10 | 11 | integer = [11, 12 ,23, 34 ,34, 12,24, 10, 1, 34, 2 ,4 ,6] 12 | string = ['Pakistan', 'India', 'Afghistan', 'Bangladesh', 'Iran', 'Nepal'] 13 | x = integer.count(34) 14 | x 15 | y = string.count('India') 16 | y 17 | print(x," ", y) 18 | 19 | 20 | # In[26]: 21 | 22 | 23 | # APPEND : us to add element at the last of list 24 | 25 | integer.append(80) 26 | string.append('Australia') 27 | 28 | print(integer, " ", string) 29 | print() 30 | 31 | # we can also append to list together 32 | 33 | integer.append(string) 34 | print(integer) 35 | 36 | 37 | # In[27]: 38 | 39 | 40 | # POP : use to removes an item at the specified index from the list. 41 | 42 | a = integer.pop(-1) 43 | b = string.pop(2) 44 | 45 | print(integer ," ",string ) 46 | print() 47 | print(a," ",b) 48 | 49 | 50 | # In[28]: 51 | 52 | 53 | # Del : The del statement can be used to delete an entire list or specific item from the list 54 | 55 | del(integer[3]) 56 | del(string[3]) 57 | 58 | print(integer, ' ' ,string ) 59 | 60 | 61 | # In[29]: 62 | 63 | 64 | # Remove : to remove any item from the list directly put the value of the item 65 | 66 | integer.remove(12) 67 | string.remove('India') 68 | 69 | print(integer, " ", string) 70 | 71 | 72 | # In[30]: 73 | 74 | 75 | # Insert : we can insert any item at any postiion while the previous item moves to the right 76 | 77 | integer.insert(5, 25) 78 | string.insert(2, 'America') 79 | 80 | print(integer, ' ', string) 81 | 82 | 83 | # In[31]: 84 | 85 | 86 | # Miximum : maximum value 87 | max(integer), min(integer) 88 | 89 | 90 | # In[32]: 91 | 92 | 93 | # Sorting : arrange in a sequence 94 | 95 | integer.sort() 96 | print(integer) 97 | 98 | # Rreverse sort 99 | 100 | integer.reverse() 101 | integer 102 | 103 | 104 | # In[33]: 105 | 106 | 107 | string.sort() 108 | print(string) 109 | 110 | string.reverse() 111 | string 112 | 113 | 114 | # In[38]: 115 | 116 | 117 | # # extend : combine or concatnate two list together 118 | x = [1, 2, 3, 4, 5] 119 | y = ['red', 'green', 'yellow'] 120 | 121 | x.extend(y) 122 | 123 | print(x) 124 | 125 | 126 | # In[47]: 127 | 128 | 129 | # Index : get intex number of the specific value from the lsit 130 | 131 | x.index(2), y.index('red') 132 | 133 | 134 | # # List Iteration 135 | 136 | # In[63]: 137 | 138 | 139 | a = [1, 3, 5, 6, 7, 9 ,10] 140 | t= len(a) 141 | 142 | for n in range(t-1, -1, -1): 143 | print(a[n]) 144 | 145 | 146 | # In[70]: 147 | 148 | 149 | # 2nd method 150 | 151 | for n in (a): 152 | print(n) 153 | 154 | 155 | # In[75]: 156 | 157 | 158 | x = ['appel', 'orange', 'banana', 'pineapple'] 159 | s = len(x) 160 | 161 | for n in range(s): 162 | print(x[n]) 163 | 164 | 165 | # In[76]: 166 | 167 | 168 | for b in (x): 169 | print(b) 170 | 171 | 172 | # # List Comprehension 173 | 174 | # In[93]: 175 | 176 | 177 | a = ['stars'] 178 | for l in range(1, 20, 2): 179 | a.append(l) 180 | print(a) 181 | 182 | 183 | # In[88]: 184 | 185 | 186 | n = [c for c in range(1, 50)] 187 | print(n) 188 | 189 | 190 | # In[135]: 191 | 192 | 193 | # zip function : is used to iterate two lists at same time 194 | 195 | l = [1, 2, 4, 5, 6, 7] 196 | l1 = ['cow', 'ox', 'tiger', 'lion', 'monkey', 'fox'] 197 | 198 | for a, b in zip(l, l1): 199 | print(a, b) 200 | 201 | 202 | # In[138]: 203 | 204 | 205 | # 2nd method 206 | a = len(l) 207 | for n in range(a): 208 | print(l[n], l1[n]) 209 | 210 | 211 | # In[156]: 212 | 213 | 214 | # convert string to list 215 | b = input('Enter the value ') 216 | l =b.split(); 217 | print(l) 218 | 219 | 220 | # In[ ]: 221 | 222 | 223 | a = [] 224 | for n in range(4): 225 | c = input('Enter the value'+str(a)+':-') 226 | a.append(n) 227 | print(l) 228 | 229 | 230 | # # Tuple Data Structure 231 | 232 | # In[104]: 233 | 234 | 235 | a = (12, 23, 34, 23, 34, 1, 3, 5, 6) 236 | print(a, type(a)) 237 | 238 | 239 | # In[115]: 240 | 241 | 242 | min(a), max(a),' ', sorted(a),' ', sum(a),' ' a.index(12) 243 | 244 | 245 | # In[131]: 246 | 247 | 248 | tup = ('banana', 2,'cherry', 'mango', 5, 6) 249 | a = len(tup) 250 | 251 | for n in range(a): 252 | print(tup[n]) 253 | 254 | 255 | # # Set Data Structure 256 | 257 | # In[2]: 258 | 259 | 260 | st = {1, 2, 3 , 4, 5, 6} 261 | print(st, type(st)) 262 | 263 | 264 | # In[6]: 265 | 266 | 267 | # Add 268 | st.add(20) 269 | st 270 | 271 | 272 | # In[12]: 273 | 274 | 275 | # Remove 276 | st.remove(20) 277 | st 278 | 279 | 280 | # In[16]: 281 | 282 | 283 | # Discard 284 | st.discard(3) 285 | st 286 | 287 | 288 | # In[18]: 289 | 290 | 291 | # Union 292 | 293 | a = {1, 2, 3, 4, 5, 6} 294 | b = {2, 4, 6, 8, 10, 12} 295 | 296 | a.union(b) 297 | 298 | 299 | # In[19]: 300 | 301 | 302 | # Intersection 303 | 304 | x = {1, 2, 3, 4, 5, 6} 305 | y = {2, 4, 6, 8, 10, 12} 306 | 307 | a.intersection(b) 308 | 309 | 310 | # # Dictionary Data Type 311 | 312 | # In[36]: 313 | 314 | 315 | # unorder data type 316 | # key and values 317 | # {} 318 | 319 | d={ 320 | 321 | 'Name' : 'Ehtijad', 322 | 'Class' : 12, 323 | 'Age' : 18 324 | } 325 | 326 | print(d, type(d)) 327 | 328 | 329 | # In[37]: 330 | 331 | 332 | print(d["Class"]) 333 | 334 | 335 | # In[38]: 336 | 337 | 338 | a = d["Age"] 339 | print(a) 340 | 341 | 342 | # In[39]: 343 | 344 | 345 | b = d["Name"] 346 | b 347 | 348 | 349 | # In[40]: 350 | 351 | 352 | for i in d: 353 | print(i) 354 | 355 | 356 | # In[41]: 357 | 358 | 359 | for n in d: 360 | print(d[n]) 361 | 362 | 363 | # In[42]: 364 | 365 | 366 | # FUNCTION 367 | # GET 368 | d={ 369 | 'Name' : 'Ehtijad', 370 | 'Class' : 12, 371 | 'Age' : 18 372 | } 373 | r = d.get("Name"), d.get('Age') 374 | print(r) 375 | 376 | 377 | # In[43]: 378 | 379 | 380 | g=d.get("Class") 381 | print(g) 382 | 383 | 384 | # In[44]: 385 | 386 | 387 | # keys 388 | d={ 389 | 'Name' : 'Ehtijad', 390 | 'Class' : 12, 391 | 'Age' : 18 392 | } 393 | a= d.keys() 394 | a 395 | 396 | 397 | # In[45]: 398 | 399 | 400 | for n in d.keys(): 401 | print(n) 402 | 403 | 404 | # In[46]: 405 | 406 | 407 | # values 408 | d={ 409 | 'Name' : 'Ehtijad', 410 | 'Class' : 12, 411 | 'Age' : 18 412 | } 413 | for k in d.values(): 414 | print(k) 415 | 416 | 417 | # In[47]: 418 | 419 | 420 | # items: work on two parameters 421 | # 422 | print('Keys Values') 423 | print() 424 | d={ 425 | 'Name' : 'Ehtijad', 426 | 'Class' : 12, 427 | 'Age' : 18 428 | } 429 | for a, b in d.items(): 430 | print(a,' ',b) 431 | 432 | 433 | # In[48]: 434 | 435 | 436 | # del 437 | d={ 438 | 'Name' : 'Ehtijad', 439 | 'Class' : 12, 440 | 'Age' : 18 441 | } 442 | del d["Age"] 443 | print(d) 444 | 445 | 446 | # In[49]: 447 | 448 | 449 | d['Class'] = '10' 450 | d 451 | 452 | 453 | # In[50]: 454 | 455 | 456 | #pop 457 | d.pop('Class') 458 | print(d) 459 | 460 | 461 | # In[51]: 462 | 463 | 464 | # dict():keys become keys and value become value 465 | 466 | name = 'Ehtijad' 467 | age = 20 468 | d = {'Name': name, 'age': age} 469 | print(d) 470 | 471 | 472 | # In[52]: 473 | 474 | 475 | #update 476 | d={ 477 | 'Name' : 'Ehtijad', 478 | 'Class' : 12, 479 | 'Age' : 18 480 | } 481 | d.update({"Age" : "18years"}) 482 | print(d) 483 | 484 | 485 | # In[53]: 486 | 487 | 488 | #dict() 489 | 490 | d = {'name': 'ehtijad', 'age': 20} 491 | print(d) 492 | 493 | 494 | # In[54]: 495 | 496 | 497 | # PYTHON NESTED DICTIONARY 498 | 499 | school={ 500 | 'Kings':{"Students":2500, 'Boys' : 1500 , 'Girls' : 1000}, 501 | 502 | "Public":{"Students":10000,'Boys' : 6500 , 'Girls' : 3500}, 503 | 504 | "APSAC":{"Students":5000,'Boys' : 2500 , 'Girls' : 2500}, 505 | } 506 | print(school, type(school)) 507 | 508 | 509 | # In[55]: 510 | 511 | 512 | school.get('Kings') 513 | 514 | 515 | # In[56]: 516 | 517 | 518 | school.get('APSAC') 519 | 520 | 521 | # In[57]: 522 | 523 | 524 | school['Kings']['Boys'] 525 | 526 | 527 | # In[58]: 528 | 529 | 530 | school['APSAC']['Girls'] 531 | 532 | 533 | # In[59]: 534 | 535 | 536 | for Keys, Values in school.items(): 537 | print(Keys, Values) 538 | 539 | 540 | # In[60]: 541 | 542 | 543 | print('School Boys Girls') 544 | for Keys, Values in school.items(): 545 | print(Keys,' ', Values['Boys'],' ', Values['Girls']) 546 | 547 | 548 | # In[61]: 549 | 550 | 551 | school['Public']['Girls'] = 4000 552 | school 553 | 554 | --------------------------------------------------------------------------------