Class – VIII
Subject: Computer
L-9 Coding in Python
State whether these statements are true or false.
1) Sequence in another name for iteration.__FALSE
2) While is a conditional loop.__TRUE
3)
There
are 2 types of decision making statements in python._FALSE
4)
A list
is denoted by round brackets. FALSE
A.) Test your Skills:
1) Which of the following is not used as loop in python?
a) For loop
b) While loop
c) Do-while loop
d) None of the above
Ans: c) Do-while loop
2) Which of the following is not a decision-making
statement?
a) If statement
b) If-else statement
c) For statement
d) If-elf statement
Ans: c) For statement
3) The order of the execution of the statements in a program
is known as
a) Flow of control
b) Central flow
c) Selection
d) Iteration
Ans: a) Flow of control
4) How many times will the loop run?
i=2
while (i>0)
i=i-1
a) 2
b) 3
c) 1
d) 0
Ans: c) 1
5) While loop in python used for what type of iteration?
a) Indefinite
b) Definite
c) Discriminant
d) Infinity
Ans: a) Indefinite
B.) Fill in the blanks using the words from the help box:
[ Iteration,
Pass, conditional, statement ]
1) An empty statement in Python is Pass .
2) Iteration
is
another name of looping.
3) Statement that allow you to give conditions are called
conditional
.
4) Python support conditional
type of control structures.
C) State ‘T’ for
True or ‘F’ False:
1) Control statements control the flow of execution of
program. TRUE
2) ‘for’ is a conditional statement. FALSE
3) You can not use else in while loop. FALSE
4) A tuple is defined in a square bracket. FALSE
5) Range function used when loop is finished. FALSE
D) Short Ans type
questions:
1) What are conditional statements?
Ans: Conditional statements are programming constructs that
allow you to make decisions based on certain conditions.
2) Difference between ‘for’ loop
and while loop?
Ans: The 'for' loop is used for definite iteration, and the
'while' loop is used for indefinite iteration.
3) What are infinite loops?
Ans: Infinite loops are loops that continue indefinitely,
never reaching a stopping condition.
E) Long answer type
questions:
1) Differentiate between a tuple
and list?
Ans: A tuple is immutable (unchangeable), denoted
by parentheses, and a list is mutable (changeable), denoted by square brackets.
2) What are iterative statement in python?
Ans: Iterative
statements in Python are loops, like 'for' and 'while' loops, that allow the
execution of a block of code repeatedly.
F) Application
based questions:
1) Your friend wants to write a program to count the number
of even and odd numbers. Can you help your friend by writing the steps to do
so?
Ans: # Step 1: Initialize
the counters for even and odd numbers
even_count
= 0
odd_count
= 0
#
Step 2: Get the range of numbers from the user
start
= int(input("Enter the starting number: "))
end
= int(input("Enter the ending number: "))
#
Step 3: Iterate through the numbers in the given range
for
num in range(start, end + 1):
# Step 4: Check if the number is even or
odd
if num % 2 == 0:
# Step 5: If even, increment the even
counter
even_count += 1
else:
# Step 6: If odd, increment the odd
counter
odd_count += 1
#
Step 7: Display the results
print(f"Number
of even numbers: {even_count}")
print(f"Number
of odd numbers: {odd_count}")
Explanation
of each step:
1.
Initialize Counters: Create variables to
keep track of the count of even and odd numbers.
2.
Get User Input: Take input from the
user for the starting and ending numbers to define the range.
3.
Iterate through the Range: Use a for
loop to iterate
through the numbers in the specified range.
4.
Check for Even or Odd: Use the modulo
operator (%
) to check if the
current number is even or odd.
5.
Increment Even Counter: If the number is even,
increment the even counter.
6.
Increment Odd Counter: If the number is odd,
increment the odd counter.
7.
Display Results: After the loop
completes, display the counts of even and odd numbers.
2) Write a program to print following pattern.
1
12
123
1234
Ans: for i in range(1, 5):
for j in range(1, i + 1):
print(j, end="")
print()
G) Guess my name:
1) The
order of the execution of the statements in a program. Flow of control
2) I am a list denoted by square bracket. List
3) I am loop also called as entry-controlled loop. For loop
4) I am loop that continues forever, and never ends. Infinite loop
5) I am statement execute in one after another. Sequential statement
Tech Practice:
1) Print squares of first 5 natural numbers and cube of 5
natural numbers.
Ans: for i in range(1, 6):
print(f"Squared: {i**2}, Cubed:
{i**3}")
2) Write a program to guess a number between 1 to 9.
Ans: import random
secret_number
= random.randint(1, 9)
guess
= int(input("Guess the number between 1 and 9: "))
if
guess == secret_number:
print("Congratulations! You guessed
the correct number.")
else:
print(f"Wrong guess. The correct
number was {secret_number}.")
3) Write a program to find area of triangle.
Ans: base =
float(input("Enter the base of the triangle: "))
height
= float(input("Enter the height of the triangle: "))
area
= 0.5 * base * height
print(f"The
area of the triangle is: {area}")
4) Rama is PGT in a school. Her gross salary is Rs.
50,000. She donate 10% of her salary for the education of needy children write
a program to find out the 10% of her salary.
Ans: salary = 50000
donation_percentage
= 10
donation_amount
= (donation_percentage / 100) * salary
print(f"Rama's
donation amount is: Rs. {donation_amount}")
5) Demonstrate the students how to use conditional,
looping statements.
Ans: Provide examples and explain the use of
'if-else' statements, 'for' loops, and 'while' loops in solving problems.
6) Draw the pattern given below using programming.
1
22
333
4444
Ans: for i in range(1, 5):
for j in range(1, i + 1):
print(i, end="")
print()
7) Draw the pattern given below using programming.
1
12
123
1234
Ans: for i
in range(1, 5):
for j in range(1, i + 1):
print(j, end="")
print()
8) Draw the pattern given below using programming.
4444
333
22
1
Ans: for i in range(4, 0,
-1):
for j in range(1, i + 1):
print(i, end="")
print()
9) Draw the pattern given below using programming.
1234
123
12
1
Ans: for i in range(4, 0,
-1):
for j in range(1, i + 1):
print(j, end="")
print()
No comments:
Post a Comment