Class 8 CH 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

4 Programming in Python

TUK Activity (Page No. 72)


Try yourself.

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

2. Write a program to enter two values and print square of the


greater number.
num1 = int (input (“enter number”))
PU

num2 = int (input (“enter another number”))


if (num1>num2) :
print (num1*num1)
elif (num2>num1) :
print (num2*num2)
K

else :
TU

print (“both the numbers are equal”)


3. Write a program to check whether a citizen is child, adolescent,
middle age or senior.
age = int (input (“enter your age”))
if (age<=11) :
print (“You are a child”)

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.

(B) Tick (✔) the correct answer.


BL

1. Which statement will print "Hello World" in Python?


a) ✔ print(“Hello world”)
b) p(“Hello world”)
PU

c) echo “ hello world”


2. What is the correct file extension for Python files?
a) .pyth b) .pyt c) ✔ .py
3. Which operator can not be used to compare two values?
K

a) ✔ <> b) == c) <=
4. How do we write an if statement in Python?
TU

a) if(x>y)then: b) if(x>y) c) ✔ if x>y:

(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

vi. It supports modules and packages, which encourages


program modularity and code reuse.
vii. It can be easily integrated with languages like C, C++,
PU

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

statements; for and while.


i. for loop: The for loop is used when the number of
TU

repetitions is pre-known. It is also known as definite


loop.
ii. while loop: The while loop is used when the number
of repetitions is not known and are based on a
condition. The loop repeats till the condition is true.

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

the condition is false


iii. Nested if Statement
Multiple number of if statements can be used one inside
the other. When we need to check a condition when
previous condition is true, nested if is used. In such cases,
control will check second condition when first condition

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

the Python program file is compiled, its byte code is


created, this byte code file is saved with extension .pyc.
TU

(E) Answer in short.


1. Is Python case-sensitive?
Ans. Yes, Python is case-sensitive language.
2. How is Python an interpreter language?

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

Example: Program to print a list


x=8
For x in [8, 9, 10, 12]:
PU

Print (x)

(F) Application based questions


1. Without using the interpreter, tell us how many times will
K

Python execute the code inside the following while loop?


i=0
TU

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy