PPS Unit 5
PPS Unit 5
MODEL ANSWERSHEET-2022
Example : 2-Mark
#function definition
def hello_world():
print("hello world")
# function calling
hello_world()
Output:
hello world
Types of Argument :
1. Default Arguments
2. Keyword Arguments
3. Positional Arguments
4. Arbitrary Positional Arguments
5. Arbitrary Keyword Arguments
Example : 2-Mark
def my_func():
x = 10 #Local Variable
print("Value inside function:",x)
x = 20 #Global Variable
my_func()
print("Value outside function:",x)
Output :
Value inside function: 10
Value outside function: 20
• While normal functions are defined using the def keyword in Python, anonymous
functions are defined using the lambda keyword.
• Hence, anonymous functions are also called lambda functions. 1-Mark
Syntax
lambda arguments : expression
Example : 2-Mark
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
Output : 15
if(num%2==0):
print(num," Is an even")
else:
print(num," is an odd")
find_Evenodd(num);//function call
Output :
Enter a number for check odd or even: 349
349 is an odd
4-Mark
Comments Docstrings
A hash symbol (#) is used to mention the They are written between double or triple
initiation of a comment quotes
These are special strings that are used for
These are basically statements that are used for
providing documentation in Python
describing what a particular line of code means
programs
Comments are not visible after the program has You can see the docstrings using the
been executed __doc__ attribute
1. in Operator : 2-Mark
To check if a certain phrase or character is present in a string, we can use the
keyword in.
Example :
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
Output : True
for i in range(1,11):
print("\n\nMULTIPLICATION TABLE FOR %d\n" %(i))
for j in range(1,11):
print("%-5d X %5d = %5d" % (i, j, i*j))
2. Multiplication :
To repeat a string in Python, We use the asterisk operator ” * ” The asterisk. is used
to repeat a string n (number) of times. Which is given by the integer value ” n ” and
creates a new string value. It uses two parameters for an operation: the integer value
and the other is String.
Example : 2-Mark
# Python String Operations
str1 = 'Hello'
str2 ='World!'
# using +
print('str1 + str2 = ', str1 + str2) //concatenation
# using *
print('str1 * 3 =', str1 * 3) //Multiplication
Output :
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
2. Find : The find() method returns the index of first occurrence of the
substring (if found). If not found, it returns -1. 2-Mark
Example :
message = 'Python is a fun programming language'
# Output: 12
Example :
string = "MetIoe"
# prints after swapping all cases
print(string.swapcase())
Output : mETiOE