PYTHON ASSIGNMENT IIIQ

PYTHON ASSIGNMENT IIIQ(1). Write about functions in Python.

Ans. Function are used to reduce code duplication and to help structure and modularize programs to perform a task.Functions often require arguments, that is, a specific data values,to perform their tasks.Arguments are also known as parameters When a function completes its task,the function in the first place.The process of sending a result back to another part of a program is known as returning a value.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

A call to a function initiates a four step process:
The calling program is suspended.

The values of actual parameters are assigned to the formal parameters.

The body of the function is executed.

Controls returns immediately following the function call in the calling program.

The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyword, variable-length argument list.

The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.

If you pass immutable arguments i.e which cannot be modified like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is actually passed to the function parameters. They can’t be changed within the function, because they can’t be changed at all, i.e. they are immutable. It’s different, if we pass mutable arguments. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller’s scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller’s scope will remain untouched. 
Sometimes we may want to use parameters in a function that takes default values in case the user doesn’t want to provide a value for them.

For this, we can use default arguments. In the list of parameters, we can give default values to one or more parameters.

The assignment operator ‘=’ is used to give a default value to an argument.

def sum(a=4, b=2): #2 is supplied as default argument
// The function will print sum of two numbers
if the arguments are not supplied
it will just add the default value //
print (a+b)
sum(1,2) #calling with arguments
sum( ) #calling without arguments
OUTPUT:
3
6
In a function, the values which are passed through arguments are assigned to parameters in order, by their position.

The importance of Keyword arguments is that, we can use the name of the parameter irrespective of its position while calling the function for the supplying of the values. All the keyword arguments must surely match one of the arguments accepted by the function.

def print_name(name1, name2):
//This function prints the name "//
print (name1 + " and " + name2 + " are friends")
#calling the function
print_name(name2 = ‘John’,name1 = ‘Gary’)
OUTPUT:
Gary and John are friends
Sometimes we may need more arguments to process the function. If we don’t know in advance i.e in prior about the arguments needed in function, we can use variable-length arguments also called arbitrary arguments.

For this an asterisk (*) is placed before a parameter in function definition which can hold non-keyword variable-length arguments and a double asterisk (**) is placed before a parameter in function which can hold keyword variable-length arguments.

def display(*name, **address):
for items in name:
print (items)
for items in address.items():
print (items)
#Calling the function
display(‘john’,’Mary’,’Nina’,John=’LA’,Mary=’NY’,Nina=’DC’)
OUTPUT:
John
Mary
Nina
(‘John’, ‘LA’)
(‘Mary’, ‘NY’)
(‘Nina’, ‘DC’)
n Python, a variable declared outside of the function or in global scope is known as global variable. This means, global variable can be accessed inside or outside of the function.

x = ;global;
def foo():
print(;x inside :;, x)
foo()
print(;x outside:;, x)
OUTPUT:
x inside : global
x outside: global
A variable declared inside the function’s body or in the local scope is known as local variable.

Normally, we declare a variable inside the function to create a local variable.

def foo():
y = "local"
print(y)
foo()
OUTPUT:
Local
Q(2).Explain about inheritance in python with example.

Ans. Inheritance is a powerful feature in object oriented programming.It refers to defining a new  HYPERLINK "https://www.programiz.com/python-programming/class" o "Python Objects and Class" class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.

Derived class inherits features from the base class, adding new features to it. This results into re-usability of code.

Python Inheritance Syntax:
class Base Class:
Body of base class
class Derived Class (Base Class):
Body of derived class
SINGLE INHERITANCE
A single Python inheritance is that when a single class inherits from a class.

>>> x=0
>>> class fruit:
def __init__(self):
global x
x+=1
print("I’m a fruit;)
;;; class citrus(fruit):
def __init__(self):
super().__init__()
global x
x+=2
print(;I’m citrus")
>>> x
OUTPUT:
I’m a fruit
I’m citrus
MULTIPLE INHERITANCE
Multiple Python inheritance is that when multiple python classes inherit from a class.

>>> class Color:
pass
>>> class Fruit:
pass
>>> class Orange(Color,Fruit):
pass
>>> is subclass(Orange,Color) and is subclass(Orange,Fruit)
True
MULTILEVEL INHERITANCE
When one class inherits from another, which in turn inherits from another, it is called as multilevel python inheritance.

>>> class A:
x=1
>>> class B(A):
pass
>>> class C(B):
pass
>>> c obj=C()
>>> c obj.x
OUTPUT:
1
HIERARCHICAL INHERITANCE
When more than one class inherits from a class, it is called as hierarchical Python inheritance.

>>> class A:
pass
>>> class B(A):
pass
>>> class C(A):
pass
>>> issubclass(B,A) and issubclass(C,A)
OUTPUT:
True
HYBRID INHERITANCE
Hybrid Python inheritance is a combination of any two kinds of inheritance.

>>> class A:
x=1
>>> class B(A):
pass
>>> class C(A):
pass
>>> class D(B,C):
pass
>>> d obj=D()
>>> d obj.x
1
Q(3).Explain why test case is required.Explain about unit testing in Python.

Python unit testing should be performed starting from the first stages and throughout the whole process of development. Unit tests are written to detect bugs early in the development of the application when bugs are less frequent and less expensive to fix. However, if unit tests are not designed well, or they are run incorrectly, bugs go through into the production stage. Skipping unit testing in the development process will surely cost your company thousands, if not hundreds of thousand of dollars depending on the scale of the project.

Python Unit Testing:
A unit test is a scripted code-level test, written in  HYPERLINK "https://quintagroup.com/cms/python/index_html" Python to verify a small "unit" of functionality. Unit tests, in general, are often designed to provide a broad testing coverage of the web application, including any weird corner cases and some tests that should not work. Their interaction with external resources like the disk, the network, and databases is set to the minimum; testing code that is dependent on these resources is usually put through functional tests, automated acceptance tests, or integration tests.

Creating test cases is accomplished by sub classing – HYPERLINK "https://docs.python.org/3/library/unittest.html" l "unittest.TestCase" o "(in Python v3.7)" unittest.TestCase .