TP Modular V2.0 Python AK
TP Modular V2.0 Python AK
For
Secondary
Classes .
1. Introduction to Python
Exercise
A. 1. d 2. d 3. b 4. a
B. 1. T 2. F 3. F 4. T
C. 1. Prompt 2. Guido van Rossum 3. Variables 4. print( ) 5. line by line
D. 1. The features of Python are:
• Easy to code: It is very easy to write programs in Python as compared to other high-level
programming languages.
• Open-source language: Python is a free and open-source programming language. An
open-source language is one which can be easily improved and distributed by anyone.
• Object-oriented: Python has an object-oriented approach. This means that the programs
are designed using objects and classes that interact with each other.
2. On execution, a Python code is immediately converted into an intermediate form. This is
known as byte code.
3. variable_name = value
E. 1. Four variable naming conventions are:
• A variable name must start with a letter or underscore character.
• A variable name cannot start with a number.
• A variable name can only contain alphanumeric characters (all the letters of the alphabet
and numbers) and underscores ( _ ).
• Variable names are case-sensitive.
2. There are two components of Python IDLE window:
• Menu Bar: The Menu Bar of Python IDLE window is similar to the Menu Bar of other
programs. It has various menus such as File, Edit, Shell, Debug, Options, Window and
Help.
In the lab
1. print("Hello, How are you? I am learning Python language.")
2. print("Education is the most powerful weapon which you can use to
change the world.")
Exercise
A. 1. b 2. a 3. d 4. d 5. d
B. 1. T 2. F 3. T 4. T 5. T
C. 1. Precedence 2. assignment 3. string 4. operators
5. false
D. 1. It is an assignment expression where the value 8 is assigned to the variable a.
2. Variables are memory reference points where we store values which can be accessed or
changed later.
3. A data type specifies the type of value a variable can contains.
4. Precedence of operators determines the order in which the operators are executed.
5. Relational operators are used to compare the value of the two operands and returns True
or False accordingly.
E. 1. The relational operators are described in the following table:
Example
Operator Name Description Output
(x=8 and y=6)
== Equal It checks if the values of two x == y FALSE
operands are equal or not. If yes,
then the condition becomes true.
2. Comments in Python can be used to explain parts of the code. It can also be used to hide
the code as well. Comments enable us to understand the way a program works. In python,
any statement starting with # symbol is known as a comment. Python supports two types
of comments: Single Line Comment and Multi-line Comment.
3. (i) Arithmetic operators are used to perform arithmetic operations between two operands.
(ii) Variables are memory reference points where we store values which can be accessed or
changed later.
(iii) Logical operators are used to evaluate and decide.
4. (i) AND returns true, if both operands are true.
Example (x=2): (x < 5) and (x < 10). Output: TRUE.
OR returns true, if one of the operands is true.
Example (x=2): (x < 5) or (x < 2). Output: TRUE.
(ii) Modulus Operator divides left hand operand by right hand operand and returns remainder.
Example (x=7 and y=3): x % y. Output: 1
Division Operator divides left hand operand by right hand operand.
Example (x=7 and y=3): x / y. Output: 2.33
Python does not have a syntax for multiline comments. To add a multiple line comment,
you could insert a # for each line.
Example: Program: Output:
# printing a string Hello World
#print five names Rachna
print("Hello World") Sambhav
print("Rachna") Akshat
print("Sambhav") Tushar
print("Akshat")
print("Tushar")
F. 1. 2.0 2. 12 3. 4 4. 6
In the lab
Do it yourself.
Exercise
A. 1. c 2. a 3. d 4. b 5. d
B. 1. F 2. F 3. F 4. T 5. T
C. 1. if 2. true 3. false 4. else
D. 1. Decision making in Python is done by called conditional statements which decide the flow
of program execution.
2. Syntax:
if (conditional expression 1):
statement(s)
False
if condition 1 Statement 3
True
False
if condition 2 Statement 2
True
Statement 1
Stop
Periodic Assessment-1
(Based on chapters 1 to 3)
A. Sum of 2 and 3 is 5
B. i = 1
while i <= 25:
if i % 2 == 0:
print(i)
i = i + 1
C. 1. It returns true, if both operands are true.
2. It checks if the values of two operands are equal or not. If the values are not equal, then the
condition becomes true.
Exercise
A. 1. b 2. a and b 3. a 4. c
B. 1. T 2. F 3. T 4. T 5. T
C. 1. while 2. infinite 3. break, continue
D. 1. Looping is the process of executing a set of instructions repeatedly until a specific condition
is met. In Python, loops are created using the for and while statements.
2. for <variable> in <iterator>:
Statements
3. Sometimes, there is a situation when the control of the program needs to be transferred out
of the loop body, even if all the values of the iterations of the loop have not been completed.
For this purpose, jumping statements are used in Python.
Initialisation
False Conditional
Expression
True
Body of for loop
Stop
2. The while statement executes a set of statements repeatedly, until the logical expression
evaluates to true. When the condition becomes false, the control comes out of the loop.
Syntax: Start
End
3. The continue statement is used inside loops. When a continue statement is encountered
inside a loop, control of the program jumps to the beginning of the loop for next iteration,
skipping the execution of rest of the statements of the loop for the current iteration.
The break is a keyword in Python which is used for bringing the program control out of
the loop.
F. 1. 55
2. apple
banana
cherry
3. 2
4
4. 0
1
2
In the lab
Do it yourself.
5. Functions in Python
Exercise
A. 1. a 2. d 3. d 4. c 5. c
B. 1. T 2. T 3. T 4. T 5. T
C. 1. return 2. built-in 3. User-defined 4. def 5. command
D. 1. A function is a block of organised and reusable code used to perform a single or related
action.
2. The features of functions are:
• A program is divided into small modules and each module performs some specific task.
Each module can be called as per the requirement.
• We can call a function as many times as required. This saves the programmer the time
and effort to rewrite the same code again. Therefore, it also reduces the length of the
program.
3. Following are the advantages of functions:
• You can write Python program in logically independent sections.
• Functions provide better modularity for your application and a high degree of code reusing.
• As the program grows larger, functions make it more organized and manageable.
E. 1. A Python function consists of the following components:
• Name of the function: A function name should be unique and easy to correlate with the
task it will perform. We can have functions of the same name with different parameters.
• Arguments: The input given to the functions are referred to as arguments. A function can
or cannot have any arguments.
• Statements: The statements are the executable instructions that the function can perform.
• Return Value: A function may or may not return a value.
2. A function can be called anytime from other functions or from the command prompt after
the definition. For calling a function, we type the function and pass the parameters.
print ("Hello") Body of a function
3. Python functions can be categorised into built-in functions and user-defined functions.
Built-in functions are the predefined functions of Python language. User-defined functions
are created by the user according to the need of the program. Once the user defines a
function, the user can call it in the same way as the built-in functions.
4. We can create a function in the following steps:
• Defining a Function: We use the def keyword to begin the function definition.
• Naming a Function: Provide a meaningful name to your function.
• Supply Parameters: The parameters (separated by commas) are given in the parenthesis
following the name of the function. These are basically the input values we pass to the
function.
• Body of the function: The body of the function contains Python statements that make
our function perform the required task. Syntax of creating a function is:
def < name of the function > (list of parameters)
<body>
F. 1. testing...
passing the value 4
the function returns 4
2. Enter number2
Raise to power3
2 raise to power 3 is 8
In the lab
1. def check_even_odd(num):
if num % 2 == 0:
print(num, "is even.")
else:
print(num, "is odd.")
number = int(input("Enter a number: "))
check_even_odd(number)
Periodic Assessment-2
(Based on chapters 4 & 5)
A. 1. Hello Touchpad
Hello Touchpad
Hello Touchpad
..........
Loop will run infinitely.
2. Name: Taarush
Age: 21
B. sum = 0
i = 1
while i <= 5:
sum += i
i += 1
print("The sum of first five natural numbers is", sum)
C. 1. Parameters are the input values passed to a function when it is called.
2. The pre-defined functions of Python are called built-in function.
3. The loop which never terminates is called infinite loop.
4. The range() function is a built-in function which generates a sequence of numbers.
Test Sheet-1
(Based on chapters 1 to 5)
Section A
A. 1. d 2. a 3. d 4. d 5. d
6. c 7. a and b 8. c 9. a 10. c
B. 1. F 2. T 3. T 4. T 5. T
C. 1. Variables 2. Precedence 3. false 4. infinite 5. def
Section B
A. 1. Features of Python are:
• Easy to code: It is very easy to write programs in Python as compared to other high-level
programming languages.
3. Start
False
if condition 1 Statement 3
True
False
if condition 2 Statement 2
True
Statement 1
Stop
Initialisation
False Conditional
Expression
True
Body of for loop
Stop
5. A function can be called anytime from other functions or from the command prompt after
the definition. For calling a function, we type the function and pass the parameters. For
example:
To call a function.
print ("Hello") Body of a function
Exercise
A. 1. a 2. b 3. d 4. d 5. a
B. 1. F 2. T 3. T 4. F 5. T
C. 1. right 2. escape 3. concatenation 4. lowercase 5. capitalize()
D. 1. A sequence of characters which is enclosed or surrounded by single (' ') or double (" ")
quotes is known as a string.
2. String concatenation operator(+) joins two or more strings into one, whereas the replication
operator(*) is used to repeat the string for a given number of times.
3. Traversing means visiting each element and processing it as required by the program. We
can access the characters of a string one at a time using indexing.
In the lab
1. string = "PYTHON"
for char in string:
print(char)
2. string = "orange education"
uppercase_string = string.upper()
print(uppercase_string)
Exercise
A. 1. b 2. a 3. b 4. b 5. a
B. 1. T 2. F 3. T 4. F 5. T
C. 1. sequence 2. item 3. indexing 4. mutable 5. +
D. 1. A list can be defined as a sequence of objects arranged in an organised form. In a list, each
element or value is called an item.
2. In case if the user tries to access an element from a list beyond the defined range of the list,
then it will give an IndexError.
3. The index of elements of a list starts from 0; which means if a list contains 10 elements then
its index (it is always an integer number) is from 0 to 9.
4. reverse()
5. Negative indexing means the index of -1 refers to the last elements of the list, the index
of -2 refers to the second last element.
E. 1. The index of elements of a list starts from 0; which means if a list contains 10 elements then
its index (it is always an integer number) is from 0 to 9, whereas List slicing refers to a part
of list. In python list slicing is done by using the Slicing operator(:).
2. The replication operator * is used to repeat a list a given number of times.
3. remove( ): Remove first element from the list.
index( ): Returns the index of first element of the list.
count( ): Returns the occurrence (number of times) of a particular element in the list.
4. list = [13, 25, 41, 63, 82]
list.extend([12, 2, 34, 65])
print(list) # Output: [13, 25, 41, 63, 82, 12, 2, 34, 65]
5. max(list): Returns the largest element from the given list.
min(list): Returns the smallest element from the given list.
F. 1. [13, 50, 41, 45, 82]
2. [13, 25, 41, 63, 82, 19]
3. [13, 302, 25, 41, 63, 82]
4. 5
82
13
Periodic Assessment-3
(Based on chapters 6 & 7)
A. def count_vowels(string):
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count += 1
return count
string = "Mango"
print("Number of vowels:", count_vowels(string))
B. numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers.copy():
if num % 2 != 0:
numbers.remove(num)
print(numbers) # Output: [2, 4, 6, 8, 10]
8. Tuple in Python
Exercise
A. 1. d 2. a 3. c 4. c 5. a
B. 1. T 2. T 3. T 4. F
C. 1. comma 2. positive 3. sorted() 4. max() 5. slicing
D. 1. A tuple is a sequence of values enclosed in parentheses and its indices start with 0.
2. The count(a) method returns number of times ‘a’ occurs in the tuple.
3. To create an empty tuple: tup= tuple( )
To create a tuple with single element: tup= (1,)
E. 1. The index or subscript operator [] is used to access the elements of a tuple. The index value
must be an integer as it will result into TypeError.
For example: If a tuple contains 5 elements, then the index value of tuple will varies from
0 to 4.
2. List Tuple
A list is a sequence of multiple values. A tuple is a collection of objects.
List is enclosed in square brackets [ ]. Tuples are enclosed in parenthesis ( ).
Elements of the list can be changed. Elements of the tuple cannot be changed.
List has the variable length. Tuple has the fixed length.
List is mutable. Tuple is immutable.
In the lab
Do it yourself.
9. Dictionary in Python
Exercise
A. 1. a 2. a 3. c 4. c
B. 1. F 2. T 3. T 4. T
C. 1. pop() 2. clear() 3. popitem() 4. del
D. 1. A dictionary in Python is another data type which contains a collection of values in the form
of key value pairs.
2. To access the elements of a dictionary, you need the key defined in the key:value pairs. You
can also use the get( ) method for fetching the value of a particular key of a dictionary.
Syntax to access a dictionary:
<dictionary-name>[<key>]
3. You can easily add or modify the elements of a dictionary. If the item is present in the
dictionary, then you can change its value by providing new value to its key term.
E. 1. To create a dictionary in Python use the following syntax:
d = {key1: value1, key2 : value2, ... }
Example:
d2 = {ꞌwidthꞌ: 8.5, ꞌheightꞌ: 11}
d3 = {1: ꞌREDꞌ, 2: ꞌGREENꞌ, 3: ꞌBLUEꞌ, }
In the lab
Do it yourself.
Exercise
A. 1. a 2. a 3. a 4. a 5. c
B. 1. F 2. T 3. F 4. T 5. F
C. 1. hybrid 2. Mobile 3. Android 4. gaming 5. install
D. 1. An app is a software program primarily developed for hand-held smart devices such as
mobile and tablet.
2. Web apps are actually web applications which give a user with experience similar to native
apps. These apps are not deployed on the app store. Hence, you need an extra app called
browser to access these apps on your mobile device.
3. Native apps are the type of Mobile apps. These are platform dependent which means that
these apps are primarily developed for a specific platform.
4. Built-in Blocks, Component Blocks and Workspace.
E. 1. a. Gaming Apps: Today’s most popular category of mobile apps is gaming apps which
shared more than 24% area of the app store. The most commonly used gaming apps are
Clash of Clans, Candy Crush Saga, and Angry Birds.
b. Productivity Apps: Productivity apps, also known as business apps used by businessmen
to perform several complex tasks. The most commonly used productive apps are Google
Calendar, Evernote and Dropbox.
c. Entertainment Apps: Entertainment apps are developed to entertain the people. The
most commonly used entertainment apps are Netflix, Talking Tom and YouTube.
In the lab
Do it yourself.
Periodic Assessment-4
(Based on chapters 8 to 10)
A. 1. App 2. Play Store 3. iOS 4. Web App
5. App Store 6. Hybrid App 7. Gaming Apps 8. Educational Apps
9. Social Networking Apps 10. Web App
B. 1. T 2. F 3. T 4. F 5. T
C. 1. (10, 20, 30, 40, 50, 60, 70)
2. (10, 20, 30, 10, 20, 30)
(60, 70, 60, 70, 60, 70)
Test Sheet-2
(Based on chapters 6 to 10)
Section A
A. 1. b 2. a 3. b 4. a 5. a
6. c 7. a 8. c 9. a 10. c
B. 1. F 2. F 3. T 4. T 5. F 6. F
7. F 8. T
C. 1. escape 2. item 3. positive 4. clear() 5. Android 6. capitalize()
7. + 8. max()
Section B
A. 1. A sequence of characters which is enclosed or surrounded by single (' ') or double (" ") quotes
is known as a string.