unit 3-1
unit 3-1
Example:
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]
Access List Elements:
Index 0 = 10
Index -1 = 40
Index -2 = 30
Iterating through List using loop:
The list element can be iterated using loops in
python.
1) append()
Syntax:
list_name.append(new_element)
2) insert()
This function insert an element at the specified
position in the list.
Syntax:
list_name.insert(index, element)
Example:
list = [1,2,3]
list.insert(1, 8)
3) extend()
This function adds elements of parameter list with
invoking list.
Syntax:
list_name.extend(another_list)
Example:
list1 = [1,2,3]
list2 = [10,20,30]
list1.extend(list2)
print(list1)
[1,2,3,10,20,30]
2) Updating element: Using assignment operator (=)
to over-write value:
Syntax:
list_name[index] = new_element
3) Delete or Remove element from list:
1) del:
del keyword is to remove one or more element from list
and even it can remove entire list.
Syntax:
del list_name[index]
2) remove():
Syntax:
list_name.remove(element)
3) pop():
Syntax:
list_name.pop(index_number)
4) clear():
Syntax:
list_name.clear()
Built-in List Functions:
Tuples:
A tuple is a collection of objects which is ordered and
immutable.
Example:
# empty tuple
my_ tuple = ( )
#Tuple of integers
my_tuple = (1, 2, 3)
print(tuple[1])
Negative tuple Indexing:
Index 0 = 10
Index -1 = 40
Index -2 = 30
Slicing tuple (Using slicing operator ( : ) on
tuple :
len(tuple)
tuple 1 = (1,2,3,4)
tuple 2 = (10,20,30)
tuple 3 = tuple1 + tuple2
Print(tuple3)
tuple2 = tuple * 3
print( tuple2)
Deleting Tuple:
Syntax:
del tuple_name
Set:
Sets are used to store multiple items in a single variable.
Set is one of the built-in data types in Python used to store collections of
data.
A set itself may be modified, but the elements contained in the set must be
of immutable type.
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and
cannot be referred to by index or key.
Declaring set Or Defining Set Or Creating
Set:
Simple Set Declaration:
Set1 = {10,20,30}
print(set1)
Set = { }
This will create a empty dictionary.
A = set ({ })
type()
From Python's perspective, sets are defined as objects with the
data type 'set'
Access Items of Set:
You cannot access items in a set by referring to an index or a
key.
But you can loop through the set items using a for loop, or ask
if a specified value is present in a set, by using the in keyword.
Iterating through Set using for-loop:
For example:
set1 = set({10,20,30})
for i in set1:
print(i)
Basic Set Operations:
1) Adding element: update() and add() functions:
Add() function:
Once a set is created, you cannot change its items, but you
can add new items.
set.add("orange")
print(“new set=“,set)
Update() function:
The update() method updates the current set, by adding
items from another set (or any other iterable).
Syntax
set.update(set)
Syntax
len(object)
1)discard():
While using discard() if the item does not exist in the set, it
remains unchanged.
Syntax:
set.discard(element)
2) remove():
The remove() method removes the specified element from
the set.
Syntax:
set.remove(element)
3) Pop():
Syntax:
set.pop()
4) Clear()
Syntax:
set.clear()
Set Union/ Join two set:
You can use the union() method that returns a new set
containing all items from both sets,
or the update() method that inserts all the items from one set
into another.
Syntax:
OR
set3 = set1.union(set2)
Set Intersection:
Intersection of A and B is a set of elements that are
common in both sets.
Syntax:
c=A&B
OR
C = A.intersection(B)
Set Difference:
Difference of A and B i.e. (A-B) is a set of elements that are
only in A but not in B.
Syntax:
C = A - B OR A.difference(B)
C = B – A OR B.difference(A)
Set Symmetric difference:
Symmetric difference of A and B is a set of elements in
both A and B except those that are common in both.
Syntax:
C = A^B
OR A.symmetric_difference(B)
Delete Set:
Similar to list and tuple, if you want to delete entire set.
Syntax:
del set
Dictionaries:
While the values can be of any data type and can repeat, keys
must be of immutable type (string, number or tuple with
immutable elements) and must be unique.
Dict1 = { } # empty dictionary
Dict2 = {1: “red”, 2: “blue”} # dict with integer keys
Dict3 = { “name”: “abc”, 1:[1,2,3]} # dict with mixed keys
Dict4 = dict([(1, “red”), (2, “blue”)]) #from seq having each item as
a pair
Accessing Dictionary elements using index
numbers:
Elements/values of a dictionary are accessible only when
keys/indexes are known.
Syntax:
dict.keys()
dict.values()
Basic Dictionary operations:
1) Change or update elements in dictionary: Update()
function
Syntax:
dict.update(1:”abc”)
Length of the dictionary:
The len() function returns the number of items in an
object.
Syntax
len(object)
Syntax:
dict[key]=value
If you want to update new item the key should be new and
assign the value to the key.
1) Del keyword:
Syntax:
dict.pop(“key”)
3) popitem() function:
Syntax: dict.popitem()
4) clear():
The clear() function will remove all elements form the dictionary.
Syntax: dict.clear()