Santhoshkumar
Santhoshkumar
ON
PYTHON
DEPTOF COMPUTER
SCIENCE
SUBMITTED BY…
Name: K.Nithis
Class : Bsc(cs)2 year
Register no : 23055861802111029
Subject : programming in python
Content
Function defenation
Function calling
Function Arguments
Packages in python
Encapsulation
Inheritance
Polymorphism
Method overloading
Data hiding
Function ;
In Python, a function is a reusable block of code that performs a specific task. It's defined using
the def keyword, followed by the function name, parentheses containing parameters (optional),
and a colon. The function body, containing the code to be executed, is indented.
Example:
def greet(name):
print("Hello, " + name + "!")
Definition:
In Python, a function is a reusable block of code that performs a specific task. It's defined using
the def keyword, followed by the function name, parentheses containing parameters (optional),
and a colon. The function body, containing the code to be executed, is indented.
Calling Arguments:
When a function is called, arguments (values passed to the function) are provided within the
parentheses. These arguments are used within the function body to perform calculations or
manipulate data.
Example:
def greet(name):
print("Hello, " + name + "!")
In this example:
greet is the function name.
name is a parameter (a placeholder for the argument that will be passed).
The function body prints a greeting message using the provided name.
When the function is called with the argument "Alice", the value "Alice" is assigned to
the name parameter, and the greeting message "Hello, Alice!" is printed.
Calling :
In Python, function calling involves invoking a previously defined function to execute its code.
When a function is called, the arguments (values passed to the function) are provided within the
parentheses. These arguments are then used within the function body to perform calculations or
manipulate data.
Example:
def greet(name):
print("Hello, " + name + "!")
In this example:
greet is the function name.
name is a parameter (a placeholder for the argument that will be passed).
The function body prints a greeting message using the provided name.
When the function is called with the argument "Alice", the value "Alice" is assigned to
the name parameter, and the greeting message "Hello, Alice!" is printed.
Arguments :
In Python, function arguments are the values that are passed to a function when it is called. They
serve as inputs to the function, allowing it to perform calculations or manipulate data based on
the specific values provided.
Types of Arguments:
Positional Arguments:
o These arguments are passed in the order they are defined in the function's
parameter list.
o The number of positional arguments must match the number of parameters unless
default values are provided.
recursive function
Python also accepts function recursion, which means a defined function can call
itself.
The developer should be very careful with recursion as it can be quite easy to
slip into writing a function which never terminates, or one that uses excess
amounts of memory or processor power. However, when written correctly
recursion can be a very efficient and mathematically-elegant approach to
programming.
Example
Recursion Example
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
Types of Packages :
1. Standard Library Packages: These are built-in packages that come with Python and
provide a wide range of functionalities, such as:
os: Operating system-related functions
sys: System-specific parameters and functions
math: Mathematical functions
random: Random number generation
time: Time-related functions
And many more
2. Third-Party Packages: These are packages created by the Python community and can
be installed using tools like pip. Some popular third-party packages include:
numpy: Numerical computing
pandas: Data analysis and manipulation
matplotlib: Data visualization
requests: HTTP requests
Django: Web framework
Flask: Web framework
And many others
Encapsulation
Encapsulation in Python is a fundamental object-oriented programming concept that involves
bundling data (attributes) and methods (functions) that operate on that data into a single unit,
known as a class. This helps in:
Key Benefits:
Data Hiding: By making attributes private, you can control how and when they are
accessed, preventing unintended modifications.
Abstraction: Encapsulation allows you to focus on the interface of a class (its public
methods) rather than its internal implementation, making it easier to use and maintain.
Modularity: Encapsulated classes can be reused in different parts of your code,
promoting code reusability and modularity.
Data Integrity: Encapsulation helps ensure data consistency and prevents accidental
corruption.
Example:
class Person:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def set_age(self, new_age):
if new_age >= 0:
self.__age = new_age
else:
print("Age cannot be negative.")
In this example, the Person class encapsulates the name and age attributes, along with methods
to access and modify them. The __ prefix makes the attributes private, ensuring that they can
only be accessed and modified through the public methods.
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.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
Polymorphism
Function Polymorphism
An example of a Python function that can be used on different objects is
the len() function.
String
For strings len() returns the number of characters:
Example
x = "Hello World!"
print(len(x))
Method Overloading
Method overloading, a common practice in many programming languages, involves defining
multiple methods with the same name but different parameters. This allows for more flexible and
concise code, as you can reuse the same method name for different scenarios.
Key Points:
Python Doesn't Support True Method Overloading: Unlike languages like Java and
C++, Python doesn't have a built-in mechanism for method overloading. This means you
cannot define multiple methods with the same name and different parameter types or
numbers.
Workarounds: While Python doesn't support direct method overloading, you can
achieve similar functionality using the following techniques:
Default Arguments:
Assign default values to parameters in a single method definition.
This allows you to call the method with different numbers of arguments.
Example:
Variable Arguments:
Use the *args syntax to accept a variable number of positional arguments.
This allows you to pass any number of arguments to the method.
Example:
def add_numbers(*args):
result = 0
for num in args:
result += num
return result
Data hiding
Data hiding is a fundamental principle in object-oriented programming that encapsulates data
within an object and restricts direct access from outside the object's scope. This ensures data
integrity, prevents unintended modifications, and promotes modularity and maintainability.
1. Private Attributes:
Double underscore prefix: By prepending a double underscore (__) to an attribute
name, you make it private to the class and its subclasses.
Name mangling: Python automatically mangles the name of private attributes to
prevent accidental access from outside the class.
Example:
Python
class MyClass:
def __init__(self):
self.__private_var = 10
def get_private_var(self):
return self.__private_var