100% found this document useful (1 vote)
3K views7 pages

Class 8 Python Programming Exercises

IT

Uploaded by

digitechrise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views7 pages

Class 8 Python Programming Exercises

IT

Uploaded by

digitechrise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

Iteration constructs like loops allow for code blocks to be executed multiple times without rewriting code, thus supporting code reuse and modularity. For example, using a 'for loop' to process each item in a list avoids the need to manually write code for each element. Decision-making constructs like 'if', 'elif', and 'else' allow for executing different code paths based on conditions, enabling dynamic responses and modular design. With these constructs, functions and modules can operate on varying inputs efficiently, encouraging program modularity and code reuse .

Python's support for multiple paradigms, such as procedural, object-oriented, and functional programming, enhances its versatility by allowing developers to choose the most suitable approach for their specific problem domain. This flexibility makes Python apt for a wide range of applications from script writing to enterprise-level application development. Its ability to integrate seamlessly with other languages further broadens its applicability, making it a popular choice in diverse fields like web development, data analysis, and artificial intelligence .

The 'elif' statement in Python allows for a cleaner and more readable flow of control by testing multiple conditions sequentially without deeply nesting 'if' statements. Unlike a nested 'if' where a new block is started for each condition, 'elif' provides a linear structure where only one block is executed. This helps in avoiding excessive indentation levels, reducing complexity, and making logic clearer by aligning related condition checks together .

Python's syntax, which is clear and closely aligned with the English language, contributes significantly to its popularity among beginners. English keywords in Python, such as 'if', 'else', 'while', and 'return', align with logical thinking, making it easier for newcomers to read and write code. This reduces the initial overhead of learning programming syntax and allows learners to focus on programming concepts and problem-solving .

In Python, indentation is used to define the scope of control structures like loops and conditional statements, indicating the block of code that they encompass. Unlike many other programming languages that use curly brackets to explicitly define code blocks, Python relies on levels of indentation (whitespace at the beginning of lines) to denote hierarchy and scope. This enforces readability and a single standard of code formatting .

In Python, the '==' operator is used to compare two values for equality, returning true if they are equal. The '<>' operator, an older and less common operator, also serves for inequality checks, similar to '!='. The '<=' operator compares two values to determine if the first is less than or equal to the second, useful in range checks or ending conditions in loops. Each operator aids in decision-making by allowing conditional flow based on the comparative evaluation of variables .

Python uses the 'for' loop for definite iterations where the number of loop cycles is known beforehand, as it can iterate over a range of numbers or elements directly. This makes it suitable for iterating over sequences, like lists or data structures. The 'while' loop is used for indefinite iterations, as it relies on a condition to continue executing, which is useful when the number of cycles is not strict and relies on dynamic computations or conditions becoming true. These two provide flexibility and clarity in managing known vs unknown iteration needs .

A Python source file is saved with the extension .py and contains Python code in its original, human-readable form. When this source file is executed or imported, it is compiled to bytecode which is a low-level set of instructions that a Python interpreter can execute. This bytecode is saved in a file with the .pyc extension. The .pyc files improve loading times by skipping the initial compilation step .

A 'while' loop is typically chosen when the number of iterations is not known before the loop starts executing. This usually happens when the code depends on user input, data retrieval from a server, or conditions updated dynamically within the loop. For example, when implementing input validation or waiting for a certain state change. Conversely, a 'for' loop is used when the number of iterations is predetermined, such as iterating over a fixed list or a range of numbers .

As an interpreted language, Python executes code line-by-line, which makes the development process faster since changes can be tested on-the-fly without additional compilation. It also enhances platform independence as code runs on any system with a compatible interpreter. However, this also means Python can be slower at runtime than compiled languages like C, as interpretation introduces overhead. Additionally, errors in code might only surface at runtime .

You might also like