0% found this document useful (0 votes)
7 views13 pages

Python 1

The document explains the usage of 'pop()' and 'del' in Python for manipulating lists and dictionaries. It details how 'pop()' removes and returns an element while 'del' is a general-purpose deletion statement that does not return anything. Additionally, it covers sorting methods 'sort()' and 'sorted()', highlighting their differences in modifying the original list and returning new lists.

Uploaded by

Danish Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Python 1

The document explains the usage of 'pop()' and 'del' in Python for manipulating lists and dictionaries. It details how 'pop()' removes and returns an element while 'del' is a general-purpose deletion statement that does not return anything. Additionally, it covers sorting methods 'sort()' and 'sorted()', highlighting their differences in modifying the original list and returning new lists.

Uploaded by

Danish Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Python

Basic Concepts
Pop() del
• Used with lists and dictionaries. • General-purpose deletion
• Removes an element and returns statement.
it. • Does not return anything.
• For lists → pop(index) removes the • Can delete variables, list
element at a given index (default is slices, items in lists, keys in
the last element). dictionaries, or even entire
• For dicts → pop(key) removes the objects.
key–value pair and returns the
value.
del
• Delete a variable
x = 10
del x

• Delete an item from a list


numbers = [1, 2, 3, 4, 5]
del numbers[2] # deletes element at index 2 → 3
print(numbers) # Output: [1, 2, 4, 5]
del
• Delete a slice of a list (multiple elements at once)

nums = [10, 20, 30, 40, 50, 60]


del nums[1:4] # deletes elements at index 1,2,3 → 20,30,40
print(nums) # Output: [10, 50, 60]

• Delete entire list


my_list = [10, 20, 30, 40]
del my_list # deletes the entire list
Now trying to access it will give an error:
print(my_list) → NameError: name 'my_list' is not defined
del
• Delete a key–value pair in a dictionary
data = {'a': 1, 'b': 2, 'c': 3}
del data['b'] # removes key 'b' and its value
print(data) # {'a': 1, 'c': 3}
sort()
• It works on Lists only
nums = [5, 2, 9, 1]
nums.sort()
print(nums) # [1, 2, 5, 9]

• It arranges the list in-place (i.e., it changes the original list) in


ascending order by default.
nums = [5, 2, 9, 1]
nums.sort()
print(nums) # [1, 2, 5, 9]
print(nums[2]) # 5
sort()
• It does not return a new list → it returns None.
sort() : parameters
• reverse
nums = [5, 2, 9, 1]
nums.sort(reverse=True)
print(nums) # [9, 5, 2, 1]

• nums.sort(reverse=False)

• Key
words = ['apple', 'kiwi', 'banana', 'fig']
words.sort(key=len)
print(words) # Output: ['fig', 'kiwi', 'apple', 'banana']
sorted()
• If need a new sorted list without changing the original, use sorted()
instead of using sort()
nums = [5, 2, 9, 1]
new_list = sorted(nums)
print(new_list) # [1, 2, 5, 9]
print(nums) # [5,2,9,1] (original list remains unchanged)
sort() vs sorted()

Method Behavior Returns new list?

sort() Sorts the same list No

sorted() Creates a new sorted list Yes


Exercise
• Create a list that contains numeric and strings values then use
sort() method.
Exercise
• Create a list that contains strings with lowercase and upper case
then use sort() method
how to print last element of a list
• my_list = [10, 20, 30, 40, 50]
• print(my_list[-1])

You might also like