PYTHON
Answers of textbook exercises
Fill in the Blanks
1. We use i n p u t ( ) command to get data from the user.
2. Python is created by G u i d o R o s s u m
3. If num=10, value of num==10 is T r u e.
4. I f statement is used in Python for decision making.
5. Elif is the short form of e l s e i f .
6. There can be an o p t i o n a l else block with
an if block.
7. I D L E is an editor for Python.
State True or False
1. Python is case sensitive. -True
2. Python supports only Object Oriented programming. -False
3. The code inside an if block is executed always. –False
4. You cannot place another if block inside an if block. –False
5. Checking multiple conditions in Python requires elif
statements. –True
[Link] code inside an if block is executed when the condition
given is true. -True
Notes to be written in the classwork
Answer in one word or one sentence
1. What is the purpose of an if statement in Python?
The if statement in Python is used to make decisions and
execute certain blocks of code based on whether a certain
condition is true or false.
2. What is the use of elif in Python?
The elif statement allows to check multiple conditions and
execute a particular block of code.
3. When is the code inside an if statement is executed ?
The code inside an if statement is executed when the
condition is evaluated to be true.
Answer the following
1. What are the features of Python?
It is easy to learn, readable and portable. Python has a standard library
which is very portable and cross-platform compatible and supports
interactive Mode. Python provides interface to all major commercial
databases.
2. What are the rules for naming variables in Python?
Must start with a letter or underscore (_).
May consist of letters, numbers, and underscores.
Variable names are Case Sensitive
3. Write down the syntax of if statement in Python.
4. What is the difference between a ‘nested if’ and
if..elif ladder?
In a nested if structure, an if statement is nested
inside another if or else block. This allows for multiple levels
of condition checking and branching within the code.
In an if..elif ladder, multiple elif statements are used to
check alternative conditions one after another and only the
code block associated with the first condition that evaluates
to True is executed.
5. Write a program to input your name and print “Hello your name".
name = input("Enter your name: ")
print("Hello", name)
6. Write a python program to input two numbers and display the sum.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
print("The result is”, sum)
7. Write a program to input a number. Find out whether
it is greater than or less than zero
[Link] a python program to check whether the number is positive,
negative or zero ?
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”)
9. Draw the flowchart of an if....elif….else ladder?