[go: up one dir, main page]

0% found this document useful (0 votes)
36 views49 pages

Ch-3 Brief overviw of Python - 2023-24

dan kardiya

Uploaded by

Jagrab Jr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views49 pages

Ch-3 Brief overviw of Python - 2023-24

dan kardiya

Uploaded by

Jagrab Jr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Chapter 3

Brief Overview of Python

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.

10. What is an Interpreter?


Ans. An Interpreter processes the program statements one by one, first
translating and then executing.
11. Write the difference between Interpreter and Compiler.
Ans. Interpreter converts a HLL program into machine language by converting
and executing it line-by-line. Interpreter must always present in the
memory every time the program is executed. It first interpreted and then
executed.
Compiler converts HLL program (source code) into machine language in
one go. After successful compiled, compiler is no longer required as the
object program is available in the memory.
12. Python is an Interpreted language. Justify your answer.
Ans. Python is an Interpreted Language and not a compiled Language. It
means it required interpreter (not compiler) to execute its code line by line –
one statement at a time.
13. What is Python Sell?
Ans. To write and run (execute) a Python program, we need to have a Python
Interpreter installed on our computer or we can use any online Python
interpreter. The interpreter is also called Python shell.
14. What are the two ways to use Python Interpreter?
Or,

Chapter 2 : Brief overview of Python Page 2 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

Name two modes of Python Interpreter?


Ans. There are two ways to use the Python interpreter:
a. Interactive mode
b. Script mode

a. Interactive Mode of working means you type the command in


Python command prompt >>> one command at time and the Python
executes and gives the output. Interactive Mode does not save
command in the form of program. It is suitable for testing the code.
b. Script Mode, Python instructions are stored in a file with .py
extension and are executed together in one go as a unit. The saved
instructions are known as Python Script or program.

15. Write one drawback of interactive mode.


Ans. Interactive Mode does not save command in the form of program.

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:

Chapter 2 : Brief overview of Python Page 3 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

 Some valid identifiers :


Mybook, file123, z2td, date_2, _no
X Some invalid identifier :
2rno, break ,my.book, data-cs
20. What is a variable?
Ans. A VARIABLE in Python represents named location that refers to a value
and whose values can be used and processed during program run.
Example :
Student = ‘Jacob’
Age = 16
Marks=80.5
21. Write the rules for naming convention of a variable.
Ans. a. Variable is an identifier whose value can change.
b. Variable name should be unique in a program.
c. In Python, we can use an Assignment Operator to assign specific
values to them.
d. Value of a variable can be string, number or any combination of
alphanumeric.
e. Variables must always be assigned values before they are used in the
program, otherwise it will lead to an error.
22. What is the difference between Keywords and an Identifiers? Write the
answer with example.
Ans. Keywords are the pre-defined and specific reserved words, which hold
special meaning. On the other hand, an identifier is a different term or
name given to a variable, function or other entities in a program.
Examples of keywords are: int, char, if, while, do, class etc.
Examples of identifiers are: Test, count1, high_speed, etc.

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.

23. Write the three different ways to assign a variable(s)?


Ans. i) LVALUE can come on LHS and RVALUES can come only on RHS of
an assignment statement. Example -
a=5
b=7
a=b
ii) Assigning same value to multiple variables. Example-
a = b = c = 5
iii) Assigning multiple values to multiple variables. Example -
x, y, z = 10,20,30
Value 10 is assign to x,
Value 20 is assign to y,
Value 30 is assign to z
24. What is Date Type?
Ans. Data type identifies the type of data which a variable can hold and the
operations that can be performed on those data.
Type of Data type in Python-

25. What do you understand by Number data type?


Ans.  Number data type stores numerical values only.
• int --> eg. -22,-11,0,11,22
• float --> eg. -2.04, 2.04
• complex --> eg. 2+5i, 6-3i
 Boolean data type (bool) is a subtype of integer. It is a unique data
type, consisting of two constants, True and False. Boolean True value is
non-zero. Boolean False is the value zero.
26. What is Sequence in Python?

Chapter 2 : Brief overview of Python Page 5 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.  A Python Sequence is an ordered collection of items, where each item is


indexed by an integer value.
 Three types of sequence data types available in Python are
• Strings
• Lists
• Tuples
27. What is String in Python?
Ans.  String is a group of characters.
 These characters may be alphabets, digits or special characters
including spaces.
 String values are enclosed either in single quotation marks (for
example ‗Hello‘) or in double quotation marks (for example ―Hello‖).
 The quotes are not a part of the string, they are used to mark the
beginning and end of the string for the interpreter.
 Example of String in Python -
name = ‗Johni‘, fname =―johny‖
sex =‗M‘, gender=―F‖
rollno=‗14‘, empid=―AS-023‖

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”]

30. What is Tuple in Python?


Ans.  Tuple is a sequence of items separated by commas and items are
enclosed in parenthesis ( ).
 Once created, we cannot change items in the tuple.
 Similar to List, items may be of different data types.
 Example of Tuple in Python -
Chapter 2 : Brief overview of Python Page 6 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

#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

33. What is Mapping in Python?


Ans.  Mapping is an unordered data type in Python.
 Currently, there is only one standard mapping data type in Python called
Dictionary.
34. What is Dictionary in Python?
Ans.  Dictionary in Python holds data items in key-value pairs and Items
are enclosed in curly brackets { }.
 Dictionaries permit faster access to data.
 Every key is separated from its value using a colon (:) sign.
 The key value pairs of a dictionary can be accessed using the key.

Chapter 2 : Brief overview of Python Page 7 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

 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?

Chapter 2 : Brief overview of Python Page 8 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. 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 :

43. Write the various categories of Operators?


Ans. Operators are categorized as –
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Membership Operators
• Identity Operators
44. Write the various types of Arithmetic operator in Python.
Ans. Arithmetic Operators are used to perform mathematical calculation.
Operators Description Example 1 Example 2

+ perform addition >>>9+2 >>>„Good‟+‟Morning‟


of two number 11 GoodMorning

- perform >>>9-2 >>>2-9


subtraction of 7 -7
two number

/ perform division >>>9/2


of two number 4.5

Chapter 2 : Brief overview of Python Page 9 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

* perform >>>9*2 >>>‟Good‟*3


multiplication of 18 GoodGoodGood
two number

% Modulus = >>>9%2
returns 1
remainder

// Floor Division = >>>9//2


remove digits 4
after the
decimal point

** 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

>>>7<10 >>> ‟a‟ < “A”


< Less than,
True False
return true if a >>> 7<5 >>>”Hello”<
is less than b False ‟Goodbye‟
>>> 7<10<15 False
True >>>'Goodbye'<
>>>7<10 and 'Hello'
10<15 True
True

>>> 2<=5 >>>”Hello”<=“Goodbye


<= Less than or
True ”
equal to ,
>>> 7<=4 False
return true if a False >>>'Goodbye' <=
is less than b 'Hello‟
or a is equals True
to b

>>> 7>5 >>> ”Hello” >


> Greater than,
True ”Goodbye”
return true if a >>> 10>10 True
is greater than False >>> 'Goodbye„ >
b 'Hello'
False

Chapter 2 : Brief overview of Python Page 10 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

>>>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

Equal to, >>>10==10 >>>”Hello” ==


== return true True “Hello”
if a equals to b >>>10==11 True
False >>>”Hello” == “Good
Bye”
False

>>>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

Chapter 2 : Brief overview of Python Page 11 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=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

47. How the python compares two strings values?


Ans. Python compares two strings lexicographically using ASCII value of the
characters. If the first character of both strings are same, the second
character is compared, and so on.
ASCII/UNICODE :
A-Z : 65 to 90 a-z : 97 to 122 0-9 : 48 to 57
**Refer Example 2 - Relational Operator (Question No. 45)
48. What is Logical Operators?
Ans. Logical Operators are used to perform logical operations on the given two
conditions.
Operators Description Example

and return true if both condition are true x and y

or return true if either or both condition x or y


are true

not reverse the condition not(a>b)

AND Operator-
Condition 1 Condition 2 Condition 1 and Condition 2

False False False

False True False


True False False

True True True

Chapter 2 : Brief overview of Python Page 12 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

OR Operator-
Condition 1 Condition 2 Condition 1 or Condition 2

False False False

False True True


True False True

True True True

NOT Operator-
Condition Not Condition

False True
True False

49. What is the purpose of using membership operator in Python?


Ans. The membership operators in Python are used to validate whether a value
is found within a sequence such as such as strings, lists, or tuples.

Operators Description Example

in return true if value exists in the a in list


sequence, else false.

not in return true if value does not exists in a not in list


the sequence, else false.

50. What is an expression?


Ans.  An Expression is defined as a combination of constants, variables, and
operators.
 An expression always evaluates to a Single Value.
 A value or a standalone variable is also considered as an expression but a
standalone operator is not an expression.
 Some examples of valid expressions are given below-
26 a+5 (3+5)/4 b>5 “Apple” + ”Tech”
51. Write the Precedence of Operators from higher to lower.

Chapter 2 : Brief overview of Python Page 13 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.

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

56. What do you mean by python statement?


Ans. In Python, a statement is a unit of code that the Python interpreter can
execute. Example-
>>> x=5 #assignment statement
>>>area=x**2 #assignment statement
>>>print x #print statement
5
>>>print area
25

57. What is an Indentation in Python?


Ans. Leading whitespace (spaces and tabs) at the beginning of a statement is
called indentation.

Chapter 2 : Brief overview of Python Page 14 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

58. What is block of code in Python? How it is related with indentation?


Ans. A block is one or more lines of code, grouped together. In Python,
statements in a block are written with indentation and all the statements of
the block should be at same indent level.
 In Python, the same level of indentation associates statements into a
single block of code.
 The interpreter checks indentation levels very strictly and
throws up syntax errors if indentation is not correct.
Example :

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.

Chapter 2 : Brief overview of Python Page 15 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. 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'>

Example 2: Convert data type – float to integer


num1 = 4.5
num2 = 5.6
num3 = num1 + num2
print(num3)
print(type(num3))
num4 = int(num1 + num2)
print(num4)
print(type(num4))

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.

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))
sum = num1 + num2
print(“Sum of “, num1, “and”, num2,”is”,sum )
OUTPUT:
Enter first number: 5
Enter second number: 7
The Sum of 5 and 7 is 12
Chapter 2 : Brief overview of Python Page 18 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

ii) Selection construct means the execution of statement(s) depending upon


a condition-test. If a condition evaluates to True, a set of statements is
followed otherwise another set of statements is followed.
Selection construct is also called decision making construct because it
helps in making decision which set of statements is to be executed.

Selection Statement – different forms :


 if statement
 if..else statement
 if..elif..else statement
 if statement — executes the statement(s) inside if when the condition is
true. If the condition is true, then the indented statement(s) are executed.
The indentation implies that its execution is dependent on the condition.
 Syntax:
if condition:
statement(s)
Example 7: Write a program to accept a number and check if the number is
more than 10 and display appropriate message.

num=int(input("Enter Number :"))


if num>10:
print("Number is more than 10.")

OUTPUT:
Enter Number : 22
Number is more than 10.

Chapter 2 : Brief overview of Python Page 19 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

Example 8: Write a program to accept three integers and display the


largest of the three. (Using if statement)

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

 if...else statement executes the statement(s) inside if when the condition is


true, otherwise executes the statement(s) inside else (when the condition is
false)
 Syntax:
if condition:
statement(s)
else:
statement(s)
Example 9: Write a program to accept a number and check if the number is
more than or less than 10 and display appropriate message.

num=int(input("Enter Number :"))


if num>10:
print(num,“ is more than 10.")
else:
print(num,“ is less than 10.")

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.

age=int(input("Enter age :"))


if age>=18:
print("The person is Eligible.")
else:
print("The person is not Eligible.")

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.

marks=float(input("Enter marks :"))


if marks>=0 and marks<=100:
print(marks, "marks is a VALID.")
else:
print(marks, "marks is an INVALID.")

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.

Chapter 2 : Brief overview of Python Page 21 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

 if...elif....else is used to check multiple conditions and execute statements


accordingly.
 Syntax:
if condition_1:
statement(s)
elif condition_2:
statement(s)
elif condition_nth:
statement(s)
else:
statement
Example 14: Write a program to Check whether a number is positive,
negative, or zero and display appropriate message.

number = int(input("Enter a number: ")


if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")

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-

Sales Amount Discount %

>=3000 18 %
>=2000 15 %
< 2000 10 %

sales=float(input("Enter the Sales amount:"))


if sales>=3000:
disc=sales*0.18
elif sales>=2000:
disc=sales*0.15
else:
disc=sales*0.10
print("Sales Amount :",sales)
print("Discount Amount :",disc)
print("After Discount, Sales Amount :",sales-disc)

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

Sales Amount : 2500.0


Discount Amount : 375.0
After Discount, Sales Amount:2125.0
Example 16: Write a program to display the appropriate message as per
the color of signal at the road crossing.

print(“Red-R, Orange-O, Green-G)


SCol = input("Enter the color:")
if SCol == “r" or SCol == “R":
print("STOP")
elif SCol == “o" or SCol == “O“
print("Be Slow")
elif SCol == “g" or SCol == “G":
print("Go!")

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

Chapter 2 : Brief overview of Python Page 23 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 : (Alternate code)


per=float(input("Enter Percentage :"))
if per>=90:
grade='A'
elif 75<=per<90:
grade='B'
elif 60<=per<75:
grade='C'
else:
grade='D„
print("Percentage :",per)
print("Grade :",grade)

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.

print("Input lengths of the triangle sides: ")


x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))
if x == y == z:
print("Equilateral triangle")
elif x==y or y==z or z==x:
print("isosceles triangle")
else:
print("Scalene triangle")

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

Chapter 2 : Brief overview of Python Page 24 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

Two categories of Loops:


 Counting loops - The loops that repeat a certain number of times. for
loop is a counting loop.
 Conditional loops - The loops that repeat until condition is TRUE.
while loop is a conditional loop.
Two kinds of Loops :
 WHILE loop is a CONDITIONAL loop that will repeat the
instruction until the condition remains TRUE.
Syntax:
initilise_var
while (<condition_test>):
statement(s)
counter_var
 FOR loop a COUNTING loop that will repeat fixed number of times.
Syntax of for loop as follow-
 with sequence
 with range() function

 with sequence
Syntax :
for <var> in <sequence>:
statement to repeat

 with range() function


Syntax :
for <var> in range(start, Stop, Step):
statement to be repeat

Range() Function Output Step Value


range( 1 , 7) 1, 2, 3, 4, 5, 6 1 (Default)
range( 1 , 9, 2) 1, 3, 5, 7 2
range( 5, 1, -1) 5, 4, 3, 2 -1 (Default)
range(5) 0, 1, 2, 3, 4 Default Start
value 0 and Step
value 1

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

Chapter 2 : Brief overview of Python Page 25 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

Example 20: Write a Python program to input some integers to calculate


their sum and average. Input 0 to exit.

print("Input some integers to calculate their sum and


average. Input 0 to exit.")
count = 0
sum = 0.0
number = 1
while number != 0:
number = int(input(""))
sum = sum + number
count += 1
print("Sum of the above numbers are: ", sum)
print("Average :", sum / (count-1))

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

Chapter 2 : Brief overview of Python Page 26 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

Example 23: Write a Python program to input any number to find the sum
of its digits.

num = int(input("Enter Number to find the sum of digits:"))


ds = 0
rem=0
while num>0 :
rem=num % 10
ds=ds+rem
num = num // 10
print("Sum of the digits:", ds)

OUTPUT:
456
Sum of the digits: 15

Example 24: Write a Python program to input any number and display its
reverse.

num = int(input("Enter number to find its reverse :"))


rev = 0
while num>0 :
d = num % 10
rev = rev*10 + d
num = num // 10
print("Reverse :", rev)

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

print ("Enter a number to calculate the factorial of a


number : ")
number = int(input())
fact = 1
while number>0:
fact = fact*number
number = number-1
print ("Factorial is",fact)

OUTPUT:
Enter a number to calculate the factorial of a number : 5
Factorial is : 120

Chapter 2 : Brief overview of Python Page 27 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

Example 26: Write a Python program to find if the input number is Prime
or not using while loop.

num = int(input('Please enter a number: '))


i = 2
flag=0
while i < num:
if num%i == 0:
flag=1
i = i + 1
if flag!=0:
print ("Your number is a prime number!");
else:
print ("Your number is NOT a prime number!");

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

for i in range(6): OUTPUT:


print (i) 0
1
2
3
4
5
Example 29: Write a Python program to print the following series – 1, 3, 5,
7, 9 using for loop

for i in range(1,10,2): OUTPUT:


print (i) 1
3
5
7
9

Chapter 2 : Brief overview of Python Page 28 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

Example 30: Write a Python program to print the numbers in reverse order
from 5 to 1 using for loop

for i in range(5,0,-1): OUTPUT: Decrement by 1


print (i) 5
4
3
2
1
Example 31: Write a Python program to print the odd numbers in reverse
order from 5 to 1 using for loop

for i in range(5,0,-2): OUTPUT: Decrement by 2


print (i) 5
3
1
Example 32: Write a Python program to display the sum of series from 1 to
5 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)

Chapter 2 : Brief overview of Python Page 29 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

Example 33: Program to print the multiples of 10 for numbers in a given


range.

for num in range(5):


if num > 0:
print(num * 10)

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.

lower = int(input("Enter lower range: "))


upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower, upper + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)

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

Chapter 2 : Brief overview of Python Page 30 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

#Outer loop for generating rows


for i in range(r):
#Inner loop for generating columns
for j in range(c):
# printing stars
print("* ", end="")
# ending line after each row
print("")
Example 36: Write a Python program to print the following triangle
pattern-
*
* *
* * *
* * * *

r=5
for i in range(r):
for j in range(i):
print("*", end='')
print()

Example 37: Write a Python program to print the following pattern-


* * * * *
* * * *
* * *
* *
*

r=1
c=5
for r in range(r,c+1):
for c in range(r, c+1):
print("*", end='')
print()

Example 38: Write a Python program to print the following triangle


pattern-

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

Example 39: Write a Python program to print the following triangle


pattern-

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.

Chapter 2 : Brief overview of Python Page 32 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

NCERT TEXT BOOK QUESTIONS


1. Which of the following identifier names are invalid and why?
(a) Serial_no.
(b) 1st_Room
(c) Hundred$
(d) Total Marks
(e) Total_Marks
(f) total-Marks
(g) _Percentage
(h) True
Ans. (a) Serial_no. : - Invalid because it contains special character dot (.)
(b) 1st_Room :- Invalid because starting with a digit
(c) Hundred$ :- Invalid because it contains special character $
(d) Total Marks :- Invalid because of space between them
(e) Total_Marks :- Valid identifier
(f) total-Marks :- Invalid because it contains special character –
(g) _Percentage :- Valid identifier
(h) True :- Invalid because it is reserved keyboard
2. Write the corresponding Python assignment statements:
(a) Assign 10 to variable length and 20 to variable breadth.
(b) Assign the average of values of variables length and breadth to a
variable sum.
(c) Assign a list containing strings ‗Paper‘, ‗Gel Pen‘, and ‗Eraser‘ to a
variable stationery.
(d) Assign the strings ‗Mohandas‘, ‗Karamchand‘, and ‗Gandhi‘ to variables
first, middle and last.
(e) Assign the concatenated value of string variables first, middle and last to
variable fullname. Make sure to incorporate blank spaces appropriately
between different parts of names.
Ans. (a)
length = 10
breadth = 20

(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

We can also assign variable in that way:-

first, middle, last = „Mohandas‟, „Karamchand‟, „Gandhi‟

(e)

fullname = first + ' ' + middle + ' ' + last

3. Which data type will be used to represent the following data values and
why?

(a) Number of months in a year


(b) Resident of Delhi or not
(c) Mobile number
(d) Pocket money
(e) Volume of a sphere
(f) Perimeter of a square
(g) Name of the student
(h) Address of the student

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

(i) num1 = 2+9*((3*12)-8)/10


(j) print(num1)
(k) num1 = float(10)
(l) print (num1)
(m) num1 = int('3.14')
(n) print (num1)
(o) print(10 != 9 and 20 >= 20)
(p) print(5 % 10 + 10 < 50 and 29 <= 29)
Ans. a) New value assign in variable num1 that is 9 but it will give no output.
b) It will give output: 9
c) New value assign in variable num1 that is 59,049 but it will give no
output.
d) It will give output: 59,049
e) It will give error because here 'c' is not defined.
f) New value assign in variable num1 that is 55 as a string, but it will
give no output.
g) It will give output: 55
h) 1.0
i) New value assign in variable num1 that is 27.2 but it will give no
output.
j) It will give output: 27.2
k) New value assign in variable num1 that is 10.0 but it will give no
output.
l) It will give output: 10.0
m) New value assign in variable num1 that is 3.14 but it will give no
output.
n) It will give output: 3.14
o) True
p) True
5. Categories the following as syntax error, logical error or runtime error:
(a) 25 / 0
(b) num1 = 25; num2 = 0; num1/num2
Ans. Option (a) and (b) both are Runtime Error that is ZeroDivisionError.
6. Write a Python program to calculate the amount payable if money has been
lent on simple interest.
Principal or money lent = P, Rate = R% per annum and Time = T years.
Then Simple Interest (SI) = (P x R x T)/ 100.
Amount payable = Principal + SI.
P, R and T are given as input to the program.

Chapter 2 : Brief overview of Python Page 35 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. P = float(input("Enter Principal :"))


R = float(input("Enter Rate of interest:"))
T = float(input("Enter Time:"))
SI = (P * R * T)/100
A = P + SI
print("Amount to be payable is : ", A)
7. Write a program to repeat the string ‗‗GOOD MORNING‖ n times. Here n is
an integer entered by the user.
Ans. n = int(input("How many times do you want: "))
print ("GOOD MORNING\n" * n)
8. Write a program to find the average of 3 numbers.
Ans. num1 = int(input("Enter First Number : "))
num2 = int(input("Enter Second Number : "))
num3 = int(input("Enter Third Number : "))
Average = (num1 +num2 + num3)/3
print("Average of three numbers is : ", Average)
9. Write a program that asks the user to enter one's name and age. Print out a
message addressed to the user that tells the user the year in which he/she
will turn 100 years old.
Ans. name = input ("Enter the Name: ")
age = int (input("Enter the Age: "))
current_year=int(input(“Enter Current Year :”))
# Let current year is 2022
user_age = (100 - age) + current_year
print ("The user will in year ", user_age ,"he/she will
turn 100 years old.")
10. What is the difference between else and elif construct of if statement?
Ans. else: The else clause of an if statement is executed when the condition of the
if statement evaluates to false. else clause does not allow to check the
condition.
elif: The elif clause of an if statement is executed when there is multiple
conditions in if statement.
11. Find the output of the following program segments:
(a)
for i in range(20,30,2):
print(i)

(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

Schools use ―Student Management Information System‖ (SMIS) to manage


student related data. This system provides facilities for:

• Recording and maintaining personal details of students.


• Maintaining marks scored in assessments and computing results of
students.
• Keeping track of student attendance, and
• Managing many other student-related data in the school.

Let us automate the same step by step.

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

clas = input("Enter the Class: ")


address = input("Enter the Address: ")
city = input("Enter the City name: ")
pin = int(input("Enter the Pin Code: "))
phone = int(input("Enter the Parent Phone number: "))
print ()
print ("-"*60)
print ("| ", name_of_school)
print ("|")
print ("| Student Name: ", stu_name, " Roll NO: ", roll)
print ("| Address: ", address)
print ("|")
print ("| City: ", city, " Pin Code: ", pin)
print ("| Prent's/ Guardian's Contact No: ", phone)
print ("-"*60)

Chapter 2 : Brief overview of Python Page 38 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

Important Sample Questions


1. Name two modes of Python.
Ans. Interactive Mode and Script Mode
2. Write Full Form of IDLE
Ans. Integrated Development Learning Environment
3. Interface mode of python is also known as _______________.
Ans. Python Shell
4. In which mode we get result immediately after executing the command?
Ans. Interactive mode
5. Write the output of the following-
a. >>> x = 5
>>> y = 7
>>> print(x + y )

b. >>> print(2 **5)


Ans. a. 12
b. 32
6. Write one drawback of interactive mode.
Ans. We cannot save our commands
7. Aditya purchased 5 pencils and 2 erasers at the cost of Rs 7 and Rs 5
respectively. Write the program to calculate & display the total amount paid
by aditya.
Ans. pencil = 7
eraser = 5
tot_cost = pencil * 5 + 2 * eraser
print("Total cost paid by Aditya is",tot_cost)
8. What do you mean by comments in Python?
Ans. Non executable lines are called Comments in Python.
9. Which symbols are used for single line comments and multiple line
comments?
Ans. Single Line Comment : # symbol
Multiple Line Comment : ‟‟‟ or ””” (Single or Double Quotation)
10. What do you mean by variable?
Ans. Named storage location of value is called variable
11. What is the purpose of creating variables?
Ans. Variables are used to store values which we can use later in our programs.
12. What do you mean by data type?
Ans. Data type refers to the type of value used for. Example integer, float string
etc.
13. Write three numeric data type in python.
Ans. Integer, Floating Point and Complex

Chapter 2 : Brief overview of Python Page 39 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

14. Name three sequential data types in python.


Ans. List, tuple and String
15. Write the data type of variable x and y.
a) x = 200
b) y = 10.5
Ans. a) Integer
b) Floating point
16. Which data type store the combination of real and imaginary numbers?
Ans. Complex
17. Which data type return value True or False?
Ans. Boolean
18. Write the output of the following :
a) (75 > 2**5)
b) (25 != 5*2)
Ans. a) True
b) True
19. Which operator can be changed in (25 != 5 *2) so that it returns True?
Ans. == instead of !=
20. What do you mean by keywords in python?
Ans. Keywords are reserved words. We cannot use keywords for variable name or
any other identifier.
21. What do you mean by Escape sequence?
Ans. The sequence of characters after backslash is called escape sequence.
Example :
\n – New line
\t – Horizontal Tab
22. What is None data type?
Ans. This data type is used to define null value or no value. Example :
m = none
Now the variable 'm' is not pointing to any value, instead it is pointing to
none.
23. Write the output of the following
a = 10
b = None
print(a)
print(b)
Ans. 10
None
24. What is the purpose of type() function?
Ans. Type() function tell us about the data type of a variable. Example :
a = 10
print(type(a))
OUTPUT : <class 'int'>

Chapter 2 : Brief overview of Python Page 40 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

25. Write the output of the following.

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

Chapter 2 : Brief overview of Python Page 41 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

32. Write the name and purpose of the following operators.


a) //
b) %
c) **
Ans. a) // : Name of Operator is Floor Division . It is used to find the integer
part of the quotient when one number is divided by other.

b) % : Name is Modulus or Remainder Operator . It is used to find the


remainder when one number is divided by other.

c) ** : Name is Exponent. It is used to find the power of a number like


2**3 will give result 8.
33. Out of relational and arithmetic operators which have higher precedence?
Ans. Arithmetic Operators
34. Write the output of the following -
a) 35 < 6
b) 5 > 2**2
c) 7 != 3.5 *2
d) 7 == 3.5 ** 2
e) "Anil" > "Anita"
Ans. a) False
b) True
c) False
d) True
e) False
35. What do you mean by shorthand operator?
Ans. A combination of assignment and binary operator is called shorthand
operator. Example
x=x+2 can be written with shorthand operator as x +=2
36. Write the output of the following if x = 8.
a) x + = 8
b) x - = 8
c) x * = 8
d) x ** = 2
e) x % = 8
f) x / = 8
g) x // = 8
Ans. a) 16
b) 0
c) 64
d) 64
e) 0
f) 1.0
g) 1
37. What is the purpose of input() function?
Ans. input() function is used to take/accept data from the user.
Chapter 2 : Brief overview of Python Page 42 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

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)

40. What is the difference between eval() and int() function?


Ans. eval() - function return error on passing integer argument while int()
function does not return error. Example :
print(eval(12)) returns error
print(int(12)) returns 12

41. What is the similarity between eval() and int() function?


Ans. Both eval() and int() function required string value as a argument to
convert String type to integer type. Example :
print(eval(„12‟)) returns 12
print(int(„12‟)) returns 12
42. Write the output of the following-
eval("5 + 8 * 6")
Ans. 53
43. Write the output of the following-
int("5 + 8 * 6")
Ans. ValueError
44. How many spaces are there in one indentation level?
Ans. 4
45. Is python a case sensitive?
Ans. Yes.
46. Write the output of the following -
x , y = 3, 5
x, y = y, x + 6
print(x,y)
Ans. 5 9
47. What is the extension of python file?
Ans. .py
48. Write the output of the following -
a, b = 2, 6
a, b, b = a+b, 2, 3
print(a,b)
Ans. 8 3
49. Write the output of the following-
a = 5
b = "Amit"
print(a + b)

Chapter 2 : Brief overview of Python Page 43 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. Generate - TypeError


50. Write the output of the following-

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:”)

Chapter 2 : Brief overview of Python Page 44 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

55. Rewrite the following code fragment using if…elif..else statement:


color = input(“Enter the color name:”)
if (color==”red”):
print(“Subrato House”)
if(color==”blue”):
print(“Arjan House”)
if(color==”yellow”):
print(“Sekhon House”)
if(color==”green”):
print(“Katre House”)
if(color!=”red” or color!=”blue” or color!=”yellow” or
color!=”green”)
print(“Not a valid color name!!!!”)

Ans. color = input(“Enter the color name:”)


if (color==”red”):
print(“Subrato House”)
eliif(color==”blue”):
print(“Arjan House”)
elif(color==”yellow”):
print(“Sekhon House”)
elif(color==”green”):
print(“Katre House”)
else:
print(“Not a valid color name!!!!”)
56. Find the error from the following code and underline and re-write code:
i)
a = int(input("Enter the value of a:"))
for i in range 1 to 11:
if a = i
a+=i
else
a*=2
Ans. a = int(input("Enter the value of a:"))
for i in range (1,11):
if a == i:
a+=i
else:
a*=2

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

Chapter 2 : Brief overview of Python Page 45 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

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

Chapter 2 : Brief overview of Python Page 46 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

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)

Ans. NameError: name 'x' is not defined

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

58. Re-write the following while loop in for loop –


i)
i=2
while i<51:
i+=2
print(i)
print("Thank You")

Ans. for i in range(2,51,2):


i+=2
print(i)
print("Thank You")
ii)
i=100
while i>0:
print(i*2)
i=i-1
print(Over)
Ans. for i in range(100,0,-1):
print(i*2)
i=i-1
print("Over")
59. Re-write the following for loop in while loop –
i)
m=1
for i in range (6):
m=m*i
Ans. m=1
i=1
while i<6:
m=m*i
i+=1

Chapter 2 : Brief overview of Python Page 48 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

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.

Chapter 2 : Brief overview of Python Page 49 of 49


Prepared by Taposh Karmakar PGT(IP) AIR FORCE SCHOOL JORHAT
MOBILE : 7002070313 | Email : tap.afsj@gmail.com

You might also like