Introduction to Python Programming
Introduction to Python Programming
WEIGHTAGE OF CHAPTER
5 Marks
VSA SA LA
& HOTS Total
1 Mark 2 Marks 3 Marks
2 MCQ 01
01 01 18
1 Fib 1 Hots
Synopsis
Python is an open-source, High Level, Interpreter based Language that can be used for a
multitude of scientific and non-scientific computing purposes.
Comments are Non-executable statements in a program.
An Identifier is a user defined name given to a variable or a constant in a program.
The process of identifying and removing errors from a computer program is called debugging.
Trying to use a variable that has not been assigned a value gives an error.
There are several data types in Python — integer, Boolean, float, complex, string, list, tuple, sets,
none and dictionary.
Datatype conversion can happen either Explicitly NOTES or Implicitly.
Operators are constructs that manipulate the value of operands. Operators may be unary or
binary.
An expression is a combination of values, variables and operators.
Python has an input() function for taking user input.
Python has a print() function to output data to a standard output device.
Python Keywords:
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Operators:
1. Arithmetic Operators [ +, -, * , / , % , // ,**]
2. Relational Operators [==,!=, >, <, >=, <= ]
3. Assignment Operators [=, +=, -=, *=, /=, %=, //=, **=]
4. Logical Operators [ and, or, not]
5. Identity Operators [ is, is not]
6. Membership Operators [ in, not in]
Precedence of operators
Order of Operators Description
Precedence
1 ** Exponentiation (raise to the power)
Type Conversion
1. Explicit / type casting
2. Implicit / type coercion
Debugging
1. Syntax Errors
2. Logical Errors
3. RunTime errors
INTRODUCTION TO PYTHON
1 Mark Questions
1. What is a program?
An ordered set of instructions to be executed by a computer to carry out a specific task is called
a program.
2 Mark Questions
FEATURES OF PYTHON
5 Mark Questions
Briefly explain 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 the same in Python.
Python is portable and platform independent, meaning 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.
2. Which symbol in the python prompt is used, where the interpreter is ready to take instructions?
Ans: >>>
EXECUTION MODES
2 Mark Questions
2. Briefly explain how python can be executed using interactive and script mode.
Interactive Mode: Allows execution of individual statements instantaneously.
Script mode: Allows us to write more than one instruction in a file called Python source code file
that can be executed.
Interactive Mode:
● To work in the interactive mode, we can simply type a Python statement on the >>> prompt
directly.
● As soon as we press enter, the interpreter executes the statement and displays the result.
● This mode is convenient for testing a single line code for instant execution.
● But in the interactive mode, we cannot save the statements for future use and we have to retype
the statements to run them again.
Script Mode:
● In the script mode, we can write a Python program in a file, save it and then use the interpreter to
execute it.
● Python scripts are saved as files where file name has extension “.py”.
To execute the Python Program in script mode click Run->Run Module from the menu or press F5
from the keyboard.
b) While working in the script mode, after saving the file, click [Run]->[Run Module] from the menu
as shown in Figure 2.
PYTHON KEYWORDS
1 Mark Questions
3 Mark Questions
1 List all the Keywords available in Python.
False class finally is return
as elif if or yield
PYTHON IDENTIFIERS
1 Mark Questions
[Link] an identifier.
In programming languages, identifiers are names used to identify a variable, function, or other
entities in a program.
5 Mark Questions
1. Mention the rules for naming an identifier in Python. Also give examples for valid and invalid
identifiers.
The rules for naming an identifier in Python are as follows:
● 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.
● It can be of any length. (However, it is preferred to keep it short and meaningful).
● It should not be a keyword or reserved word given in Table 5.1.
● We cannot use special symbols like !, @, #, $, %,etc., in identifiers.
To find the average of marks obtained by a student in three subjects, we can choose the identifiers as
marks1, marks2, marks3 and avg rather than a, b, c, or A, B, C.
avg = (marks1 + marks2 + marks3)/3
Similarly, to calculate the area of a rectangle, we can use identifier names, such as area, length, breadth
instead of single alphabets as identifiers for clarity and more readability.
area = length * breadth
Example for Invalid Identifiers:
Serial_No, 1st_Room, Hundred$ etc.
VARIABLES
1 Mark Questions
1. What is a variable?
A variable in a program is uniquely identified by a name (identifier). Variable in Python refers to an
object — an item or element that is stored in the memory.
2 Mark Questions
1. Illustrate with an example of how a variable can be created and assign values to it.
In Python we can use an assignment statement to create new variables and assign specific values to
them.
Eg: gender = 'M'
message = "Keep Smiling"
price = 987.9
3 Mark Questions
1. Write a program to display values of variables in Python.
message = "Keep Smiling"
print(message)
userNo = 101
print('User Number is', userNo)
COMMENTS
1 Mark Questions
[Link] Comments.
Comments are used to add a remark or a note in the source code. Comments are not executed by the
interpreter.
5 Mark Questions
1. Briefly explain the benefits of using comments in the python programming language with an
example.
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 and its input and
output requirements, so that we can remember later how it functions and how to use it.
For large and complex software, it may require programmers to work in teams and sometimes, a
program written by one programmer is required to be used or maintained by another programmer.
In such situations, documentations in the form of comments are needed to understand the working
of the program.
In Python, a 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:
#Variable amount is the total spending on
#grocery
amount = 3400
#totalMarks is sum of marks in all the tests
#of Mathematics
totalMarks = test1 + test2 + finalTest
3 Mark Questions
EVERYTHING IS AN OBJECT
3 Mark Question
1. Explain briefly with an example how everything is treated as an object in python programming.
● Python treats every value or data item whether numeric, string, or other type (discussed in the
next section) as an object in the sense that it can be assigned to some variable or can be passed to
a function as an argument.
● Every object in Python is assigned a unique identity (ID) which remains the same for the
lifetime of that object.
● This ID is akin to the memory address of the object. The function id() returns the identity of an
object.
Example:
>>> num1 = 20
>>> id(num1)
1433920576 #identity of num1
>>> num2 = 30 - 10
>>> id(num2)
1433920576 #identity of num2 and num1
#are same as both
refers to #object 2
DATA TYPES
1 Mark Questions
1. What is a Data Type?
Data type identifies the type of data values a variable can hold and the operations that can be
performed on that data.
2 Mark Questions
1. With a neat diagram illustrate the different data types in Python.
NUMBER
1 Mark Questions
1. List the 2 constants of Boolean Data Type
True value is non-zero
False is the value zero
2. Mention the built-in function used to determine the data type of the variable.
type():
3 Mark Questions
1. With a neat tabular representation, explain Number data type with examples.
Type/Class Description Examples
Table :
int Integer numbers -12,-3,0,125,2
Numeric Data
float real/floating point numbers -2.04,4.0,14.23 Types
5 Mark Questions
[Link] a python program which determines the data type of the variable using built-in-function.
>>> num1 = 10
>>> type(num1)
<class 'int'>
>>> num2 = -1210
>>> type(num2)
<class 'int'>
>>> var1 = True
>>> type(var1
<class 'bool'>
>>> float1 = -1921.9
>>> type(float1)
<class 'float'>
>>> float2 = -9.8*10**2
>>> print(float2, type(float2))
-980.0000000000001 <class 'float'>
>>> var2 = -3+7.2j
>>> print(var2, type(var2)) (-3+7.2j) <class 'complex'>
SEQUENCE
1 Mark Questions
1. Define a sequence.
A Python sequence is an ordered collection of items, where each item is indexed by an integer.
2. List the sequence data types available in Python.
The three types of sequence data types available in Python are
● Strings
● Lists
● Tuples.
5 Mark Questions
1. Define a String. Briefly explain the data type String with an example.
Definition : 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 (e.g., ‘Hello’) or in double quotation marks (e.g., “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.
Eg:
>>> str1 = 'Hello Friend'
>>> str2 = "452"
We cannot perform numerical operations on strings, even when the string contains a numeric value, as
in str2
2. Define a List. Briefly explain the data type List, with an example program
Definition : List is a sequence of items separated by commas and the items are enclosed in square
brackets [ ].
Eg:
#To create a list
>>> list1 = [5, 3.4, "New Delhi", "20C", 45]
#print the elements of the list list1
>>> print(list1)
[5, 3.4, 'New Delhi', '20C', 45]
3. Define a tuple. Briefly explain the data type Tuple with an example program.
Definition: Tuple is a sequence of items separated by commas and items are enclosed in parenthesis ( ).
●This is unlike list, where values are enclosed in brackets [ ]. Once created, we cannot change the tuple.
Eg:
#create a tuple tuple1
>>> tuple1 = (10, 20, "Apple", 3.4, 'a') #print the elements of the tuple tuple1
>>> print(tuple1)
(10, 20, "Apple", 3.4, 'a')
4. Define a set. Briefly explain the data type Set with an example program.
Definition:Set is an unordered collection of items separated by commas and the items are enclosed in
curly brackets { }.
●A set is similar to a list, except that it cannot have duplicate entries.
●Once created, elements of a set cannot be changed.
Eg:
#create a set
>>> set1 = {10,20,3.14,"New Delhi"}
>>> print(type(set1))
<class 'set'>
>>> print(set1)
{10, 20, 3.14, "New Delhi"}
NONE
5 Mark Questions
1. Define None data [Link] with an example program.
Definition: 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 perations, and it is
neither the same as False nor 0 (zero).
Eg:
>>> myVar = None
>>> print(type(myVar))
<class 'NoneType'>
>>> print(myVar)
None
MAPPING
1 Mark Questions
1. Define Mapping data type.
Mapping is an unordered data type in Python.
5 Mark Questions
1. With an example program explain the standard mapping data type Dictionary in Python. OR What
are the rules to be followed while declaring a dictionary?
● Dictionary in Python holds data items in key-value pairs. Items in a dictionary 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.
● The keys are usually strings and their values can be any data type.
● In order to access any value in the dictionary, we have to specify its key in square brackets [ ].
Eg:
#create a dictionary
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
>>>print(dict1['Price( kg)'])
120
2 Mark Questions
1. Draw a diagram for the classification of data types.
5 Mark Questions
1. Explain with a neat labeled diagram, how memory is allocated for variables with values being
assigned to them.
OPERATORS
1 Mark Questions
1. What is an Operator?
An operator is used to perform specific mathematical or logical operation on values.
2. What is an Operand?
The values that the operators work on are called operands.
2 Mark Questions
1. With an example explain what is an operator and an operand.
An operator is used to perform specific mathematical or logical operations on values.
The values that the operators work on are called operands.
For example, in the expression 10 + num, the value 10, and the variable num are operands and the +
(plus) sign is an operator.
ARITHMETIC OPERATORS
1 Mark Question
1. What is the usage of Arithmetic Operators?
Python supports arithmetic operators that are used to perform the four basic arithmetic operations as
well as modular division, floor division and exponentiation.
5 Mark Questions
[Link] the concept of Arithmetic Operators in Python with an example.
Oper Operation Description Example
ator
+ Addition Adds the two numeric values on >>> num1 = 5
either side of the operator >>> num2 = 6
>>> num1 + num2
This operator can also be used to 11
concatenate two strings on either >>> str1 = "Hello"
side of the operator >>> str2 = "India"
>>> str1 + str2
'HelloIndia'
- Subtraction Subtracts the operand on the right >>> num1=5
from the operand on the left >>> num2 =6
>>> num1 - num 2
-1
* Multiplication Multiplies the two values on both >>> num1 = 5
side of the operator. >>> num2 = 6
>>> num1 * num2 30
Repeats the item on left of the >>> str1 = 'India'
operator if first operand is a string >>> str1 * 2 'IndiaIndia'
and second operand is an integer
value
/ Division Divides the operand on the left by >>> num1=8
the operand on the right and returns >>> num2 =4
the quotient >>> num1 / num 2
0.5
% Modulus Divides the operand on the left by >>> num1=13
the operand on the right and returns >>> num2 =5
the remainder >>> num1 % num 2
3
// Floor Division Divides the operand on the left by the >>> num1=13
operand on the right and returns the >>> num2 =4
quotient by removing the decimal >>> num1 // num 2
part. It is sometimes also called 3
integer division. >>>num2 // num1
0
** Exponent Performs exponential (power) >>> num1=3
calculation on operands. That is, raise >>> num2 =4
the operand on the left to the power >>> num1 ** num 2
of the operand on the right 81
RELATIONAL OPERATORS
1 Mark Question
5 Mark Questions
1. Explain the concept of Relational Operators in Python with an example.
Assume the Python variables num1 = 10, num2 = 0, num3 = 10, str1 = "Good", str2 = "Afternoon" for
the following examples:
ASSIGNMENT OPERATORS
1 Mark Question
1. What is the purpose of the Assignment Operator?
Ans: Assignment operator assigns or changes the value of the variable on its left.
5 Mark Question
1. Explain the concept of Assignment Operators in Python with an example.
LOGICAL OPERATORS
1 Mark Question
1. List the 3 logical operators supported by Python.
There are three logical operators supported by Python. These operators (and, or, not) are to be
written in lower case only.
5 Mark Question
1. Explain the concept of Logical Operators in Python with an example.
The logical operator evaluates to either True or False based on the logical operands on either side.
Every value is logically either True or False. By default, all values are True except None, False, 0 (zero),
empty collections "", (), [], {}, and few other special values. So if we say num1 = 10, num2 = -20, then
both num1 and num2 are logically True.
and Logical If both the operands are True, >>> True and True
AND then condition becomes True True
>>> num1 = 10
>>> num2 = -20
>>> bool(num1 and num2)
True
>>> True and False
False
>>> num3 = 0
>>> bool(num1 and num3)
False
>>> False and False
False
IDENTITY OPERATORS
1 Mark Question
1. What is the use of an Identity Operator?
Identity operators are used to determine whether the value of a variable is of a certain type or not.
5 Mark Question
1. Explain the concept of Identity Operators in Python with an example.
● Identity operators are used to determine whether the value of a variable is of a certain type or
not.
● Identity operators can also be used to determine whether two variables are referring to the
same object or not. There are two identity operators.
Operator Description Example
MEMBERSHIP OPERATORS
5 Mark Question
EXPRESSIONS
1 Mark Questions
1. What is an expression?
An expression is defined as a combination of constants, variables, and operators. An expression
always evaluates to a value.
2. What is a value?
A value or a standalone variable is also considered as an expression but a standalone operator is not
an expression.
2 Mark Questions
1. What is an expression? Give an example.
An expression is defined as a combination of constants, variables, and operators.
Eg:
1. 100
2. Num
3. Num - 20.4
4. 3.0 + 3.14
5. 23/3 - 5 * 7(14 - 2)
6. “Global” + “Citizen”
PRECEDENCE OF OPERATORS
1 Mark Questions
1. What are binary operators?
Binary operators are operators with two operands.
2. What is meant by precedence of Operators?
Evaluation of the expression is based on precedence of operators. When an expression contains different
kinds of operators, precedence determines which operator should be applied first. Higher precedence
operator is evaluated before the lower precedence operator.
2 Mark Question
[Link] is meant by an Unary Operator? Give examples.
The unary operators need only one operand, and they have a higher precedence than the binary
operators.
Eg: The minus (-) as well as + (plus) operators can act as both unary and binary operators, but not is a
unary logical operator.
#Depth is using - (minus) as unary operator Value = -Depth
#not is a unary operator, negates True print(not(True))
5 Mark questions
1. With a tabular representation explain the precedence of operators in Python.
● 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.
Order of Operators Description
Precedence
10 and
11 or
Note:
a) Parenthesis can be used to override the precedence of operators. The expression within () is
evaluated first.
b) For operators with equal precedence, the expression is evaluated from left to right.
2. How will Python evaluate the following expressions?
a) 20 + 30 * 40 b) 20 - 30 + 40
= 20 + (30 * 40) #Step 1 The two operators (–) and (+) have equal
#precedence of * is more than that of + precedence. Thus, the first operator, i.e.,
= 20 + 1200 #Step 2 subtraction is applied before the second
= 1220 #Step 3 operator, i.e., addition (left to right).
= (20 – 30) + 40 #Step 1
= -10 + 40 #Step 2
= 30 #Step 3
c) (20 + 30) * 40 d) 15.0 / 4 + (8 + 3.0)
=(20 + 30) * 40 #Step 1 =15.0 / 4 +(8.0 + 3.0) # Step 1
#using parentheses(), we have forced =15.0 /4.0 + 11.0 #Step 2
precedence of + to be more than that of * = 3.75 + 11.0 #Step 3
= 50 * 40 #Step 2 =14.75 #Step 4
= 2000 # Step 3
STATEMENT
1 Mark Questions
1 Define Statement in Python with an example.
In Python, a statement is a unit of code that the Python interpreter can execute
Eg : >>> x = 4 # assignment statement
>>>cube = x ** 3 # assignment statement
>>> print (x, cube) #print statement
4 64
2. Name the data type which the input() function will take on accepting the data from the user
It accepts all user input as string.
2. Explain with an example of how we can typecast or change the datatype of the string data
accepted from the user to an appropriate numeric value.
The following statement will convert the accepted string to an integer. If the user enters any non-numeric
value, an error will be generated.
Eg:
#function int() to convert string to integer
>>> age = int( input("Enter your age:")) Enter your age: 19
>>> type(age)
<class 'int'>
1. sep: The optional parameter sep is a separator between the output values. We can use a
character, integer or a string as a separator. The default separator is space.
2. end: This is also optional and it allows us to specify any string to be appended after the last value.
The default is a new line.
Eg:
Statement Output
print("Hello") Hello
print(10*2.5) 25.0
print("I" + "love" + "my" + "country") Ilovemycountry
print("I'm", 16, "years old") I'm 16 years old
● The third print function in the above example is concatenating strings, and we use + (plus)
between two strings to concatenate them.
● The fourth print function also appears to be concatenating strings but uses commas (,) between
strings. Actually, here we are passing multiple arguments, separated by commas to the print
function.
● As arguments can be of different types, hence the print function accepts integer (16) along with
strings here.
● But in case the print statement has values of different types and ‘+’ is used instead of comma, it
will generate an error as discussed in the next section under explicit conversion.
TYPE CONVERSION
1 Mark Questions
[Link] is meant by type conversion?
As and when required, we can change the data type of a variable in Python from one type to another.
2 Mark Questions
1. List and explain the two ways of data type conversion.
The 2 ways of data type conversion are:
● Explicit (forced) conversion
● Implicit type conversion
Explicit Type Conversion: When the programmer specifies for the interpreter to convert a data type
to another type.
Implicit Type Conversion: When the interpreter understands such a need by itself and does the type
conversion automatically.
The program was expected to display double the value of the number received and stored in variable
num1. So if a user enters 2 and expects the program to display 4 as the output, the program displays
the following result:
As and when required, we can change the data type of a variable in Python from one type to another.
EXPLICIT CONVERSION
1 Mark Questions
Ans: Explicit conversion, also called type casting, happens when data type conversion takes place
because the programmer forced it in the program.
3 / 5 Mark Questions
3. Write a Program depicting explicit type [Link] a Program depicting explicit type conversion
conversion from int to float. from float to int.
#Explicit type conversion from int to float #Explicit type conversion from float to int
num1 = 10 num1 = 10.2
num2 = 20 num2 = 20.6
num3 = num1 + num2 num3 = (num1 + num2)
print(num3) print(num3)
print(type(num3)) print(type(num3))
num4 = float(num1 + num2) num4 = int(num1 + num2)
print(num4) print(num4)
print(type(num4)) print(type(num4))
5. Write a Program depicting explicit type conversion between numbers and Strings
#Explicit type casting
priceIcecream = 25
priceBrownie = 45
totalPrice = priceIcecream + priceBrownie print("The total in Rs." + str(totalPrice))
● On execution, program gives an error as shown in the above output, informing that the
interpreter cannot convert an integer value to string implicitly.
● It may appear quite intuitive that the program should convert the integer value to a string
depending upon the usage.
● However, the interpreter may not decide on its own when to convert as there is a risk of loss of
information.
● Python provides the mechanism of the explicit type conversion so that one can clearly state the
desired outcome.
6. Write a Program depicting explicit type casting
IMPLICIT CONVERSION
1 Mark Questions
Implicit conversion, also known as coercion, happens when data type conversion is done
automatically by Python and is not instructed by the programmer.
3 Mark Questions
1. Write a Program depicting implicit type conversion from int to float.
#Implicit type conversion from int to float
num1 = 10 #num1 is an integer
num2 = 20.0 #num2 is a float
sum1 = num1 + num2 #sum1 is sum of a float and an integer
print(sum1)
print(type(sum1))
In the above example, an integer value stored in variable num1 is added to a float value stored in
variable num2, and the result was automatically converted to a float value stored in variable sum1
without explicitly telling the interpreter.
DEBUGGING
1 Mark Questions
1. Define Debugging.
The process of identifying and removing such mistakes, also known as bugs or errors, from a program
is called debugging.
2 Mark Questions
1. List the different types of Errors occurring in programs.
● Syntax Errors
● Logical Errors
● RunTime errors
SYNTAX ERRORS
1 Mark Question
1. What is a Syntax Error?
The interpreter interprets the statements only if it is syntactically (as per the rules of Python)
correct. If any syntax error is present, the interpreter shows error message(s) and stops the execution
there.
3 Mark Question
1. Briefly explain Syntax errors with an example.
● Python has its own rules that determine its syntax.
● The interpreter interprets the statements only if it is syntactically (as per the rules of Python)
correct.
● If any syntax error is present, the interpreter shows error message(s) and stops the execution
there.
For example, parentheses must be in pairs, so the expression (10 + 12) is syntactically correct, whereas
(7 + 11 is not due to absence of right parenthesis. Such errors need to be removed before the execution
of the program.
LOGICAL ERRORS
1 Mark Question
RUNTIME ERRORS
1 Mark Question
1. What are Runtime Errors?
Ans: A runtime error causes abnormal termination of the program while it is executing. Runtime error
is when the statement is correct syntactically, but the interpreter cannot execute it.
3 Mark Questions
1. Briefly explain Runtime errors with an example.
● A runtime error causes abnormal termination of the 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.
For example, we have a statement having division operation in the program. By mistake, if the
denominator entered is zero then it will give a runtime error like “division by zero”.
ASSIGNEMNT - 1
1. Briefly explain the Features of Python
2. Briefly explain the 2 ways to use Python Interpreter.
3. Mention the rules for naming an identifier in Python. Also give examples for
valid and invalid identifiers.
4. Briefly explain the benefits of using comments in the python programming
language with an example.
5. Write a python program which determines the data type of the variable using
built-in-function.
6. Define a String. Briefly explain the data type String with an example.
7. Define a set. Briefly explain the data type Set with an example program.
8. Define None data type. Explain with an example program.
9. What are the rules to be followed while declaring a dictionary?
10. Explain with a neat labeled diagram, how memory is allocated for variables with
values being assigned to them.
ASSIGNEMNT - 2
i. Serial_no. v Total_marks
Ans
:
Ans
:
Ans
:
6. 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 . 9
: a. 1024
b. Syntax Error
c. 55
d. 1.0
e. 27.2
f. 3
g. 10.0
h. ValueError
i. False
j. True
k. True
l. True
m. True
9. A dartboard of radius 10 units and the wall it is hanging on are represented using a two-
dimensional coordinate system, with the board’s center at coordinate (0,0). Variables x
and y store the x-coordinate and the y-coordinate of a dart that hits the dartboard. Write
a Python expression using variables x and y that evaluates to True if the dart hits (is
within) the dartboard, and then evaluate the expression for these dart coordinates:
a) (0,0)
b) (10,10)
c) (6,6)
d) (7,8)
Ans
:
10. Write a Python program to convert temperature in degree Celsius to degree Fahrenheit.
If water boils at 100 degree C and freezes as 0 degree C, use the program to find out
what is the boiling point and freezing point of water on the Fahrenheit scale.
(Hint: T(°F) = T(°C) × 9/5 + 32)
11. Write a Python program to calculate the amount payable if money has been lent on
simple [Link] or money lent = P, Rate of interest = 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.
Ans
:
12. Write a program to calculate in how many days a work will be completed by three
persons A, B and C together. A, B, C take x days, y days and z days respectively to do the
job alone. The formula to calculate the number of days if they work together is xyz/(xy
+ yz + xz) days where x, y, and z are given as input to the program.
Ans
:
13. Write a program to enter two integers and perform all arithmetic operations on them.
Ans
:
Ans
:
15. Write a program to swap two numbers without using a third variable.
Ans
:
OR
16. Write a program to repeat the string ‘‘GOOD MORNING” n times. Here ‘n’ is an integer
entered by the user.
18. The volume of a sphere with radius r is 4/3πr3. Write a Python program to find the
volume of spheres with radius 7cm, 12cm, 16cm, respectively.
19. Write a program that asks the user to enter their name and age. Print a message
addressed to the user that tells the user the year in which they will turn 100 years old.
Ans
:
20. The formula E = mc2 states that the equivalent energy (E) can be calculated as the mass
(m) multiplied by the speed of light (c = about 3×108 m/s) squared. Write a program
that accepts the mass of an object and determines its energy.
Ans
:
21. Presume that a ladder is put upright against a wall. Let variables length and angle store
the length of the ladder and the angle that it forms with the ground as it leans
against the wall. Write a Python program to compute the height reached by the
ladder on the wall for the following values of length and angle:
a) 16 feet and 75 degrees
b) 20 feet and 0 degrees
c) 24 feet and 45 degrees
d) 24 feet and 80 degrees
Ans
:
HOTS
Lab Program - 1
2 Write a program to enter two integers and perform all arithmetic operations on
them.
# Python Program to Perform All Arithmetic Operations on Two Given Numbers
print("Enter any two positive integer numbers:")
p, q = int(input()), int(input())
sum, sub, mul, mod, div = 0, 0, 0, 0, 0
sum = p + q
sub = p - q
mul = p * q
div = p / q
mod = p % q
print("\n")
print("SUM ", p, " + ", q, " = ", sum)
print("DIFFERENCE ", p, " - ", q, " = ", sub)
print("PRODUCT ", p, " * ", q, " = ", mul)
print("QUOTIENT ", p, " / ", q, " = ", div)
print("MODULUS ", p, " % ", q, " = ", mod)
3 Write a python program to accept length and width of a rectangle and compute
its perimeter and area
print("Enter Length of Rectangle: ")
l = int(input())
print("Enter Breadth of Rectangle: ")
b = int(input())
p = 2*(l+b)
a=l*b
print("\nPerimeter = ", p)
print("\nArea=",a)
4 Write a python program to calculate the amount payable if money has been lent
on Simple Interest. Principal or money lent=P,Rate of interest=R% per annum and
Time=T years. Then Simple Interest(SI)=(P*R*T)/100.
Amount Payable = Principal + SI
P,R and T are given as input to the program.
5. Python is a
Low level language High level language Machine level language All of the above
6. Python is .
Open source language Free language Both a) and b) None of the above
7. Python support .
Compiler Interpreter Assembler None of the above
9. Python is .
a. Case – sensitive
b. Non case – sensitive
c. Both a) and b)
d. None of the above
17. To work in the interactive mode, we can simply type a Python statement on the
prompt directly.
>>> >> > None of the above
a. .py
b. .ppy
c. .pp d. .pyy
20. are reserved words in python.
a. Keyword
b. Interpreter
c. Program
d. None of the above
25. The variable message holds string type value and so its content is assigned within
.
Double quotes “” Single quotes ” Both a) and b) None of the above
31. identifies the type of data values a variable can hold and the
operations that can be performed on that data.
Data type Data base Both a) and b) None of the above
45. In the dictionary every key is separated from its value using
a
a. Colon (:)
b. Semicolon (;)
c. Comma (,)
d. All of the above
46. Variables whose values can be changed after they are created
and assigned are called .
Immutable Mutable Both a) and b) None of the above
a. Operands
b. Assignment
c. Mathematical Operator
d. All of the above
a. Arithmetic Operator
b. Logical Operator
c. Relational Operator
d. All of the above
a. and
b. or
c. not
d. All of the above
56. are used to determine whether the value of a
variable is of a certain type or not.
Relational Operator Logical Operator Identity Operator All of the above
57. can also be used to determine whether two
variables are referring to the same object or not.
Relational Operator Logical Operator Identity Operator All of the above
63. In Python, we have the function for taking the user input.
prompt() input() in() None of the above