# O Level Exam

#### VarArgs Parameters (Variable-Length Arguments)

In some instances you might need to pass more arguments than have already been specified. Going back to the function to redefine it can be a tedious process. Variable-Length arguments can be used instead. These are not specified in the function’s definition and an asterisk (*) is used to define such arguments.

Lets see what happens when we pass more than 3 arguments in the sum() function.

Example:

`def sum(x,y,z):	print("sum of three nos :",x+y+z)sum(5,10,15,20,25)`

When the above code is executed, it produces the following result :

TypeError: sum() takes 3 positional arguments but 5 were given

Syntax - Variable-Length Arguments

`def function_name(*args):	function_body	return_statement`

Example:

`def printnos (*nos):	for n in nos:	print(n)	return# now invoking the printnos() functionprint ('Printing two values')printnos (1,2)print ('Printing three values')printnos (10,20,30)`

Output:

Printing two values

1

2

Printing three values

10

20

30

Qus: What is a function? How is it useful?

A function is a named block in a program which has some certain working. Whenever you call this it performs all its working. It reduces the complexity and reduces the labour of writing same code again and again.

Qus: What is an argument? Give an example of one.

Ans : An argument is the requirement of a function to perform its working. For ex- pow(x,y), here x and y are the arguments which are required to pass to pow() for its working.

Qus. What is a string slice? How is it useful?

Ans: String Slice is a part of a string containing some contiguous characters from the string. It is accessed from the string by providing a range in ―[ ]‖ brackets i.e. S [n:m]. Python returns all the characters at indices n, n+1, n+2 . . . m-1 e.g. 'Barabanki'.[4:7] will return 'ban'.

Qus. Which functions would you chose to use to remove leading and trailing white spaces from a given string?

Ans: Python String strip() function will remove leading and trailing white spaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

Qus. Suggest appropriate functions for the following tasks –

(a) To check whether the string contains digits.

(b) To find the occurrence a string within another string.

(c) To convert the first letter of a string to upper case.

(d) To convert all the letters of a string to upper case.

(f) To check whether all the letters of the string are in capital letters.

(g) to remove all the white spaces from the beginning of a string.

Ans.  (a) isalnum() (b) find() (c) capitalize()   (d) upper() (f) isupper() (g) lstrip()

#### Programs

Write a python script that traverses through an input string and prints its characters in different lines – two characters per line.

WAP to print the number of occurrences of a substring into a line.

WAP to check the given string is palindrome or not.

Python program to convert decimal into other number systems

Write a Python program to show the application of Global variable.

Write a Python program to create a function for factorial value

Write program to find the sum of factorial upto n terms

Write a python program to create a function which reverse the given number

Write a Python program to input any number and check number is Palindrome or Not using Function

Write a Python program to input any number from user and find its sum of digit

Write a Python function to calculate the factorial value of given number using recursion.