0% found this document useful (0 votes)
21 views10 pages

Viva Questions On Python With Answers Class 9 TH 17.02.2025

This document provides a comprehensive overview of Python programming concepts, including definitions and examples of variables, expressions, tokens, data types, conditional statements, and looping statements. It also covers lists, their creation, manipulation, and differences from tuples. The content is structured as a series of questions and answers, making it suitable for interview preparation or study.

Uploaded by

tandonkannav10
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
0% found this document useful (0 votes)
21 views10 pages

Viva Questions On Python With Answers Class 9 TH 17.02.2025

This document provides a comprehensive overview of Python programming concepts, including definitions and examples of variables, expressions, tokens, data types, conditional statements, and looping statements. It also covers lists, their creation, manipulation, and differences from tuples. The content is structured as a series of questions and answers, making it suitable for interview preparation or study.

Uploaded by

tandonkannav10
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
You are on page 1/ 10

# Viva Questions on Python with Answers

# 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. What is a Variable in Python?


# A variable in Python is used to store data values. It is a name given to a memory location that
stores a value.
# Python is dynamically typed, meaning you don't have to declare the type of the variable
explicitly.
# Example:
x = 5 # Variable x stores the integer value 5
y = "Hello, Python!" # Variable y stores the string value
print(x, y)

# 3. What is an Expression in Python?


# An expression is a combination of variables, operators, and values that evaluates to a single
value.
# Example:
expression_result = x + 10 # This is an expression where 10 is added to the variable x.
print(f"Expression result: {expression_result}")

# 4. What are Tokens in Python?


# Tokens are the smallest units in Python. The key tokens include:
# - Keywords (like `if`, `else`, `for`, `def`)
# - Identifiers (names of variables, functions, classes, etc.)
# - Literals (values like 5, "hello", etc.)
# - Operators (like `+`, `-`, `*`, `/`)
# - Delimiters (like `()`, `{}`, `[]`, `:`, `,`)
# Example:
a = 10 # 'a' is an identifier, and 10 is a literal value (token).
b = 20 # Another identifier and literal.
result = a + b # '+' is an operator token
print(f"Tokens Example: {a} + {b} = {result}")

# 5. Data Types in Python


# Python has several built-in data types, including:
# - int: Integer values (whole numbers)
# - float: Floating point numbers (decimal values)
# - str: String (text)
# - bool: Boolean values (True or False)
# - list: A collection of ordered elements
# - tuple: An immutable collection of ordered elements
# - set: An unordered collection of unique elements
# - dict: A collection of key-value pairs (associative array)

# Example demonstrating various data types:


integer_example = 42 # int
float_example = 3.14 # float
string_example = "Hello" # str
boolean_example = True # bool
list_example = [1, 2, 3, 4] # list
tuple_example = (5, 6, 7) # tuple
set_example = {8, 9, 10} # set
dict_example = {"key1": "value1", "key2": "value2"} # dict

# Variable Declaration in Python


# 1. Declaring a Variable
# In Python, you do not need to explicitly declare a variable type.
# The variable is automatically created when you assign a value to it.
# Example of variable declaration
age = 25 # 'age' is a variable storing an integer value
name = "John" # 'name' is a variable storing a string value
is_student = True # 'is_student' is a variable storing a boolean value

# 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)}")

# 4. Variable Naming Rules in Python


# - Variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_)
# - A variable name cannot start with a number.
# - Python keywords (e.g., 'if', 'while', 'class') cannot be used as variable names.
# Examples of valid and invalid variable names:
valid_variable = "Valid" # Valid
_invalid_variable = "Invalid" # Valid (although not recommended to start with an underscore)
var123 = "Valid" # Valid
# 5. Multiple Variable Assignment
# Python allows multiple variables to be assigned values in a single line.
# Example of multiple variable assignment
x, y, z = 5, 10, 15 # Assigning 5 to x, 10 to y, and 15 to z
print(f"x = {x}, y = {y}, z = {z}")
# 6. Constants in Python (convention)
# Python doesn't have built-in support for constants (immutable values).
# However, by convention, variables that should be constants are written in uppercase.
# Example of a constant
PI = 3.14159 # Conventionally, this variable is treated as a constant
print(f"PI = {PI}")

# Conditional Statements in Python


# 1. What is a Conditional Statement?
# A conditional statement is used to execute a block of code only if a condition is true.
# It allows us to make decisions based on conditions (i.e., whether a condition is true or false).

# 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

# Q1: What is a List in Python?


# A List in Python is a collection of ordered, changeable, and indexed elements. Lists are defined
by placing the elements inside square brackets `[]` and separating them with commas.
my_list = [1, 2, 3, 'Python', 4.5]
print("Example List:", my_list)
# Q2: How do you create a list in Python?
# A list is created by placing elements inside square brackets `[]`.
empty_list = []
print("Empty List:", empty_list)
my_list = [1, 'hello', 3.14, True]
print("List with multiple data types:", my_list)
# Q3: What is the difference between a list and a tuple in Python?
# - A List is mutable (can be modified).
# - A Tuple is immutable (cannot be modified after creation).
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
# Example of list modification:
my_list[0] = 10
print("Modified List:", my_list)
# The following would raise an error because tuples are immutable:
# my_tuple[0] = 10
# Q4: How can you access elements of a list in Python?
# List elements are accessed using indexing. Index starts from 0.
print("First element of the list:", my_list[0])
print("Last element of the list:", my_list[-1])
# Q5: How do you slice a list in Python?
# Slicing a list returns a subset of the list.
my_list = [1, 2, 3, 4, 5]
print("Sliced List (1:4):", my_list[1:4])
# Q6: How can you add elements to a list in Python?
# You can add elements using the following methods: append(), insert(), extend()
# Using append() to add an element at the end
my_list.append(6)
print("List after append:", my_list)
# Using insert() to add an element at a specific position
my_list.insert(2, 10)
print("List after insert:", my_list)
# Using extend() to add multiple elements
my_list.extend([7, 8, 9])
print("List after extend:", my_list)
# Q7: How do you remove elements from a list?
# You can remove elements using remove(), pop(), del, or clear()
my_list.remove(10) # Removes the first occurrence of 10
print("List after remove:", my_list)
removed_element = my_list.pop(1) # Removes the element at index 1 and returns it
print("Removed element:", removed_element)
print("List after pop:", my_list)
# Using del to delete an element at a specific index
del my_list[2]
print("List after del:", my_list)
# Using clear() to remove all elements
my_list.clear()
print("List after clear:", my_list)
# Q9: How do you find the length of a list in Python?
# You can find the length using the len() function.
print("Length of the list:", len(my_list))
# Q11: What is the difference between append() and extend() in Python?
# - append() adds a single element to the list.
# - extend() adds multiple elements from an iterable to the list.
# Using append
my_list = [1, 2]
my_list.append([3, 4])
print("List after append:", my_list) # The list becomes: [1, 2, [3, 4]]
# Using extend
my_list = [1, 2]
my_list.extend([3, 4])
print("List after extend:", my_list) # The list becomes: [1, 2, 3, 4]
# Q12: How can you sort a list in Python?
# You can sort a list using sort() or sorted().
my_list = [3, 1, 4, 2]
# Using sort() to sort the list in place
my_list.sort()
print("List after sort:", my_list)
# Using sorted() to get a sorted copy of the list
sorted_list = sorted(my_list)
print("Sorted copy of the list:", sorted_list)

You might also like