You are on page 1of 4

Chapter 4:

Q 1.Write a program to create bar-chart using mathplotlib.

from matplotlib import pyplot as plt


import numpy as np
players=["Vivek","Ram","Raj","Virat"]
cent=[100,65,38,81]
y_pos=np.arange(len(players))
plt.bar(y_pos,cent)
plt.xticks(y_pos,players)
plt.xlabel(players)
plt.show()

Q 2.Define and Explain Local and Global Variable with suitable Example.

g=10
def show( ):
l=20
global g
print("Local Variable= ",l)
print("Global Variable= ",g)
show()
#local variable l=20
#Global variable g=10
Q 3.Write a short note on Function with default Argument.
->As we know that Function is nothing but block of code that is execute by
calling it.
->Python supports default argument in Function,A Default argument that
assumes a default value if value is not passed into Function.

Example:
def disp(name,age=18)
print(“Name: ”,name)
print(“Age: ”,age)
return
disp(name=”Vivek”)

o/p:
Name:Vivek
Age:18

Q 4.What is kwrgs in Python ?


->

Q 5.How to create module in python explain with it example.

->In Python, Modules are simply files with the “.py” extension containing
Python code that can be imported inside another Python Program.

->In simple terms, we can consider a module to be the same as a code library
or a file that contains a set of functions, classes ,list etc that you want to
include in your application.
Writing/Creating Modules

->It means simply creating a file which can contains python definitions and
statements. The file name is the module name with the extension “.py”.To
include module in a file, we use import statement.

Steps:
1)Create a first File as a python program with extension as “.py”.This will be
your module file where we can write functions which perform some task.

2)Now create second file in the same directory called as main file where we
can import the module to the top of the file and call the function.

Example: lets save the code operations.py


def add(a,b):
result=a+b
return result
def sub(a,b):
result=a-b
return result
def mul(a,b):
result=a*b
return result
def div(a,b):
result=a/b
return result
#For Importing use.
import operations as ex

Q 6.Explain Python built in module : random.

->Sometimes we want the computer to pick a random number in a Given range


or pick a random element from list.

->The random module provides Functions to perform such type of


operation,This Function is not accessisble directly,so we need import random
module and then we need to call this function using random static object.
import random as r
print(r.random())

from random import randrange


print([randrange(1,7) for _ in range(10)])

Q 7.Explain concept of Parameter passing to Python Function.

->As we know that Function is nothing but block of code that perform some
action and once defined it can be reused.

->we can also pass the parameters to this Function at the time of calling,this
passed parameter are nothing but actual parameter.

Actual Parameter:

->That means Actual values that are passed to Function,it in form of either
values or variables.

->The datatype of Actual parameter must match with corresponding datatype


of formal parameter.

Formal Parameter

->This are nothing but parameter used in header of function defination,this


parameters are used to receive value from the calling Function.

def cube(x): #formal Parameter


return x*x*x

result=cube(3) #Actual Parameter


print(result)

You might also like