31
loading...
This website collects cookies to deliver better user experience
>>> lst = []
>>> for i in range(1,11):
... lst.append(i**2)
...
>>> lst
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> lst = [i**2 for i in range(1,11)]
>>> lst
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> students = {'ram':44,'shyam':22,'harry':75,'john':54,'robert':25,'seta':65,'max':23,'harry':75,'robin':24,'philip':56}
>>> selected_students = []
>>> for student,marks in students.items():
... if marks >= 40:
... selected_students.append(student)
...
>>> selected_students
['ram', 'harry', 'john', 'seta', 'philip']
>>>
>>> students = {'ram':44,'shyam':22,'harry':75,'john':54,'robert':25,'seta':65,'max':23,'harry':75,'robin':24,'philip':56}
>>> selected_students = [student for student,marks in students.items() if marks >= 40]
>>> selected_students
['ram', 'harry', 'john', 'seta', 'philip']
>>> for i in range(3):
...
... # Append an empty sublist inside the list
... matrix.append([])
...
... for j in range(5):
... matrix[i].append(j)
...
>>> matrix
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> matrix = [[i for i in range(5)] for _ in range(3)]
>>> matrix
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>>
>>> fruits = ["apple","pineapple","banana","orange","papaya","strawberry","grapes","watermalon"]
>>> calories = [100,141,65,43,120,80,110,200]
>>> fruit_dict = {}
>>> for i in range(len(fruits)):
... fruit_dict[fruits[i]] = calories[i]
...
>>> fruit_dict
{'apple': 100, 'pineapple': 141, 'banana': 65, 'orange': 43, 'papaya': 120, 'strawberry': 80, 'grapes': 110, 'watermalon': 200}
>>> fruits = ["apple","pineapple","banana","orange","papaya","strawberry","grapes","watermalon"]
>>> calories = [100,141,65,43,120,80,110,200]
>>> fruits_dict = { fruits[i]:calories[i] for i in range(len(fruits))}
>>> fruits_dict
{'apple': 100, 'pineapple': 141, 'banana': 65, 'orange': 43, 'papaya': 120, 'strawberry': 80, 'grapes': 110, 'watermalon': 200}
>>> students = {'ram':44,'shyam':22,'harry':75,'john':54,'robert':25,'seta':65,'max':23,'harry':75,'robin':24,'philip':56}
>>> selected_students_dict = {}
>>> for student,marks in students.items():
... if marks >= 40:
... selected_students_dict[student] = marks
...
>>> selected_students_dict
{'ram': 44, 'harry': 75, 'john': 54, 'seta': 65, 'philip': 56}
>>> students = {'ram':44,'shyam':22,'harry':75,'john':54,'robert':25,'seta':65,'max':23,'harry':75,'robin':24,'philip':56}
>>> selected_students_dict = {student:marks for student,marks in students.items() if marks >= 40}
>>> selected_students_dict
{'ram': 44, 'harry': 75, 'john': 54, 'seta': 65, 'philip': 56}