0% found this document useful (0 votes)
141 views6 pages

Python Notes (Unit-4)

The document discusses various Python concepts like slicing, encapsulation, inheritance, built-in functions and methods of common Python data types like lists, tuples, dictionaries and sets.

Uploaded by

Vish P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
141 views6 pages

Python Notes (Unit-4)

The document discusses various Python concepts like slicing, encapsulation, inheritance, built-in functions and methods of common Python data types like lists, tuples, dictionaries and sets.

Uploaded by

Vish P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

UNIT – IV

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

start Optional. An integer number specifying at which position to


start the slicing. Default is 0

end An integer number specifying at which position to end the


slicing

step Optional. An integer number specifying the step of the slicing.


Default is 1

2. What is encapsulation in Python with example?


Encapsulation is one of the most fundamental concepts in object-oriented
programming (OOP). This is the concept of wrapping data and methods that work
with data in one unit. A class is an example of encapsulation as it encapsulates all
the data that is member functions, variables, etc. The goal of information hiding
is to ensure that an object’s state is always valid by controlling access to
attributes that are hidden from the outside world.
3. What is __ init __ Python?
__init__ method in Python is used to initialize objects of a class. It is also called
a constructor. The task of constructors is to initialize (assign values) to the data
members of the class when an object of the class is created. Like methods, a
constructor also contains a collection of statements (i.e. instructions) that are
executed at the time of Object creation. It is run as soon as an object of a class is
instantiated.
4. Give some examples of built in tuple functions.
Built-in Function Description
max() return maximum element of given tuple
min() return minimum element of given tuple
sum() Sums up the numbers in the tuple
len() Returns length of the tuple or size of the tuple

5. How do you access values in dictionary?


 You can access the items of a dictionary by referring to its key name, inside
square brackets:
Ex: thisdict={“brand”: “Ford”, model: “Mustang”, “year”: 1964}
x = thisdict["model"]
 There is also a method called get() that will give you the same result:
x = thisdict.get("model")
 The keys() method will return a list of all the keys in the dictionary.
x = thisdict.keys()

6. How do you delete elements in dictionary?


In Python, you can remove an item (key-value pair) from a dictionary (dict) using
the clear(), pop(), popitem() methods, or the del statement. You can also remove
items that satisfy the conditions using dictionary comprehensions.
 The clear() method removes all items from a dictionary, making it empty.
 The pop() method removes the item associated with the specified key from
the dictionary and returns its value.
 The popitem() method removes an item from a dictionary and returns its key
and value as a tuple, (key, value)
 The del statement to delete an item from a dictionary.

7. List out the built - in functions of the dictionary.

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

8. Name the uses of Inheritance.

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}

9. Give some built- in functions of Lists.

Built-in Function Description


Append() Adds an element to the end of the list.
insert() Inserts an element at a specified position in the list.
sort() Sorts the elements in the list in ascending order.
pop() Removes and returns the element at a specified index.

10.Give some examples of List Methods.

List Methods Description


copy() It returns a shallow copy of a list
clear() This method is used for removing all items from the list.
count() These methods count the elements.
index() Returns the lowest index where the element appears.

11.What is a set in python?

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 { }

12. What are the operations of set?

As you learnt in mathematics, the python is also supports the set operations such as
Union, Intersection, difference and Symmetric difference.

 Union: It includes all elements from two or more sets


In python, the operator | is used to union of two sets. The function union( ) is also
used to join two sets.

 Intersection: It includes the common elements in two sets

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.

13. Name some methods of set?

Set Methods Description


update() Update the set with another set, or any other iterable
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not.
issuperset() Returns whether this set contains another set or not

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.

You might also like