Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## List of list or nested list
- persons = [["sam",45],["kevin",30],["john",20],["prince",25]]
- for item in persons:
- name = item[0]
- age = item[1]
- print(name, age)
- ### Unpacking second method
- persons = ["sam",45]
- name,age = persons
- print(persons)
- ### Unpacking Third method
- s1 = ["sam",25,"[email protected]"]
- name,age,mail = s1
- print(mail)
- ### Unpacking fourth method
- s1 = ["sam",25,"[email protected]","AB+","9249282390"]
- name,age,mail,*_ = s1
- print(s1)
- ### Unpacking 5 method
- s1 = ["sam",25,"[email protected]","AB+","9249282390"]
- *_,mobile = s1
- print(mobile)
- persons = [["sam",20,"[email protected]"],["john", 28, "[email protected]"],["prince",20,"[email protected]"]]
- search = input("Enter the person u want to search : ")
- for item in persons:
- name = item[0]
- age = item[1]
- if name == search:
- print(name, "age is : ", age)
- ## Break
- persons = [["sam",20,"[email protected]"],["john", 28, "[email protected]"],["prince",20,"[email protected]"]]
- search = input("Enter the name of the person : ")
- for item in persons:
- name = item[0]
- age = item[1]
- mail = item[2]
- if name == search:
- print("The", name,"details are", "Age:",age, "Maild_id: ", mail)
- else:
- print("Sorry no details fount")
- break
- number = ["79345","4567","2345","8065","3218"]
- for item in number:
- for x in item:
- if int(x) % 2 == 0:
- print(x)
- break
- ## Dictionary data type
- persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
- x = len(persons)
- print(x)
- persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
- print(persons["kevin"])
- persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
- for item in persons:
- age = persons[item]
- print(item, age)
- persons = {"sam": 45, "kevin": 30, "prince": 20, "john":25}
- print("sam" in persons)
- persons = {"sam": 45, "kevin": 30, "prince": 20, "john": 25, "jobin": 30 , "colombus": 30}
- search = input("Enter the name of the employee to search the age: ")
- if search in persons:
- print(persons[search])
- else:
- prinT("Sorry no match found")
- ## Empty dit
- students = {}
- students["sam"] = "25000"
- students["john"] = "30000"
- students["kevin"] = "5000"
- print(students)
- ## Value as list
- persons = {"sam": [45,4500], "john": [50,50000], "prince": [60,6000]}
- salary = persons['sam'][1]
- print(salary)
- ## Value as dict
- persons = {"sam": {"age": 45, "salary": 45000}, "john": {"age": 50 , "salary": 5000}, "prince": {"age": 60,"salary": 6000}}
- print(persons['john']['salary'])
- ##------------------------------------- Task 2 -----------------------------------##
- ## 1] find the biggest number from the list.
- l = [1, 4, 2, 9, 7]
- y = 0
- for item in l:
- x = int(item)
- if x > y:
- y = x
- print(y)
- ## 2] find the smallest number from the list.
- l = [1, 4, 2, 9, 7]
- y = 1
- for item in l:
- x = int(item)
- if x <= y:
- y = x
- print(y)
- ## 3] find the sum of salaries.
- persons = [ ["john",2400] , ["sam",2500] , ["teena",2210] , ["kevin",3400] , ["devon",1200] ]
- y = 0
- for item in persons:
- x = int(item[1])
- y = y + x
- print(y)
- ## 4] add the names to employees list.
- persons = [ ["john",20] , ["sam",25] , ["teena",22] , ["kevin",34] , ["devon",32] ]
- employees = []
- for item in persons:
- name = item[0]
- employees.append(name)
- print(employees)
- ## 5] print all the words Starts With vowel chars "aeiou" append all word to mylist, avoid duplicate.
- s = """The OS includes some common elastic core components, like the GNU tools,
- among others. These kick tools give the user a way to manage the resources
- provided by the kernel, Kernel apple install additional axe knee software, configure performance
- and security Knife settings, and more. eye keep All of odd these tools bundled together
- make up the knee umbrella keen axe functional kick operating system. Because Linux is an open source
- OS, combinations of kick elastic software enrich can vary between Linux distributions."""
- mylist = []
- s = s.replace('.','')
- s = s.lower().replace(',','')
- s = s.split()
- for item in s:
- if item[0] in 'a,e,i,o,u':
- if item not in mylist:
- mylist.append(item)
- print(mylist)
- ## 6] add salary to salary list, salary list must be empty.
- mydict = { "sam":50 , "alex":48 , "kevin":30 , "john":35 , "jane": 25 }
- salary = []
- for item in mydict:
- x = int(mydict[item])
- salary.append(x)
- print(salary)
- ## 7] find the sum of salaries.
- mydict = { "sam":50 , "alex":48 , "kevin":30 , "john":35 , "jane": 25 }
- y = 0
- for item in mydict:
- x = int(mydict[item])
- y = y + x
- print(y)
- ## 8 ] find the sum of salaries
- mydict = { "sam":[50,5000] , "alex":[48,4800] , "kevin":[30,3000] , "john":[35,3500] , "jane":[25,2500] }
- y = 0
- for item in mydict:
- salary = mydict[item][1]
- y = y + salary
- print(y)
- ## 9] create a list of list from mydict
- mydict = { "sam":[50,5000] , "alex":[48,4800] , "kevin":[30,3000] , "john":[35,3500] , "jane":[25,2500] }
- ll = []
- for item in mydict:
- salary = mydict[item][1]
- ll.append(item)
- ll.append(salary)
- print(ll)
- ## 10] find the total salary
- mydict = {
- "sam":{ "age":50,"salary":5000},
- "alex":{ "age":48,"salary":4800},
- "kevin":{ "age":30,"salary":3000}
- }
- y = 0
- for item in mydict:
- x = int(mydict[item]['salary'])
- y = y + x
- print(y)
Advertisement
Add Comment
Please, Sign In to add comment