0% found this document useful (0 votes)
8 views5 pages

PYTHON

The document provides an overview of key concepts in Python, including defining functions, creating user-defined functions, and understanding classes and objects. It explains data hiding, inheritance types, variable scopes, data conversion functions, method overloading and overriding, file modes, user input, and list creation and access. Each concept is illustrated with code examples for clarity.

Uploaded by

xfarcartoon58
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)
8 views5 pages

PYTHON

The document provides an overview of key concepts in Python, including defining functions, creating user-defined functions, and understanding classes and objects. It explains data hiding, inheritance types, variable scopes, data conversion functions, method overloading and overriding, file modes, user input, and list creation and access. Each concept is illustrated with code examples for clarity.

Uploaded by

xfarcartoon58
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/ 5

1

1) Define Function

In Python, a function is a block of reusable code that performs a specific task. You define a
function using the def keyword, followed by the function name and parentheses () that may
include parameters.

• def function_name(parameters):
• # Code block (indented)
• return result # optional

2)How to create user define function

Steps to Create a User-Defined Function:

• Use the def keyword.


• Give your function a name.
• Add parentheses () — include parameters if needed.
• End the line with a colon:
• Write your indented code block inside the function.

3)How to create class and Object

• class MyClass:
• def __init__(self, name):
• self.name = name
• def greet(self):
• print("Hello,", self.name)

4)What is data hiding

Data Hiding is an object-oriented programming concept where internal object details (like
variables) are hidden from outside access to protect the integrity of the data.

In Python, data hiding is done using name mangling by prefixing variables or methods
with double underscores (__).

class Student:

• def __init__(self, name, marks):


• self.name = name
• self.__marks = marks # Hidden variable

• def show(self):
• print(f"{self.name} scored {self.__marks} marks.")
• s1 = Student("Amit", 92)
• s1.show()
2

5)Explain print()function with its argument

6)What is single inheritance

Single Inheritance is a type of inheritance in object-oriented programming where a child


class (derived class) inherits from one parent class (base class).

This allows the child class to:

1. Reuse the code of the parent class


2. Add or override functionalities
• class A:
• def show(self):
• print("Hello from A")
• class B(A):
• def display(self):
• print("Hi from B")
• b = B()
• b.show()
• b.display()

7)Define and explain local variable and global variable with suitable Example

A local variable is declared inside a function and can be used only within that function

• def my_func():
• x = 10 # local variable
• print("Local x:", x)
• my_func()

A global variable is declared outside all functions and can be accessed inside any function

• x = 20 # global variable
• def my_func():
• print("Global x inside function:", x)
• my_func()

8)Explain any four data conversion function in python

• int() :-Converts a number or string to an integer (whole number).


• float() :- Converts a number or string to a decimal number
• str() :- Converts numbers or other data types to strings
• list() :- Converts an iterable (like string, tuple) to a list
3

9)Explain method overloading and method overriding with suitable example

Feature Method Overloading Method Overriding


Concept Same method name, different Same method name in child class
parameters in the same class that redefines the method from
parent class
Inheritance Not required Requires inheritance
Polymorphism Compile-time (simulated in Run-time
Type Python)
Example class Calculator: class Animal:
def add(self, a=0, b=0, c=0): def sound(self):
return a + b + c print("Animal makes sound")

calc = Calculator() class Dog(Animal):


print(calc.add(2, 3)) # def sound(self): # Overriding
Output: 5 parent method
print(calc.add(2, 3, 4)) # print("Dog barks")
Output: 9
d = Dog()
d.sound() # Output: Dog barks

10)Explain multiple And multilevel inheritance.

Feature Multiple Inheritance Multilevel Inheritance


Definition A child class inherits from more than A class inherits from a class which is
one parent class. itself inherited.
Structure Combines multiple base classes into Forms a chain of inheritance across
one derived class. multiple levels.
Syntax class C(A, B): class C(B):, class B(A):
Diagram A←C→B A→B→C
Complexity More complex due to multiple Less complex, follows a single path
parents. of inheritance.
Example class A: class A:
def showA(self): def showA(self):
print("This is class A") print("This is class A")

class B: class B(A): # Inherits from A


def showB(self): def showB(self):
print("This is class B") print("This is class B")

class C(A, B): # Multiple Inheritance class C(B): # Inherits from B


def showC(self): def showC(self):
print("This is class C") print("This is class C")

obj = C() obj = C()


obj.showA() obj.showA()
obj.showB() obj.showB()
obj.showC() obj.showC()
4

11)Explain any four file mode in python

1. 'r' – Read Mode

• Use: Opens the file for reading only.


• File must exist, otherwise an error is thrown.
• Default mode if no mode is specified.

Example:

• f = open("data.txt", "r")
• print(f.read())
• f.close()

2. 'w' – Write Mode

• Use: Opens the file for writing only.


• Overwrites the file if it exists.
• Creates a new file if it doesn’t exist.

Example:

• f = open("data.txt", "w")
• f.write("Hello World!")
• f.close()

3. 'a' – Append Mode.

• Adds content to the end of the file.


• Does not overwrite existing data.
• Creates the file if it doesn’t exist.

Example:

• f = open("data.txt", "a")
• f.write("\nNew Line")
• f.close()

4. 'r+' – Read and Write Mode

• Use: Opens the file for both reading and writing.


• File must exist, or an error will occur.
• Does not truncate the file (unlike 'w').

Example:

• f = open("data.txt", "r+")
• print(f.read())
• f.write("\nAdded text")
• f.close()
5

12)which function is used to take input from user

The input() function is used to take input from the user via the keyboard.

Syntax;- variable = input("Enter something: ")

Example :- name = input("Enter your name: ")

print("Hello,", name)

Example :- age = int(input("Enter your age: "))

print("You are", age, "years old.")

14)how tom create and access list

A list is a built-in ordered, mutable collection used to store multiple items in a single
variable.

# Creating a list

fruits = ["apple", "banana", "mango"]

# Accessing a list

print(fruits[0]) # apple

print(fruits[-1]) # mango

# Accessing a list Using LOOP

for f in fruits:

print("I like", f)

You might also like