7.
Introduction to Python
Q.1 Select the correct option.
.
i. Which of the following tasks can be performed using Python [U]
programming language?
a. data analysis b. machine learning
c. natural language processing d. all of these
ii. You have to create a program that calculates and displays the [Ap
]
sum of three numbers. The program prompts the user to enter
the numbers. Which mode of Python would be most suitable
for developing this program?
a. interactive mode b. script mode
c. debugging mode d. both a. and c.
iii. You have to develop a program to greet users by name and [Ap
]
let them know the current temperature. The current
temperature is stored in a variable ‘temp’. Which print
command would be most suitable for displaying the message?
a. print("Hello user! The b. print("Hello user! The
temperature is:", temp) temperature is " + temp
+ " degrees.")
c. print("Hello user! The d. print("Hello user! The
temperature is {} temperature is",
degrees.".format(temp)) str(temp), "degrees.")
iv. What is the file extension of a saved Python program? [R]
Pg no. 1
7. Introduction to Python
a. .src b. .py
c. .code d. .pyc
v. Which of the following symbol is used to add a comment in a [R]
Python program?
a. # b. $
c. % d. &
Q.2 What will be the output of the following programs in [Ap
. ]
Python?
i.
Ans The result of 12 / 5 is: 2.4
:
ii.
Ans The result of 3*4 is: 12
:
iii.
Ans The result of {a} % {b} is: 5
:
Pg no. 2
7. Introduction to Python
Q.3 Answer the following questions.
.
i. What are the advantages of using an Integrated [U]
Development Environment (IDE) for programming in Python?
Ans: An IDE enables you to create, test and execute
programs within the same environment.
It has many useful features, such as syntax
highlighting and autocomplete, making writing and
reading Python code easier.
It also includes a debugger, which allows you to
execute your code line by line to find and fix errors.
ii. What are variables? Write commands in Python to create a [U]
variable called ‘a’, assign the sum of values stored in
variables ‘b’ and ‘c’ to the variable ‘a’ and display its value.
Ans: A variable is a named location in the computer
memory that is used to store a value. You can use a
variable to store either number or text values. You
can also change the value stored in a variable.
Python code
b = 5 # Example value for variable b
c = 10 # Example value for variable c
a = b + c # Sum of b and c assigned to variable a
print(a)
iii. Explain why you cannot use a value input by a user directly [Ap
]
in mathematical calculations. What do you need to do to use
them for calculations?
Ans: A value entered by a user is always stored as a string
data type even if it is a number. If you want to use it
in calculations, you will need to convert it into a
Pg no. 3
7. Introduction to Python
numeric data type. This can be
either integer or decimal number type.
Lab Activity
Q.1 Write a program that asks the user for his/her name, stores [Ap
. the name in a variable and then displays ‘Hello’ along with ]
the user name.
An
s:
Q.2 Write a Python program that accepts the base and height [Ap
. from the user and calculates the area of a triangle. ]
Area of triangle = (base * height) / 2
An
s:
Q.3 Write a Python program for the following:
accepts two values from the user and stores them in
two variables
interchange the values of the two variables
display the variables along with their values before and
after interchanging
Pg no. 4
7. Introduction to Python
An
s:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Pg no. 5