PWP_Unit-3-Notes
PWP_Unit-3-Notes
• 3.1 List
a) Defining list , accessing value in list, deleting value in list, updating list
b) Basic list Operations
c) Built in List-functions
Defining list
Creating a list is as simple as putting different comma-separated
values between square brackets.
• # List of integers
• a = [1, 2, 3, 4, 5]
• # List of strings
• b = ['apple', 'banana', 'cherry']
• S=“python”
• Print(s[0])----------p
• print(list1[2])
• print(list1[2:5])
• print(list1[3:])
• print(list1[-2])
• print(list1[-4:3])
a = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Get the entire list reverse using negative
step
b = a[-2:] b = a[::-1]
print(b) print(b)
d = a[-4:-1] print(my_list[1:3:-1])----- []
print(d)
e = a[-8:-1:2]
print(e)
deleting value in list
• Lists in Python have various built-in methods to remove items such
as remove, pop, del and clear methods.
remove():-
• The remove() method deletes the first occurrence of a specified value in the list.
If multiple items have the same value, only the first match is removed.
• Syntax remove(value only)
• a = [10, 20, 30, 40, 50]
• a.remove(30)
• print(a) ---------- [10, 20, 40, 50]
Pop(): The pop() method can be used to remove an element from
a list based on its index and it also returns the removed
element.
• a = [10, 20, 30, 40, 50]
val = a.pop(1)
print(a)
print("Removed Item:", val)
Output:-
[10, 30, 40, 50]
Removed Item: 20
del():The del statement can remove elements from a list by
specifying their index or a range of indices.
• Example:
• a = [10, 20, 30, 40, 50,60,70]
• del a[2]
• print(a)----------Output:[10, 20, 40, 50]
• del a[1:4]
• print(a)---------- [10, 50, 60, 70]
Clear():-If we want to remove all elements from a list then we can use
the clear() method. and return empty list
8.len():-The Python List len() method is used to compute the size of a Python List
• Syntax:- len(list)
• Print(len(A))
Tuple:
a)Creating tuple, Accessing value in tuple , deleting value in tuple,
updating value in tuple
• Creating tuple:
• tuple of integers
• a = (1, 2, 3, 4, 5,1)
• # of tuple strings
• b = ('apple', 'banana', 'cherry‘)
2. min():-min(): gives the smallest element in the tuple as an output. Hence, the name is min().
• >>> max(tup)-------------------------output:-890
3. max(): gives the largest element in the tuple as an output. Hence, the name is
max()
• >>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
• >>> max(tup)----------------------output:-890
4. Sum():-gives the sum of the elements present in the tuple as an output
• >>> tup = (22, 3, 45, 4, 2, 56, 890, 1)
• >>> sum(tup)---------------------------1023
Set
a) Accessing values in set, deleting values in set, Updating sets
b) Basic set operations
c) Built-in set functions
• Sets are used to store multiple items in a single variable.
• Sets do not support indexing
• A set is a collection which is unordered, unchangeable, and unindexed
• Unordered means that the items in a set do not have a defined order
• The elements of the set can not be duplicate.
• The elements of the python set must be immutable.
• The values False ,True and 0,1 are considered the same value in sets,
and are treated as duplicates
• Set declared with curly braces
Creating a set
• Example 1:
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"}
print(Days)
Accessing set values:we access value from set using for loop
for i in Days:
print(i)
• Output:
• Friday
• Tuesday
• Monday
• Saturday
• Thursday
• Sunday
Updating set
• Output:-
• {'February', 'March', 'April', 'June'}
remove() method: method Python provides remove()
method which can be used to remove the items from the
set
• thisset = {"apple", "banana", "cherry"}
• thisset.remove("banana")
• print(thisset)
• output:
• {"apple”, "cherry"}
difference between remove and
discard
• The discard() method removes the specified item from the set. This
method is different from the remove() method, because the remove()
method will raise an error if the specified item does not exist, and the
discard() method will not
pop() method
• the pop(), method to remove an item, but this method will remove the last item.
Remember that sets are unordered, so you will not know what item that gets
removed
• Note: Sets are unordered, so when using the pop() method, you will not know
which item that gets removed
• thisset = {"apple", "banana", "cherry"}
• x = thisset.pop()
• print(x)
• print(thisset)
• output:
• apple
• {'cherry', 'banana'
delete the set
• The del keyword will delete the set completely:
• thisset = {"apple", "banana", "cherry"}
• del thisset
• print(thisset)
• output:
• File "demo_set_del.py", line 5, in print(thisset) #this will raise an error because the set no
longer exists NameError: name 'thisset' is not defined
Adding items to the set
• add() method
• update() method.
add() method
• Python provides the add() method which can be used to add some particular
item to the set
• | for union.
• & for intersection.
• – for difference
• ^ for symmetric difference
• Set union:-
• The union of two sets is the set of all the elements of both the sets without
duplicates. You can use the union() method or the | operator
• Example-1:-
first_set = {1, 2, 3}
second_set = {3, 4, 5}
• first_set.union(second_set)----------------------------{1, 2, 3, 4, 5}
• Example-2:-
• # using the `|` operator
• first_set | second_set -----------------------------------------{1, 2, 3, 4, 5}
Set intersection:-The intersection of two sets is the set of all the common
elements of both the sets. You can use the intersection() method of the &
operator to find the intersection of a Python set.
• first_set = {1, 2, 3, 4, 5, 6}
• second_set = {4, 5, 6, 7, 8, 9}
• first_set.intersection(second_set)------------------------{4, 5, 6}
• delete an element
del x["four"]----------------------{"one":”hiii”, "two":2, "three":3}
remove all items
x.clear() ------------------------------{}
x = {"one":1, "two":2, "three":3}
• looping over values
• for i in x.values():
print(i)
Output
1
2
3
• looping over keys
• for I in x.keys():
• Print( i)
• Output:_
One
two