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

Santhoshkumar

The document is an assignment on Python programming, covering key concepts such as function definition, calling, arguments, and various object-oriented programming principles like encapsulation, inheritance, polymorphism, method overloading, and data hiding. It provides definitions, examples, and explanations of these concepts to aid understanding. Additionally, it discusses standard and third-party packages in Python, illustrating their use and importance.

Uploaded by

sabarisri9585
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Santhoshkumar

The document is an assignment on Python programming, covering key concepts such as function definition, calling, arguments, and various object-oriented programming principles like encapsulation, inheritance, polymorphism, method overloading, and data hiding. It provides definitions, examples, and explanations of these concepts to aid understanding. Additionally, it discusses standard and third-party packages in Python, illustrating their use and importance.

Uploaded by

sabarisri9585
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ASSIGNMENT

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.

Parameters and Arguments:


 Parameters: Placeholders for values that will be passed to the function when it's called.
 Arguments: The actual values that are passed to the function when it's called.

Example:

def greet(name):
print("Hello, " + name + "!")

# Calling the function with an argument


greet("Alice")

 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 + "!")

# Calling the function with an argument


greet("Alice")

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 + "!")

# Calling the function with an argument


greet("Alice")

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.

def greet(name, age):


print("Hello, " + name + "! You are " + str(age) + " years old.")

greet("Alice", 30) # Positional arguments

 recursive function
Python also accepts function recursion, which means a defined function can call
itself.

Recursion is a common mathematical and programming concept. It means that


a function calls itself. This has the benefit of meaning that you can loop through
data to reach a result.

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.

In this example, tri_recursion() is a function that we have defined to call


itself ("recurse"). We use the k variable as the data, which decrements (-1)
every time we recurse. The recursion ends when the condition is not greater
than 0 (i.e. when it is 0).
To a new developer it can take some time to work out how exactly this works,
best way to find out is by testing and modifying it.

Example
Recursion Example

def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result

print("\n\nRecursion Example Results")


tri_recursion(6)

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.

How to Implement Encapsulation in Python:


1. Create a class: Define a class using the class keyword.
2. Define attributes: Declare attributes within the class using the self keyword.
3. Define methods: Create methods to operate on the attributes.
4. Control access: Use the __ (double underscore) prefix to make attributes and methods
private.

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.")

person = Person("Alice", 30)


print(person.get_name()) # Output: Alice
print(person.get_age()) # Output: 30
person.set_age(35)
print(person.get_age()) # Output: 35

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.

Create a Parent Class


Any class can be a parent class, so the syntax is the same as creating any other
class:
Example
Create a class named Person, with firstname and lastname properties, and
a printname method:

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

The word "polymorphism" means "many forms", and in programming it


refers to methods/functions/operators with the same name that can be
executed on many objects or classes.

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:

def greet(name, greeting="Hello"):


print(greeting, name)

greet("Alice") # Output: Hello Alice


greet("Bob", "Hi") # Output: Hi Bob

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

print(add_numbers(1, 2, 3)) # Output: 6


print(add_numbers(10, 20)) # Output: 30

 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.

In Python, data hiding is achieved primarily through:

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

You might also like