Class 8 CH 4
Class 8 CH 4
Class 8 CH 4
S
N
IO
1. Write a program to enter age and check if the citizen is senior
or not.
AT
age = int (input (“enter your age”))
if (age>=60) :
IC
print (“You are a senior citizen”)
else :
print (“You are not a senior citizen”)
BL
else :
TU
Computer-8
elif (age<=18) :
print (“You are an adolescent”)
elif (age<=59) :
print (“You are a middle age”)
else :
print (“You are a senior”)
S
Exercises
N
(A) Fill in the blanks with the help of the hints.
1. Intel, Cisco, Seagate, Qualcomm, and IBM use Python for
IO
hardware testing
_______________________.
2. The if statement will execute block of statements only if the
true
condition is ______.
3.
4.
A _______
AT
Loop statement allows us to execute a statement or group
of statements multiple times.
float() function is used to convert input into floating point
________
IC
numbers.
a) ✔ <> b) == c) <=
4. How do we write an if statement in Python?
TU
(C) State whether these statements are True or False. Correct the
False statements also.
1. Syntax of range() Function is : range(step, stop, start).
False. Syntax of range() Function in range (start, stop, step).
___________________________________________________________________
Computer-8
2. To check another condition when first condition is false elif is
used.
True
___________________________________________________________________
3. The for Loop executes a block of code repeatedly till the
condition is true.
True
___________________________________________________________________
S
(D) Answer the following questions.
N
1. List the key features of Python.
Ans. Some important features of Python:
IO
i. Python is easy to understand and learn for beginners.
ii. Python interpreters are available for many operating
systems.
AT
iii. Python is a multi-paradigm programming language.
(Procedural and Object-oriented)
iv. Its formatting is visually uncluttered, and it often uses
IC
English keywords.
v. The simple and easy-to-learn syntax of Python
emphasises readability.
BL
Java etc.
2. What are iteration statements in Python? Define each
one of them.
Ans. Iteration or loop statement in python are used to repeat
steps till the condition is true. Python has two iteration
K
Computer-8
3. Describe the different types of decision making statements
available in Python.
Ans. In Python if is the main decision making or conditional
statement. We can use it in four ways.
i. if statement
ii. if...else statement
S
iii. Nested if statement
iv. elif statement
N
Python supports the usual relational operators for writing
condition.
IO
i. if Statement
It is one of the commonly used flow of control
statements. It allows to check a condition for true
AT
evaluation. If the condition is evaluated to true, the
statement(s) following the if statement will be executed.
Syntax:
IC
if(condition): #Condition to be check
statements #Statement to be executed if the
condition is true
BL
ii. if...else
The if else statements will execute one block of
statements if the condition is true and another block of
statements will be executed if the condition is false.
PU
Syntax:
if(condition):
statements #Block of statement to be executed if
the condition is true
K
else:
statements #Block of statement to be executed if
TU
Computer-8
is true.
Syntax:
if (condition):
statements
if (condition): #this condition will be checked if
the previous condition is true.
S
statements
else:
N
statements
iv. elif statement
IO
Python also allows to check multiple levels of conditions.
elif is used when we have to check another condition
when the previous condition is false. If the second
AT
condition is true, statements following it will be
executed otherwise statements with else will be
executed. In other languages for such purpose else if
statement is used.
IC
Syntax:
if (condition):
BL
statements
elif (condition): #This condition will be checked if the
previous condition is false
PU
statements
else:
statements
4. How is a .pyc file different from a .py file?
Ans. A Python program file is saved with extention .py. When
K
Computer-8
Ans. Once compiled, a Python program file is saved as byte
code. This byte code is interpreted to execute program
using Python Virtual Machines. Thus, Python is an
interpreter language.
3. What is the significance of indentation in python?
Ans. Python use indentation (whitespace at the beginning of
S
a line) to define scope in the code. Other programming
languages often use curly-brackets for this purpose.
N
4. How does nested if and elif differ in python?
Ans. Nested if is used when we have to check another
IO
condition when the first condition is true. An elif is
used when another condition has to be checked when
the first condition is false.
5.
Ans.
AT
What does the “in” operator do?
A list or sequence has to be defined to use a for loop in
Python. The membership operator 'in' checks whether a
IC
value is present in a list or not. The loop will continue
to execute for all the values present in the list. The loop
will stop at the end of the list.
BL
Print (x)
while i < 2 :
print “Hello ...”
i = i+1
Two times
___________________________________________________________________
Computer-8
2. Rewrite the following for loop into while loop:
for a in range(25,500,25):
print a
a=25
___________________________________________________________________
while a<500:
___________________________________________________________________
S
print a
___________________________________________________________________
a = a + 25
___________________________________________________________________
N
IO
AT
IC
BL
PU
K
TU
Computer-8