Viva Questions On Python With Answers Class 9 TH 17.02.2025
Viva Questions On Python With Answers Class 9 TH 17.02.2025
# 1. What is Python?
# Python is a high-level, interpreted, and general-purpose programming language.
# It is known for its simplicity and readability, which makes it an excellent choice for beginners
and professionals alike.
# Example:
print("Python is an interpreted, high-level programming language.")
# 2. Variable Assignment
# Variables are assigned using the assignment operator '='
# Example of assigning new values to variables
age = 30 # Reassigning a new value to the variable 'age'
name = "Alice" # Reassigning a new value to the variable 'name'
# 3. Dynamic Typing in Python
# Python is dynamically typed, which means that the type of a variable is determined at runtime.
# You don't need to specify the data type when declaring a variable.
# Example of dynamic typing
value = 10 # Initially 'value' is an integer
print(f"value is {value} and its type is {type(value)}")
value = "This is now a string" # Now 'value' is a string
print(f"value is {value} and its type is {type(value)}")
# 2. If Statement
# The 'if' statement is used to check a condition and execute a block of code if the condition is
true.
x = 10
y=5
# Example of 'if' statement
if x > y: # Condition checks if x is greater than y
print(f"{x} is greater than {y}")
# 3. If-Else Statement
# The 'if-else' statement provides an alternative code block to execute if the condition is false.
# Example of 'if-else' statement
if x < y:
print(f"{x} is less than {y}")
else:
print(f"{x} is not less than {y}")
# 4. Elif (Else If) Statement
# The 'elif' (else if) statement allows us to check multiple conditions sequentially.
# Once a condition is true, the rest of the 'elif' conditions are skipped.
z=7
# Example of 'if-elif-else' statement
if x > y:
print(f"{x} is greater than {y}")
elif z > y:
print(f"{z} is greater than {y}")
else:
print(f"{y} is the greatest number")
# 5. Nested Conditional Statements
# You can place one conditional statement inside another to check more complex conditions.
# Example of nested conditional statement
if x > y:
if z > y:
print(f"Both {x} and {z} are greater than {y}")
else:
print(f"Only {x} is greater than {y}")
else:
print(f"{x} is not greater than {y}")
# 1. What is a Looping Statement?
# Looping statements are used to repeatedly execute a block of code based on a condition.
# In Python, there are two primary types of loops: the 'for' loop and the 'while' loop.
# 2. For Loop
# The 'for' loop is used to iterate over a sequence (like a list, tuple, or string) and execute a block
of code for each element in that sequence.
# Example 1: Using for loop with a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # Iterating through each element in the list 'fruits'
print(fruit) # Printing each fruit
# Example 2: Using for loop with range() function
# The range() function generates a sequence of numbers.
for i in range(5): # Iterates through numbers from 0 to 4 (5 is exclusive)
print(i) # Printing each number
# 3. While Loop
# The 'while' loop executes a block of code as long as the condition is true. It repeats the loop
until the condition becomes false.
# Example 1: Using while loop with a condition
count = 0
while count < 3: # Loop continues as long as 'count' is less than 3
print(f"Count is {count}") # Printing the current value of 'count'
count += 1 # Incrementing 'count' to avoid infinite loop
# Example 2: Using while loop to sum numbers
sum = 0
num = 1
while num <= 5: # Loop will run as long as num is less than or equal to 5
sum += num # Add the current value of num to sum
num += 1 # Increment num by 1
print(f"Sum of numbers from 1 to 5 is {sum}")
Viva