0% found this document useful (0 votes)
5 views1 page

Oops

Uploaded by

jamefik256
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)
5 views1 page

Oops

Uploaded by

jamefik256
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/ 1

1. What is class in Python, and how it is used to create objects?

ANS-a class is a blueprint for creating objects, encapsulating data (attributes) and behavior
(methods) into a single entity.

Creating Classes and Objects-To define a class in Python, use the class keyword. An object is
an instance of a class. When you create an object, you can access the data and methods
defined within that class.

class Dog:

species = "Canis familiaris"

def __init__(self, name, age):

self.name = name

self.age = age

my_dog = Dog("Buddy", 5)

2. What are methods and attributes in Python Classes?

ANS- Attributes: These are variables defined within a class to hold data. They can be class
attributes (shared by all objects) or instance attributes (specific to each object).

Methods: These are functions defined within a class that describe the behaviors or actions
of the object. They usually take self as the first parameter, referring to the current instance.

3. What is encapsulation and how does it protect data within a class?

ANS-Encapsulation is the practice of hiding the internal representation of an object, exposing


only what is necessary. This is achieved in Python by:

• Marking variables as "private" using a leading underscore (_) or double underscore (__),
indicating they shouldn't be accessed directly from outside the class.

• Providing methods (getter and setter functions) to access and modify these variables,
enabling controlled access.

You might also like