Ch-3 Brief overviw of Python - 2023-24
Ch-3 Brief overviw of Python - 2023-24
1. What is Python?
Ans. Python is a General Purpose High Level programming Language which
can be used for both scientific and non-scientific programming.
It can be used for -
• Console Application
• Desktop Application
• Web Application
• Mobile Application
• AI and Machine Learning
• IOT Application (Internet of Things)
2. Who create and develop Python?
Ans. Python is a very popular and easy to learn programming language, created
by Guido van Rossum in 1991.
3. Write the features of Python.
Ans. Following are the features of Python -
Python is a high level language. It is a free and open source
language.
It is an interpreted language, as Python programs are executed by
an interpreter.
Python programs are easy to understand as they have a clearly
defined syntax and relatively simple structure.
Python is case-sensitive. For example, NUMBER and number are
not same in Python.
Python is portable and platform independent, means it can run
on various operating systems and hardware platforms.
Python has a rich library of predefined functions.
Python is also helpful in web development. Many popular web
services and applications are built using Python.
Python uses indentation for blocks and nested blocks
4. Name some popular companies who use Python.
Ans. In operations of Google search engine, YouTube, etc.
Bit Torrent peer to peer file sharing is written using Python
Intel, Cisco, HP, IBM, etc use Python for hardware testing.
Maya provides a Python scripting API
i–Robot uses Python to develop commercial Robot.
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
NASA and others use Python for their scientific programming task.
5. What is Program?
Ans. An ordered set of instructions or commands to be executed by a computer is
called a PROGRAM.
6. What is Programming Language?
Ans. The language used to specify those set of instructions to the computer is
called a PROGRAMMING LANGUAGE. For example Python, C, C++,
Java, etc.
7. What is Machine Language or Low-Level Language?
Ans. Computers understand the language of 0s and 1s which is called
MACHINE LANGUAGE or Low Level Language.
8. What is High-Level Language or Source Code?
Ans. A program written in a high-level language is called Source Code. High-
Level language like Python, C++, Visual Basic, PHP, Java that are easier to
manage by humans but are not directly understood by the computer.
Language translators like compilers and interpreters are needed to
translate the Source Code into Machine Language.
9. Which language translator used by Python?
Ans. Python uses an interpreter to convert its instructions into machine
language, so that it can be understood by the computer.
16. In which mode we get result immediately after executing the command?
Ans. Interactive mode
17. What is Keywords?
Ans. Keywords are the words that convey a special meaning to the language
compiler/interpreter.
These are reserved for special purpose and must not be used as normal
identifier names.
They are the words used by Python interpreter to recognize the structure of
program.
18. What is an Identifiers in programming language?
Ans. In programming languages, identifiers are names used to identify a
variable, function, or other entities in a program.
19. Write the rules for naming an identifier in Python.
Ans. The rules for naming an identifier in Python are as follows:
a. The name should begin with an uppercase or a lowercase alphabet or an
underscore sign (_). This may be followed by any combination of
characters a-z, A-Z, 0-9 or underscore (_). Thus, an identifier cannot
start with a digit.
b. It can be of any length. However, it is preferred to keep it short and
meaningful.
c. It should not be a keyword or reserved word.
d. We cannot use special symbols like !, @, #, $, %, etc. in identifiers.
e. Python is a case-sensitive programming language as it treats
UPPERCASE and lowercase characters differently. Thus, Rollnumber
and rollnumber are two different identifiers in Python.
Example:
OR,
Keyword Identifier
a) Keywords are the pre- a) An identifier is a different term or
defined and specific reserved name given to a variable, function
words, which hold special or other entities in a program.
meaning.
b) It always starts with a b) First character can be a uppercase,
lowercase letter. lowercase letter or underscore.
c) A keyword should be in c) An identifier can be in upper case
lower case. or lower case.
d) A keyword contains only d) An identifier can consist of
alphabetical characters. alphabetical characters, digits and
underscores.
e) No special symbol, e) No punctuation or special symbol
punctuation is used. except ‗underscore‘ is used.
f) Examples of keywords are: f) Examples of identifiers are: Test,
int, if, elif, else, while, for, count1, high_speed, etc.
Chapter 2 : Brief overview of Python Page 4 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
break etc.
28. Can we perform arithmetic operation on string value? Justify your answer.
Ans. We cannot perform arithmetic operations on strings, even when the string
contains a numeric value. Example –
+ operator is used to concatenate or join :
A=”4”
B=”8”
C=A+B
Output-
“48”
* operator is used to repetition the value :
A=”4”
A*3
Output-
“444”
29. What is List Data type?
Ans. List is a sequence of items contain mixed data types separated by
commas and items are enclosed in square brackets [ ]. Example-
#To create a list – L1
>>> L1 = [5,”Anuj Singh”,”M”,“Jorhat-3”]
#create a tuple T1
>>> T1 = (10, 20, "Apple", 3.4, 'a')
#print the elements of the tuple T1
>>> print(T1)
(10, 20, "Apple", 3.4, 'a')
31. What is Set data type in Python?
Ans. Set is an unordered collection of items separated by commas and the
items are enclosed in curly brackets { }.
A set is similar to list, except that it cannot have duplicate entries.
Once created, elements of a set cannot be changed.
Example of Set in Python -
#create a set
>>> set1 = {10,20,3.14,"New Delhi"}
>>> print(type(set1))
<class 'set'>
>>> print(set1)
{10, 20, 3.14, "New Delhi"}
#duplicate elements are not included in set
>>> set2 = {1,2,1,3}
>>> print(set2)
{1, 2, 3}
32. What is None data type in Python?
Ans. None is a special data type with a single value.
It is used to signify the absence of value in a situation.
None supports no special operations, and it is neither False nor 0 (zero).
Example of None in Python –
>>> a = None
>>> print(type(a))
<class 'NoneType'>
>>> print(a)
None
Keys are usually of string type and their values can be of any data type.
35. How to access value in dictionary?
Ans. In order to access any value in the dictionary, we have to specify its key
in square brackets [ ].
Example of Dictionary in Python -
#create a dictionary – dict1
>>> dict1 = {'Fruit':'Apple', 'Price':120}
>>> dict1
{'Fruit': 'Apple', 'Price': 120}
#getting value by specifying a key
>>> dict1['Price']
120
36. What you understand the term Mutable and Immutable?
Ans. Variables whose values can be changed after they are created and
assigned are called Mutable.
Variables whose values cannot be changed after they are created and
assigned are called Immutable.
37. Write different classification of mutable and immutable data type in Python.
Ans. Python data types can be classified under mutable and immutable are -
Immutable Data Type – Integer, Float, Complex, Boolean, String,
Tuple, Set
Mutable Data Type – Lists , Dictionary
38. What is Comments?
Ans. Comments are used to add a remark or a note in the source code.
Comments are not executed by interpreter.
39. What is the purpose of adding comments in the program?
Ans. They are added with the purpose of making the source code easier for
humans to understand. They are used primarily to document the meaning
and purpose of source code.
40. How to add single line comments in the python program?
Ans. In Python, a single line comment starts with # (hash sign). Everything
following the # till the end of that line is treated as a comment and the
interpreter simply ignores it while executing the statement.
Example :
# Calculating area of a square
>>> area = side**2
Or,
>>>area= side**2 #calculating area of a square
41. How to add multiline comments in the python program?
Ans. In Python, a multi line comment starts with ‟‟‟ or ””” (triple single or
double quotes). Example -
‟‟‟
This is a comment
written in
more than just one line
‟‟‟
print("Hello, World!")
42. What do you understand by an Operator and an Expression?
Ans. An Operator is used to perform specific mathematical or logical
operation on values. The values that the operator works on are called
OPERANDS.
Operators when applied on operands form an EXPRESSION.
Example :
% Modulus = >>>9%2
returns 1
remainder
** Exponent = >>>9**2
perform raise to 81
power
45. What is Relational Operators?
Ans. Relational Operators are used to compare the values and return Boolean
values i.e. True or False
Operators Description Example 1 Example 2
>>>10>=10 >>>‟Hello‟>=
>= Greater than
True “Goodbye‟
or equal to ,
>>>10>=12 True
return true if a False >>>'Goodbye' >=
is greater 'Hello'
than b or a is False
equals to b
>>>10 != 11 >>>‟Hello‟ !=
!= Not equal,
True “HELLO”
return true if a >>>10 != 10 True
is not equals False >>> “Hello” !=
to b “Hello”
False
46. What is an Assignment Operator?
Ans. Assignment Operator combines the effect of arithmetic and assignment
operator.
Operators Description Example Output
a=2 2
= Assigns values from right side b=a
operands to left side operand print(b)
a=2
Add and assigns the result to left 7
+= b=5
operand. a+=b
Note :
a+=b is same as a = a+b
a=5
Divides and assigns the result to left 2.5
/= b=2
operand. a/=b
Note :
a/=b is same as a = a/b
a=5
Multiply and assigns the result to left 10
*= operand.
b=2
a*=b
Note :
a*=b is same as a = a*b
a=5
Subtracts and assigns the result to 3
-= leftoperand.
b=2
a-=b
Note :
a-=b is same as a = a-b
a=5
Modulus to find the remainder and 1
%= assigns the result to leftoperand.
b=2
a%=b
Note :
a%=b is same as a = a%b
Perform floor division on operators a=5 2
//= and assigns the result to leftoperand. b=2
Note : a//=b
a//=b is same as a = a//b
AND Operator-
Condition 1 Condition 2 Condition 1 and Condition 2
OR Operator-
Condition 1 Condition 2 Condition 1 or Condition 2
NOT Operator-
Condition Not Condition
False True
True False
Ans.
Note :
• Parenthesis can be used to override the precedence of operators. The
expression within () is evaluated first.
• For operators with equal precedence, the expression is evaluated from
left to right.
52. Evaluate the following expression : 10+20*40
Ans. 810
53. Evaluate the following expression : (10+20)*40
Ans. 1200
54. Evaluate the following expression :
15.0 / 4.0 + ( 8 + 3.0 )
Ans. 14.75
55. Evaluate the following expression : 10-20+40
Ans. 30
Since (-) and (+) have equal precedence, evaluate from left to right
59. Name and define the functions to accepting and displaying value in Python.
Ans. In Python, input() function for taking values entered by input device
such as a keyboard. The input() function prompts user to enter data. It
accepts all user input (whether alphabets, numbers or special character)
as string.
Python uses the print() function to output data to standard output
device — the screen. The function print() evaluates the expression before
displaying it on the screen. To print more than one item on a single line,
comma (,) may be used.
60. What is Data conversion? Write two way to convert data type.
Ans. Data conversion - As and when required, we can change the data type of a
variable in Python from one type to another.
Two ways to convert data type conversion-
• Explicit Conversion
• Implicit Conversion
61. Explain with example of Explicit Conversion.
Ans. Explicit conversion, also called Type Casting happens when data type
conversion takes place because the programmer forced it in the program.
Syntax:
(new_data_type) (expression)
Various conversion methods are-
• int(x) - Converts x to an integer
• float(x) - Converts x to a floating-point number
• str(x) - Converts x to a string representation
• chr(x) - Converts x to a character
• unichr(x) - Converts x to a Unicode character
Example 1: Convert data type - integer to float
num1 = 4
num2 = 5
num3 = num1 + num2
print(num3)
print(type(num3))
num4 = float(num1 + num2)
print(num4)
print(type(num4))
OUTPUT:
9
<class 'int'>
9.0
<class 'float'>
OUTPUT:
10.1
<class „float'>
10
<class „int'>
Example 3: Convert data type – Number to String
num1 = 4.5
num2 = 5.6
num3 = num1 + num2
print(num3)
print(“Sum = “ + str(num3))
OUTPUT:
10.1
Chapter 2 : Brief overview of Python Page 16 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
<class „float'>
Sum = 10.1
Example 4:
num1 = „4‟
num2 = „5‟
num3 = num1 + num2
print(“Sum = “ + num3)
num3 = int(num1) + int(num2)
print(“Sum = “ + str(num3))
OUTPUT:
Sum = „45‟ #Wrong output – Concatenate the String numeric
values
Sum = 9 #Correct output
62. What is the purpose of type() function?
Ans. To determine the data type of the variable using built-in function type().
>>> age = 13
>>> type(age)
<class 'int'>
>>> temp = -2.0
>>> type(temp)
<class 'float'>
63. Explain with example of Implicit Conversion.
Ans. Implicit conversion, also known as Coercion, happens when data type
conversion is done automatically by Python and is not instructed by the
programmer.
Example 5: To Type Promotion that allows performing operations by converting
data into a wider-sized data type without any loss of information.
num1 = 4.5
num2 = 5
num3 = num1 + num2
print(num3)
type(num3)
OUTPUT:
9.5
<class „float„>
64. What is bugs and debugging?
Ans. The process of identifying and removing mistakes done by programmer, also
known as BUGS or Errors, and to rectify those errors from a program is
called Debugging.
Errors occurring in programs can be categorised as:
• Syntax errors
• Logical errors
• Runtime errors
65. Write a short notes on Syntax error, Logical errors and Runtime errors.
Chapter 2 : Brief overview of Python Page 17 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
Ans. Syntax Error - Python has its own rules that determine its syntax. The
interpreter interprets the statements only if it is syntactically correct. If
any Syntax Error is present, the interpreter shows error message(s)
and stops the execution there.
Logical Error - A Logical Error is a bug in the program that causes it
to behave incorrectly. A Logical Error produces an undesired output
but without abrupt termination of the execution of the program. Since
the program interprets successfully even when logical errors are
present in it, it is sometimes difficult to identify these errors. The only
evidence to the existence of logical errors is the wrong output.
Logical errors are also called Semantic Errors as they occur when the
meaning of the program (its Semantics) is not correct.
Runtime Error - A Runtime Error causes abnormal termination of
program while it is executing. Runtime error is when the Statement is
correct syntactically, but the interpreter cannot execute it. Runtime
errors do not appear until after the program starts running or executing.
The process of identifying and removing logical errors and runtime errors
is called DEBUGGING.
66. What is Flow of Control? Write the three control structures in Python.
Ans. The order of execution of the statements in a program is known as Flow of
Control. The flow of control can be implemented using control structures.
Three Control Structures are-
Sequential Statement
Selection Statements
Iteration Statements
i) Sequence refers to the normal flow of control in a program.
Example 6: Write a program to accept two integer values and display their
sum.
OUTPUT:
Enter Number : 22
Number is more than 10.
a=b=c=0
a=int(input("Enter First Number :"))
b=int(input("Enter Second Number :"))
c=int(input("Enter Third Number :"))
max=a
if b>max:
max=b
if c>max:
max=c
print("Largest number is ",max)
Output:
Enter First Number : 3
Enter Second Number : 7
Enter Third Number : 5
Largest number is 7
OUTPUT:
Enter Number : 2
2 is less than 10.
Example 10: Write a program to accept a number and check if the number
is EVEN or ODD and display appropriate message.
Method 1:
num=int(input("Enter Number :"))
reminder=num%2
if reminder==0:
print(num,"is EVEN number.")
else:
print(num,"is ODD number.")
Chapter 2 : Brief overview of Python Page 20 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
Method 2:
num=int(input("Enter Number :"))
if num%2==0:
print(num,"is EVEN number.")
else:
print(num,"is ODD number.")
OUTPUT:
Enter Number : 2
2 is EVEN number.
Example 11: Write a program to accept the age of a person and check
whether the person is eligible to cast vote or not and display appropriate
message.
OUTPUT:
Enter Age : 15
The person is not Eligible.
Example 12: Write a program to accept the marks of a subject and find if
the marks is valid within a range of 0-100 and display appropriate message.
OUTPUT:
Enter marks : 105
105 marks is a INVALID.
Example 13: Write a program to accept a character to determine whether it
is a digit or character and display appropriate message.
ch=input("Enter a character:")
if ch>='0' and ch<='9':
print(ch, "is a DIGIT.")
else:
print(ch, "is a CHARACTER.")
OUTPUT:
Enter a Character : P
P is a CHARACTER.
OUTPUT:
Enter a Number : 5
Number is positive.
Example 15: Write a program to accept the Sales amount. The Discount is
given based on the following criteria-
>=3000 18 %
>=2000 15 %
< 2000 10 %
OUTPUT:
Enter the Sales amount:2500
Chapter 2 : Brief overview of Python Page 22 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
OUTPUT:
Red-R, Orange-O, Green-G
Enter the color : G
Go!
Example 17: WAP to input Percentage Marks of a students, and find the
grade as per following criterion:
Marks Grade
>=90 A
75-90 B
60-75 C
Below 60 D
Method 1:
per=float(input("Enter Percentage :"))
if per>=90:
grade='A'
elif per>=75 and per<90:
grade='B'
elif per>=60 and per<75:
grade='C'
else:
grade='D„
print("Percentage :",per)
print("Grade :",grade)
OUTPUT:
Enter Percentage :95.5
Percentage : 95.5
Grade : A
OUTPUT:
Enter Percentage :85.5
Percentage : 85.5
Grade : B
Example 18: Write a Python program to check a triangle is equilateral,
isoscele‘s or scalene.
Note :
• An Equilateral triangle is a triangle in which all three sides are
equal.
• A Scalene triangle is a triangle that has three unequal sides.
• An Isosceles triangle is a triangle with (at least) two equal sides.
OUTPUT:
x: 6
y: 8
z: 12
Scalene triangle
iii) Iteration construct means repetition of a set of statements depending
upon a condition-test. Iteration is also called repetition or looping
construct.
Python provides two kinds of loops:
while loop
for loop
with sequence
Syntax :
for <var> in <sequence>:
statement to repeat
Example 19: Write a Python program generates and print number from 1
to 5 using while loop.
i=1 OUTPUT:
while (i <=5): 1
print (i) 2
i = i+1 3
4
5
OUTPUT:
Input some integers to calculate their sum and average.
Input 0 to exit.
8
6
5
0
Sum of the above numbers are: 19.0
Average : 6.333333333333333
break statement - With the break statement we can stop the loop even if
the while condition is true.
continue statement - With the continue statement we can stop the
current iteration, and continue with the next.
Example 21: Write a Python program to print numbers from 1 to 6 and exit
the loop when the value is 3.
i = 1 OUTPUT:
while i < 6: 1
print(i) 2
if (i == 3): 3
break
i += 1
Example 22: Write a Python program to print numbers from 1 to 6 and
Continue to the next iteration by skipping the value 3 using continue
statement.
i = 0 OUTPUT:
while i < 6: 1
i += 1 2
if i == 3: 4
continue 5
print(i) 6
Example 23: Write a Python program to input any number to find the sum
of its digits.
OUTPUT:
456
Sum of the digits: 15
Example 24: Write a Python program to input any number and display its
reverse.
OUTPUT:
456
Reverse: 654
Example 25: Write a Python program to calculate the factorial of a number.
The factorial of a number is the product of all the integers from 1 to that
number. Example – Factorial of 5 is 120 i.e. 1*2*3*4*5
OUTPUT:
Enter a number to calculate the factorial of a number : 5
Factorial is : 120
Example 26: Write a Python program to find if the input number is Prime
or not using while loop.
OUTPUT:
Please enter a number : 2
Your number is a Prime number !
Example 27: Write a Python program to print the numbers from 1 to 5
using for loop
i=1 OUTPUT:
for i in range(i,6): 1
print (i) 2
3
4
5
Example 28: Write a Python program to print the numbers from 0 to 5
using for loop
Example 30: Write a Python program to print the numbers in reverse order
from 5 to 1 using for loop
Method 1:
sum=0
for i in range(1,6):
print (i)
sum+=i
print("Sum of series :",sum)
Method 2:
sum=0
OUTPUT:
i=1
1
for i in range( i , 6):
2
print (i)
3
sum+=i
4
print("Sum of series :",sum)
5
Sum of series: 15
Method 3 :
sum=0
i=1
j=6
for i in range( i , j ):
print (i)
sum+=i
print("Sum of series :",sum)
OUTPUT:
10
20
30
40
Nested Loop :
A Loop may contain another loop in its body. This form of a loop is
called Nested loop.
In a Nested loop, the inner loop must terminated before the outer
loop.
Example 34: Write a Python program to print all the prime numbers by
taking input from user using Nested for loop.
OUTPUT:
Enter lower range: 5
Enter upper range: 11
Prime numbers between 5 and 11 are:
5
7
11
Example 35: Write a Python program to print the following pattern-
* * * *
* * * *
* * * *
* * * *
r = 4
c = 4
r=5
for i in range(r):
for j in range(i):
print("*", end='')
print()
r=1
c=5
for r in range(r,c+1):
for c in range(r, c+1):
print("*", end='')
print()
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
r=6
for i in range(r):
for j in range(i):
print(i, end='')
print()
Chapter 2 : Brief overview of Python Page 31 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
r=6
for i in range(r):
for j in range(i):
print(j, end='')
print()
Example 40: Write a Python program to print the following triangle
pattern-
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
r=6
for i in range(1,r):
for j in range(i,0,-1):
print(j, end='')
print()
67.
68.
(b)
sum = (length + breadth)/2
(c)
stationery = [„Paper‟, „Gel Pen‟, „Eraser‟]
(d)
first = „Mohandas‟
middle = „Karamchand‟
last = „Gandhi‟
Chapter 2 : Brief overview of Python Page 33 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
(e)
3. Which data type will be used to represent the following data values and
why?
Ans. (a) Number of months in a year: int (integer) because number of months
always integer.
(b) Resident of Delhi or not: bool (Boolean) because it contains Answer
yes or no it means True or False.
c) Mobile number: int (integer) because mobile number always in integer
form.
d) Pocket money: float or int because pocket money is different for
different persons.
(e) Volume of a sphere: float because volume may be in decimal.
(f) Perimeter of a square: float because perimeter may be in decimal.
(g) Name of the student: str (string) because name is the collections of
characters.
(h) Address of the student: str (string) because address is the collections
of characters.
4. Give the output of the following when num1 = 4, num2 = 3, num3 = 2
(a) num1 += num2 + num3
(b) print (num1)
(c) num1 = num1 ** (num2 + num3)
(d) print (num1)
(e) num1 **= num2 + c
(f) num1 = '5' + '5'
(g) print(num1)
(h) print(4.00/(2.0+2.0))
Chapter 2 : Brief overview of Python Page 34 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
(b)
country = 'INDIA'
for i in country:
print (i)
(c)
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
Chapter 2 : Brief overview of Python Page 36 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
sum = sum + i
i = i + 2
print (sum)
Ans. (a)
20
22
24
26
28
(b)
I
N
D
I
A
(c)
12
12. Case Study Based Question
Identify the personal details of students from your school identity card and
write a program to accept these details for all students of your school and
display them in this format.
Ans. name_of_school = input("Enter tne Name of School: ")
stu_name = input("Enter the Name of Student: ")
roll = int(input("Enter the Roll number: "))
Chapter 2 : Brief overview of Python Page 37 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
a) type(10)
b) type('10')
c) type(10.0)
d) type(True)
e) type('False')
Ans. a) <class 'int'>
b) <class 'str'>
c) <class 'float'>
d) <class 'bool'>
e) <class 'str'>
26. Which function is used to find the data type of variable?
Ans. type() function
27. Identify the variable name, variable type, value and operator used in the
following expression -
X=9
Ans. variable name : x
variable type : integer
value : 9
operator : =
28. What do you mean by assignment operator?
Ans. An operator which is used to assign a value to a variable is called
assignment operator. Symbol of assignment operator is '=', Example :
c = 10, value 10 is assigned to variable 'c'
29. Write the output of the following :
x = 8
x = 5
print (x + x)
Ans. 10
30. Is the following statement correct?
a, b, c = 2 , 3 , 'Amit'
Ans. Yes.
31. Evaluate the following expressions
a) 23 + 4 **2
b) 9 * 3 - 8 + 6
c) 7%2 + 7//2
d) 67 + 3%3
e) 12 % 4 + 6 + 4 // 3
Ans. a) 39
b) 25
c) 4
d) 67
e) 7
38. Write a program to accept name from the user and display "Hello <name>".
Ans. name=input("Enter your name")
print("Hello ",name)
39. Write a program to accept the first name and last name from user and
display the full name.
Ans. fname=input("Enter your first name")
lname=input("Enter your last name")
print("Complete name is",fname , lname)
a =3
b =4
print(a + b%2 + a**2)
Ans. 12
51. Write the output of the following-
a) int(20.5 + 2.7)
b) int(20.5) + int(2.7)
Is both the expression produce the same result?
Ans. a) 23
b) 22
No, in the first expression it first add the float values and then conversion
take place. But in second expression conversion the value first and then
addition take place.
52. Find errors in the following code and re-write:
stu_marks = 90
print(Students, Score stu_marks)
Ans. There is error in line 2. The print() function accepts only variable without function if the
user needs to print any custom message, the message text should be enclosed in double-
quotes. So correct statement is anyone these:
print(stu_marks)
print(“Students Score:”, stu_marks)
53. Find errors in the following code and re-write:
a = 30, b = 40
print(a;b)
Ans. There is an error in Line 1. Python doesn’t allow to assign multiple variables like this. If
its required it can be done either one of these ways:
Method 1:
a,b = 30, 40
Method 2:
a = 30
b = 40
There is an error in Line 2 as well. To separate values of the variable in print, user can
use, (comma) not a semicolon, hence the correct code is like this:
print(a,b)
54. Find errors in the following code and re-write:
stu_name(input=”Enter name of student:”)
print(“Student Name:”,Stu_name)
Ans. There is error in Line 1. When input() function is used in the expression, in
must come after =. Here the correction is like this:
stu_name = input(“Enter name of student:”)
ii)
while i>j;
print(i*j)
i++1
Ans Error 1 : Variable i and j not define.
Error 2 : Colon (:) required instead of Semi-colon(;)
Error 3 : Step variable i should be -= operator to decrease
the value of variable by 1
i=5
j=1
while i>j:
print(i*j)
i-=1
iii)
f=1
for i in range(30,70,-3)
print(f*i)
Ans. Error 1 : Colon required at the end of Line 2
Error 2 : Start and Stop value should be 70 and 30
respectively in range() because Step value is negative
integer otherwise No output.
f=1
for i in range(70,30,-3):
print(f*i)
iv)
c=1
while(c=!0)
sum=sum+c
c++
Ans. Error 1 : Variable sum is not define and initilise
Error 2 : It should be != instead of =! Operator.
Error 3 : Instead of ++ , it should be =+ to increase the
value of a variable by 1.
Error 4 : Colon is missing after while loop condition.
c=1
sum=0
while(c!=0):
sum=sum+c
c+=1
57. Find the output of the following looping statement -
i)
a = 10
while a<=25:
a+10
print(a)
Ans. 10
ii)
for i in '678':
print('IP Class XI')
Ans. IP Class XI
IP Class XI
IP Class XI
iii)
for i in range(10,20,6):
print(i**2)
Ans. 100
256
iv)
i,j,n=1,0,0
while i<4:
for j in range(i):
n+=1
print(n)
i+=1
Ans. 1
3
6
v)
i,n=2,0
while i<4:
n+=1
i+=1
print(n)
Ans. 2
vi)
i=1
s=0
while i<10:
print(j,"+")
s=s+j
j=j+j%3
print("=",s)
Ans. NameError: name 'j' is not defined
vii)
for i in range(30,40):
if i==25:
break
print(x)
viii)
for a in range(50,60):
if a%4==3:
continue
print(a)
Ans. 50
52
53
54
56
57
58
ix)
if a in range(3):
for b in range(a):
if a+2==b:
Chapter 2 : Brief overview of Python Page 47 of 49
Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com
Unit 2 : Introduction to Python : Class XI Notes, 2023-24
print("+",end=" ")
else:
print("o",end=" ")
Ans. NameError: name 'a' is not defined
x)
a=0
for i in range(1,3):
for j in range(1,i):
x=i+j-1
if x%2==0:
y+=x
elif z%3==0:
y+=z-2
print(y)
Ans. NameError: name 'y' is not defined
ii)
c=0
for i in range(10,0,-2):
if i+3%2==1:
c+=1
Ans. c=0
i=10
while i>0:
if i+3%2==1:
c+=1
i-=2
iii)
for i in range(10,20):
if i%2==0:
continue
print(i)
Ans. i=10
while i<20:
if i%2==0:
continue
i+=1
print(i)
60. Find the output of variable a and b and number of times loop repeats -
i)
a=25
b=25
while a>=b:
a=b/a
print(a,b)
Ans. a=1.0
b=25
Number of times loop repeats – 1
ii)
a=10
b=7
while a%b>=0:
break
a=a+1
b=b+2
print(a,b)
Ans. A=10
B=7
Number of times loop repeats – 1
61.
62.