Xii CS Chapter 7 & 8 - Notes
Xii CS Chapter 7 & 8 - Notes
AMALORPAVAM
HIGHER SECONDARY SCHOOL
PUDUCHERRY
7 • PYTHON FUNCTIONS
Syntax: Syntax:
math.ceil (x) math.floor (x)
import math import math
x= 26.7 x=26.7
y= -26.7 y=-26.7
z= -23.2 z=-23.2
print (math.ceil (x)) print (math.floor (x))
print (math.ceil (y)) print (math.floor (y))
print (math.ceil (z)) print (math.floor (z))
Output: Output:
27 26
-26 -27
-23 -24
5. Write a Python code to check whether a given year is leap year or not.
year = int(input("Enter a year: "))
if(year%4==0 and year%100!=0) or (year%400==0):
print("The year is a leap year")
else:
print("The year is not a leap year")
Output 1:
Enter a year: 1900
The year is not a leap year
>>>
Output 2:
Enter a year: 1996
The year is a leap year
>>>
Output 3:
Enter a year: 2000
The year is a leap year
6. What is composition in functions?
The value returned by a function may be used as an argument for another function in
a nested manner. This is called composition.
If we wish to take a numeric value or an expression as a input from the user, we take the input
string from the user using the function input() and apply eval() function to evaluate its value, for
example:
n2 = eval (input ("Enter an arithmetic expression: "))
Output:
Enter an arithmetic expression: 12.0+13.0 * 2
38.0
>>>
7. How recursive function works?
1. Recursive function is called by some external code.
2. If the base condition is met then the program gives meaningful output and exits.
3. Otherwise, function does some required processing and then calls itself to continue recursion.
8. What are the points to be noted while defining a function?
Function blocks begin with the keyword “def” followed by function name and parenthesis ().
Any input parameters or arguments should be placed within these parentheses when you
define a function.
The code block always comes after a colon (:) and is indented.
The statement “return [expression]” exits a function, optionally passing back an expression
to the caller. A “return” with no arguments is the same as return None.
Part - IV
Answer the following questions: (5 Marks)
1. Explain the different types of function with an example.
Basically, we can divide functions into four types. They are:
User-defined Functions
Built-in Functions
Lambda Functions
Recursion Functions
1. User-defined Functions :
Functions defined by the users themselves.
Function blocks begin with the keyword “def” followed by function name and parenthesis ().
The statement “return [expression]” exits a function,
Example:
def hello():
print (“hello - Python”)
return
2. Built-in functions:
Functions that are inbuilt with in Python.
There are many built-in functions that comes with the language python (for instance, the
print() function)
Example:
ord ( ) - Returns the ASCII value for the given Unicode character. This function is inverse of chr()
function.
chr ( )- Returns the Unicode character for the given ASCII value. This function is inverse of
ord()function.
3. Anonymous Functions:
In Python, anonymous function is a function that is defined without a name.
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 as lambda functions.
Syntax:
lambda [argument(s)] :expression
Example:
sum = lambda arg1, arg2: arg1 + arg2
print ('The Sum is :', sum(30,40))
print ('The Sum is :', sum(-30,40))
Output:
The Sum is : 70
The Sum is : 10
Recursive functions:
When a function calls itself is known as recursion.
Example :
def fact(n):
if n == 0:
return 1
else:
return n * fact (n-1)
print (fact (0))
print (fact (5))
Output:
1
120
2. Explain the scope of variables with an example.
Scope of variable refers to the part of the program, where it is accessible, i.e., area where you can
refer (use) it. We can say that scope holds the current set of variables and their values.
There are two types of scopes - local scope and global scope.
Local Scope:
A variable declared inside the function's body or in the local scope is known as localvariable.
Rules of local variable:
• A variable with local scope can be accessed only within the function/block that it is created in.
• When a variable is created inside the function/block, the variable becomes local to it.
• A local variable only exists while the function is executing.
• The formal arguments are also local to function.
Example : Create a Local Variable
def loc():
y=0 # local scope
print(y)
loc()
Output:
0
CHAPTER - 8
STRINGS AND STRING
MANIPULATION
PART-I
Choose the best answer: (1 Mark)
1. Which of the following is the output of the following python code?
str1="TamilNadu"
print(str1[::-1])
(a) Tamilnadu (b) Tmlau (c) udanlimaT (d) udaNlimaT
2. What will be the output of the following code?
str1 = "Chennai Schools"
str1[7] = "-"
(a) Chennai-Schools (b) Chenna-School (c) Type error (d) Chennai
3. Which of the following operator is used for concatenation?
(a) + (b) & (c) * (d) =
4. Defining strings within triple quotes allows creating:
(a) Single line Strings (b) Multiline Strings
(c) Double line Strings (d) Multiple Strings
5. Strings in python:
(a) Changeable (b) Mutable (c) Immutable (d) flexible
6. Which of the following is the slicing operator?
(a) { } (b) [ ] (c) <> (d) ( )
7. What is stride?
(a) index value of slide operation (b) first argument of slice operation
(c) second argument of slice operation (d) third argument of slice operation
8. Which of the following formatting character is used to print exponential notation in upper case?
(a) %e (b) %E (c) %g (d) %n
9. Which of the following is used as placeholders or replacement fields which get replaced along with
format( ) function?
(a) { } (b) <> (c) ++ (d) ^^
10. The subscript of a string may be:
(a) Positive (b) Negative (c) Both (a) and (b) (d) Either (a) or (b)
Part -II
Answer the following questions (2 Marks)
1. What is String?
String is a sequence of Unicode characters that may be a combination of letters, numbers, or
special symbols enclosed within single, double or even triple quotes.
Example:
‘Welcome to learning Python’
“Welcome to learning Python”
‘‘‘Welcome to learning Python ’’’
2. Do you modify a string in Python?
No, Strings in python are immutable. That means, once you define a string modifications or
deletion is not allowed. If you want to modify the string, a new string value can be assign to the
existing string variable.
3. How will you delete a string in Python?
Python will not allow deleting a particular character in a string.
Whereas you can remove entire string variable using del command.
Example:
>>> str1="How about you"
>>> print (str1)
How about you
>>> del str1
>>> print (str1)
NameError: name 'str1' is not defined
4. What will be the output of the following python code?
str1 = “School”
print(str1*3)
Output:
SchoolSchoolSchool
5. What is slicing?
Slice is a substring of a main string. A substring can be taken from the original string by
using [ ] operator and index or subscript values. Thus, [ ] is also known as slicing operator.
Using slice operator, you have to slice one or more substrings from a main string.
General format of slice operation:
str[start:end]
Where start is the beginning index and end is the last index value of a character in the string.
Example:
>>> str1="THIRUKKURAL"
>>> print (str1[0:5])
THIRU
Part -III
Answer the following questions (3 Marks)
1. Write a Python program to display the given pattern
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C
Python Program:
str=”COMPUTER”
index = 0
for i in str:
print(str[:index+1])
index+=1
2. Write a short about the followings with suitable example:
(a) capitalize( ) (b) swapcase( )
(a)capitalize():
Syntax Description Example
capitalize( ) Used to capitalize the first character >>> city=”chennai”
of the string >>> print(city.capitalize())
Chennai
(b) swapcase( ) :
Syntax Description Example
swapcase( ) It will change case of >>> str1="tAmiL NaDu"
every character to its >>> print(str1.swapcase())
opposite case vice-versa. TaMIl nAdU