chrisluiz16

Python day 5

Jul 21st, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. ## List of list or nested list
  2.  
  3. persons = [["sam",45],["kevin",30],["john",20],["prince",25]]
  4. for item in persons:
  5. name = item[0]
  6. age = item[1]
  7. print(name, age)
  8.  
  9. ### Unpacking second method
  10.  
  11. persons = ["sam",45]
  12. name,age = persons
  13. print(persons)
  14.  
  15. ### Unpacking Third method
  16.  
  17. s1 = ["sam",25,"[email protected]"]
  18. name,age,mail = s1
  19. print(mail)
  20.  
  21. ### Unpacking fourth method
  22.  
  23. s1 = ["sam",25,"[email protected]","AB+","9249282390"]
  24. name,age,mail,*_ = s1
  25. print(s1)
  26.  
  27. ### Unpacking 5 method
  28.  
  29. s1 = ["sam",25,"[email protected]","AB+","9249282390"]
  30. *_,mobile = s1
  31. print(mobile)
  32.  
  33. persons = [["sam",20,"[email protected]"],["john", 28, "[email protected]"],["prince",20,"[email protected]"]]
  34. search = input("Enter the person u want to search : ")
  35. for item in persons:
  36. name = item[0]
  37. age = item[1]
  38. if name == search:
  39. print(name, "age is : ", age)
  40.  
  41.  
  42. ## Break
  43.  
  44. persons = [["sam",20,"[email protected]"],["john", 28, "[email protected]"],["prince",20,"[email protected]"]]
  45. search = input("Enter the name of the person : ")
  46. for item in persons:
  47. name = item[0]
  48. age = item[1]
  49. mail = item[2]
  50. if name == search:
  51. print("The", name,"details are", "Age:",age, "Maild_id: ", mail)
  52. else:
  53. print("Sorry no details fount")
  54. break
  55.  
  56.  
  57. number = ["79345","4567","2345","8065","3218"]
  58. for item in number:
  59. for x in item:
  60. if int(x) % 2 == 0:
  61. print(x)
  62. break
  63.  
  64.  
  65. ## Dictionary data type
  66.  
  67. persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
  68. x = len(persons)
  69. print(x)
  70.  
  71. persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
  72. print(persons["kevin"])
  73.  
  74. persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
  75. for item in persons:
  76. age = persons[item]
  77. print(item, age)
  78.  
  79. persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
  80. print("sam" in persons)
  81.  
  82. persons = {"sam": 45, "kevin": 30, "prince": 20, "john": 25, "jobin": 30 , "colombus": 30}
  83. search = input("Enter the name of the employee to search the age: ")
  84. if search in persons:
  85. print(persons[search])
  86. else:
  87. prinT("Sorry no match found")
  88.  
  89.  
  90. ## Empty dit
  91.  
  92. students = {}
  93. students["sam"] = "25000"
  94. students["john"] = "30000"
  95. students["kevin"] = "5000"
  96. print(students)
  97.  
  98. ## Value as list
  99.  
  100. persons = {"sam": [45,4500], "john": [50,50000], "prince": [60,6000]}
  101. salary = persons['sam'][1]
  102. print(salary)
  103.  
  104. ## Value as dict
  105.  
  106. persons = {"sam": {"age": 45, "salary": 45000}, "john": {"age": 50 , "salary": 5000}, "prince": {"age": 60,"salary": 6000}}
  107. print(persons['john']['salary'])
  108.  
  109.  
  110. ##------------------------------------- Task 2 -----------------------------------##
  111.  
  112. ## 1] find the biggest number from the list.
  113.  
  114. l = [1, 4, 2, 9, 7]
  115. y = 0
  116.  
  117. for item in l:
  118. x = int(item)
  119. if x > y:
  120. y = x
  121. print(y)
  122.  
  123.  
  124. ## 2] find the smallest number from the list.
  125.  
  126. l = [1, 4, 2, 9, 7]
  127. y = 1
  128.  
  129. for item in l:
  130. x = int(item)
  131. if x <= y:
  132. y = x
  133. print(y)
  134.  
  135.  
  136. ## 3] find the sum of salaries.
  137.  
  138.  
  139. persons = [ ["john",2400] , ["sam",2500] , ["teena",2210] , ["kevin",3400] , ["devon",1200] ]
  140. y = 0
  141. for item in persons:
  142. x = int(item[1])
  143. y = y + x
  144. print(y)
  145.  
  146.  
  147.  
  148. ## 4] add the names to employees list.
  149.  
  150. persons = [ ["john",20] , ["sam",25] , ["teena",22] , ["kevin",34] , ["devon",32] ]
  151.  
  152. employees = []
  153.  
  154. for item in persons:
  155. name = item[0]
  156. employees.append(name)
  157. print(employees)
  158.  
  159. ## 5] print all the words Starts With vowel chars "aeiou" append all word to mylist, avoid duplicate.
  160.  
  161. s = """The OS includes some common elastic core components, like the GNU tools,
  162. among others. These kick tools give the user a way to manage the resources
  163. provided by the kernel, Kernel apple install additional axe knee software, configure performance
  164. and security Knife settings, and more. eye keep All of odd these tools bundled together
  165. make up the knee umbrella keen axe functional kick operating system. Because Linux is an open source
  166. OS, combinations of kick elastic software enrich can vary between Linux distributions."""
  167.  
  168. mylist = []
  169.  
  170. s = s.replace('.','')
  171. s = s.lower().replace(',','')
  172. s = s.split()
  173. for item in s:
  174. if item[0] in 'a,e,i,o,u':
  175. if item not in mylist:
  176. mylist.append(item)
  177.  
  178. print(mylist)
  179.  
  180. ## 6] add salary to salary list, salary list must be empty.
  181.  
  182. mydict = { "sam":50 , "alex":48 , "kevin":30 , "john":35 , "jane": 25 }
  183.  
  184. salary = []
  185.  
  186. for item in mydict:
  187. x = int(mydict[item])
  188. salary.append(x)
  189. print(salary)
  190.  
  191. ## 7] find the sum of salaries.
  192.  
  193. mydict = { "sam":50 , "alex":48 , "kevin":30 , "john":35 , "jane": 25 }
  194. y = 0
  195. for item in mydict:
  196. x = int(mydict[item])
  197. y = y + x
  198. print(y)
  199.  
  200.  
  201. ## 8 ] find the sum of salaries
  202.  
  203. mydict = { "sam":[50,5000] , "alex":[48,4800] , "kevin":[30,3000] , "john":[35,3500] , "jane":[25,2500] }
  204. y = 0
  205.  
  206. for item in mydict:
  207. salary = mydict[item][1]
  208. y = y + salary
  209. print(y)
  210.  
  211.  
  212. ## 9] create a list of list from mydict
  213.  
  214. mydict = { "sam":[50,5000] , "alex":[48,4800] , "kevin":[30,3000] , "john":[35,3500] , "jane":[25,2500] }
  215.  
  216. ll = []
  217.  
  218. for item in mydict:
  219. salary = mydict[item][1]
  220. ll.append(item)
  221. ll.append(salary)
  222. print(ll)
  223.  
  224. ## 10] find the total salary
  225.  
  226. mydict = {
  227. "sam":{ "age":50,"salary":5000},
  228. "alex":{ "age":48,"salary":4800},
  229. "kevin":{ "age":30,"salary":3000}
  230. }
  231.  
  232. y = 0
  233. for item in mydict:
  234. x = int(mydict[item]['salary'])
  235. y = y + x
  236. print(y)
  237.  
  238.  
Advertisement
Add Comment
Please, Sign In to add comment