Python Notes (Unit-4)
Python Notes (Unit-4)
SECTION – A
1. What is slicing in Python?
Slicing is the extraction of a part of a string, list, or tuple. It enables users to access
the specific range of elements by mentioning their indices. The slice() function
returns a slice object. A slice object is used to specify how to slice a sequence. You
can specify where to start the slicing, and where to end.
Syntax:
slice(start, end, step)
Parameter Values:
Parameter Description
Built-in Description
Function
any() Returns True if any of the key in the dictionary is True. If the
dictionary is empty it returns False
len() Returns the number of items in the dictionary
cmp() Used to compare items of 2 dictionaries
sorted() Returns a sorted list of keys in the dictionary
Inheritance allows us to define a class that inherits all the methods and properties
from another class. Parent class is the class being inherited from, also called base
class. Child class is the class that inherits from another class, also called derived
class. It provides the reusability of a code. We don’t have to write the same code
again and again.
Syntax:
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
Set is a data type in python used to store several items in a single variable. It is an
unordered collection data type that is iterable, mutable and has no duplicate
elements. It is a collection that is written with curly brackets and is both unindexed
and unordered. It is one of the four built-in data types (List, Dictionary, Tuple, and
Set) having qualities and usage different from the other three. Set are represented
by { }
As you learnt in mathematics, the python is also supports the set operations such as
Union, Intersection, difference and Symmetric difference.
The operator & is used to intersect two sets in python. The function intersection( )
is also used to intersect two sets in python
Difference: It includes all elements that are in first set (say set A) but not in
the second set (say set B)
The minus (-) operator is used to difference set operation in python. The function
difference( ) is also used to difference operation.
14.Name some of the functions to add and remove elements from the Set.
Add elements from the set:
add() method:
Elements can be added to the Set by using the built-in add() function.
update() method:
For the addition of two or more elements Update() method is used.
Remove elements from the set:
remove() method or discard() method:
Elements can be removed from the Set by using the built-in remove() function
but a KeyError arises if the element doesn’t exist in the set. To remove elements
from a set without KeyError, use discard(), if the element doesn’t exist in the set, it
remains unchanged.
pop() method:
Pop() function can also be used to remove and return an element from the set,
but it removes only the last element of the set.
SECTION – B
15.