Python Unit 2
Python Unit 2
Python Unit 2
Ans. In all programming and scripting language, a function is a block of program statements
which can be used repetitively in a program. It saves the time of a developer. In Python
concept of function is same as in other languages. There are some built-in functions which
are part of Python.
Ans. Lambda functions are similar to user-defined functions but without a name. They're
commonly referred to as anonymous functions. Lambda functions are efficient whenever
you want to create a function that will only contain simple expressions – that is, expressions
that are usually a single line of a statement.
For example, if we also want to check if num1 is greater than num2, if num2 is greater
than num1, or else (meaning if they are equal), we can use the following lambda expression:
So if our lambda expression finds that num1 > num2, it will return num1. If this condition is
false, it'll move on to the else statement.
plt.bar(x_axis, y_axis)
plt.title('title name')
plt.xlabel('x_axis name')
plt.ylabel('y_axis name')
plt.show()
Global Variable:-
Global variables are the types of variables that are declared outside of every function of the
program. The global variable, in contrast to local variables, is accessible by all functions in a
program. Global variables are not very reliable because any function in the program can
alter their value.
E. Define class and object in Python
Ans. Class:- A class is a code template for creating objects. Objects have member variables
and have behaviour associated with them. In python a class is created by the keyword class.
An object is created using the constructor of the class. This object will then be called the
instance of the class. In Python we create instances in the following manner.
2. Set Intersection:-
The intersection of two sets is the set of all the common elements of both the sets. You can
use the intersection() method of the & operator to find the intersection of a Python set.
Example:-
>>> first_set = {1, 2, 3, 4, 5, 6}
>>> second_set = {4, 5, 6, 7, 8, 9}
>>> first_set.intersection(second_set)
{4, 5, 6}
>>>
>>> first_set & second_set # using the & operator
{4, 5, 6}
3. Set Difference
The difference between two sets is the set of all the elements in first set that are not
present in the second set. You would use the difference() method or the - operator to
achieve this in Python.
Example:-
>>> first_set = {1, 2, 3, 4, 5, 6}
>>> second_set = {4, 5, 6, 7, 8, 9}
>>> first_set.difference(second_set)
{1, 2, 3}
>>>
>>> first_set - second_set # using the - operator
{1, 2, 3}
>>>
>>> second_set - first_set
{8, 9, 7}
s = "10010"
c = int(s,2)
print (c)
e = float(s)
print (e)
2) ord()&hex(),oct():-
ord(): This function is used to convert a character to an integer.
hex(): This function is to convert an integer to a hexadecimal string.
oct(): This function is to convert an integer to an octal string.
s = '4'
c = ord(s)
print (c)
c = hex(56)
print (c)
c = oct(56)
print (c)
3) tuple()&set(),list():-
tuple(): This function is used to convert to a tuple.
set(): This function returns the type after converting to set.
list(): This function is used to convert any data.
s = 'geeks'
c = tuple(s)
print (c)
# printing string converting to set
c = set(s)
print (c)
c = list(s)
print (c)
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
Output
3
class GeekforGeeks:
# default constructor
def _init_(self):
self.geek = "GeekforGeeks"
def print_Geek(self):
print(self.geek)
obj = GeekforGeeks()
Output:-
GeekforGeeks
class Addition:
first = 0
second = 0
answer = 0
# parameterized constructor
self.first = f
self.second = s
def display(self):
def calculate(self):