[]
and separated by commas.list1 = [2, 4, 6, 8, 10, 12]
creates a list of six even numbers, and printing it outputs [2, 4, 6, 8, 10, 12]
.list2 = ['a', 'e', 'i', 'o', 'u']
creates a list of vowels, and printing it outputs ['a', 'e', 'i', 'o', 'u']
.list3 = [100, 23.5, 'Hello']
creates a list with mixed data types, and printing it outputs [100, 23.5, 'Hello']
.list4 = [['Physics', 101], ['Chemistry', 202], ['Mathematics', 303]]
creates a nested list, and printing it outputs [['Physics', 101], ['Chemistry', 202], ['Mathematics', 303]]
.[]
with the index of the element, e.g., list1[0]
returns the first element.-1
refers to the last element, -2
to the second-to-last, and so on.list1 = [2, 4, 6, 8, 10, 12]
, list1[0]
returns 2
, and list1[3]
returns 8
.IndexError: list index out of range
.list1
, list1[15]
raises an IndexError
.list1[1+4]
returns 12
.list1[-1]
returns the last element, 12
.len()
function, e.g., len(list1)
returns 6
.list1[len(list1)-1]
, which returns 12
.list1[-len(list1)]
returns 2
.list1 = ['Red', 'Green', 'Blue', 'Orange']
, assigning list1[3] = 'Black'
changes the fourth element, resulting in ['Red', 'Green', 'Blue', 'Black']
.+
operator joins two or more lists.list1 = [1, 3, 5, 7, 9]
and list2 = [2, 4, 6, 8, 10]
, list1 + list2
returns [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
.list3 = ['Red', 'Green', 'Blue']
and list4 = ['Cyan', 'Magenta', 'Yellow', 'Black']
, list3 + list4
returns ['Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow', 'Black']
.newList = list1 + list2
.+
operator requires both operands to be lists; concatenating a list with a non-list type raises a TypeError
.for
loop or a while
loop.list1 = ['Red', 'Green', 'Blue', 'Yellow', 'Black']
, the loop for item in list1: print(item)
outputs each color on a new line: Red
, Green
, Blue
, Yellow
, Black
.list1
, the loop for i in range(len(list1)): print(list1[i])
produces the same output.len(list1)
function returns the number of elements in the list.range(n)
function generates a sequence of numbers from 0 to n-1
.len()
: Returns the number of elements in a list. Example: For list1 = [10, 20, 30, 40, 50]
, len(list1)
returns 5
.list()
: Creates an empty list if no argument is provided, e.g., list1 = list()
returns []
.list()
: Converts a sequence (e.g., string) into a list. Example: For str1 = 'aeiou'
, list1 = list(str1)
returns ['a', 'e', 'i', 'o', 'u']
.append()
: Adds a single element to the end of the list. Example: For list1 = [10, 20, 30, 40]
, list1.append(50)
results in [10, 20, 30, 40, 50]
.append()
: Can append a list as a single element. Example: list1.append([50, 60])
results in [10, 20, 30, 40, [50, 60]]
.extend()
: Appends each element of a provided list to the end of the list. Example: For list1 = [10, 20, 30]
and list2 = [40, 50]
, list1.extend(list2)
results in [10, 20, 30, 40, 50]
.insert()
: Inserts an element at a specified index. Example: For list1 = [10, 20, 30, 40, 50]
, list1.insert(2, 25)
results in [10, 20, 25, 30, 40, 50]
.count()
: Returns the number of occurrences of an element. Example: For list1 = [10, 20, 30, 10, 40, 10]
, list1.count(10)
returns 3
.index()
: Returns the index of the first occurrence of an element; raises ValueError
if not found. Example: For list1 = [10, 20, 30, 20, 40, 10]
, list1.index(20)
returns 1
.remove()
: Removes the first occurrence of an element; raises ValueError
if not found. Example: For list1 = [10, 20, 30, 40, 50, 30]
, list1.remove(30)
results in [10, 20, 40, 50, 30]
.pop()
: Removes and returns the element at the specified index; if no index is provided, removes the last element. Example: For list1 = [10, 20, 30, 40, 50, 60]
, list1.pop(3)
returns 40
and results in [10, 20, 30, 50, 60]
.reverse()
: Reverses the order of elements in the list. Example: For list1 = [34, 66, 12, 89, 28, 99]
, list1.reverse()
results in [99, 28, 89, 12, 66, 34]
.sort()
: Sorts the list in place. Example: For list1 = ['Tiger', 'Zebra', 'Lion', 'Cat', 'Elephant', 'Dog']
, list1.sort()
results in ['Cat', 'Dog', 'Elephant', 'Lion', 'Tiger', 'Zebra']
.sort(reverse=True)
: Sorts the list in descending order. Example: For list1 = [34, 66, 12, 89, 28, 99]
, list1.sort(reverse=True)
results in [99, 89, 66, 34, 28, 12]
.sorted()
: Returns a new sorted list without modifying the original. Example: For list1 = [23, 45, 11, 67, 85, 56]
, sorted(list1)
returns [11, 23, 45, 56, 67, 85]
.min()
: Returns the smallest element. Example: For list1 = [34, 12, 63, 39, 92, 44]
, min(list1)
returns 12
.max()
: Returns the largest element. Example: For the same list1
, max(list1)
returns 92
.sum()
: Returns the sum of all elements. Example: For the same list1
, sum(list1)
returns 284
.:
.{}
and separated by commas.{}
, with keys and values separated by colons :
.dict1 = {}
creates an empty dictionary.dict3 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
creates a dictionary mapping student names to their marks, and printing it outputs {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
.dict3 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
, dict3['Ram']
returns 89
.KeyError
, e.g., dict3['Shyam']
raises KeyError: 'Shyam'
.in
operator checks if a key exists in the dictionary, returning True
if present, False
otherwise.dict1 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
, 'Suhel' in dict1
returns True
.not in
operator returns True
if the key is not present, False
otherwise.dict1
, 'Suhel' not in dict1
returns False
.dict1 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
, dict1['Meena'] = 78
results in {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85, 'Meena': 78}
.dict1
, dict1['Suhel'] = 93.5
results in {'Mohan': 95, 'Ram': 89, 'Suhel': 93.5, 'Sangeeta': 85}
.for
loop to access each key-value pair.dict1 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
, the loop for key in dict1: print(key, ':', dict1[key])
outputs each key-value pair on a new line.items()
method to iterate over key-value pairs directly. Example: For the same dict1
, the loop for key, value in dict1.items(): print(key, ':', value)
produces the same output.len()
: Returns the number of key-value pairs. Example: For dict1 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sange Morrison': 85}
, len(dict1)
returns 4
.dict()
: Creates a dictionary from a sequence of key-value pairs. Example: For pair1 = [('Mohan', 95), ('Ram', 89), ('Suhel', 92), ('Sangeeta', 85)]
, dict1 = dict(pair1)
results in {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
.keys()
: Returns a list of all keys. Example: For dict1
, dict1.keys()
returns dict_keys(['Mohan', 'Ram', 'Suhel', 'Sangeeta'])
.values()
: Returns a list of all values. Example: For dict1
, dict1.values()
returns dict_values([95, 89, 92, 85])
.items()
: Returns a list of tuples containing key-value pairs. Example: For dict1
, dict1.items()
returns dict_items([('Mohan', 95), ('Ram', 89), ('Suhel', 92), ('Sangeeta', 85)])
.get()
: Returns the value for a given key; returns None
if the key is not present. Example: For dict1
, dict1.get('Sangeeta')
returns 85
, and dict1.get('Sohan')
returns None
.update()
: Adds key-value pairs from another dictionary. Example: For dict1 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
and dict2 = {'Sohan': 79, 'Geeta': 89}
, dict1.update(dict2)
results in {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85, 'Sohan': 79, 'Geeta': 89}
.clear()
: Removes all items from the dictionary. Example: For dict1
, dict1.clear()
results in {}
.del
: Deletes an item with a specific key or the entire dictionary. Example: For dict1
, del dict1['Mohan']
removes the item, and del dict1
deletes the dictionary entirely.ODD = {1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven', 9: 'Nine'}
to map odd numbers to their word representations.ODD.keys()
, which returns dict_keys([1, 3, 5, 7, 9])
.ODD.values()
, which returns dict_values(['One', 'Three', 'Five', 'Seven', 'Nine'])
.ODD.items()
, which returns dict_items([(1, 'One'), (3, 'Three'), (5, 'Five'), (7, 'Seven'), (9, 'Nine')])
.len(ODD)
, which returns 5
.7 in ODD
, which returns True
, or 2 in ODD
, which returns False
.ODD.get(9)
, which returns 'Nine'
.del ODD[9]
, resulting in {1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven'}
.1. What are lists in Python and how do they differ from other data types? | ![]() |
2. How can I access elements in a list? | ![]() |
3. What are some common operations I can perform on lists? | ![]() |
4. How do I create and access items in a dictionary? | ![]() |
5. What is the membership operation in dictionaries and how does it work? | ![]() |