0% found this document useful (0 votes)
13 views5 pages

Python Viva

Uploaded by

anushaghosh2003
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)
13 views5 pages

Python Viva

Uploaded by

anushaghosh2003
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/ 5

1.

Introduction to Python:

 What is Python, and why is it popular?


Python is a high-level, interpreted language known for its readability and wide usage in
various fields.
 How is Python an interpreted language?
Python code is executed line-by-line by an interpreter, not precompiled.
 Can you explain the difference between Python 2 and Python 3?
Python 3 introduced improvements like print() function, better Unicode handling, and
integer division.
 What are Python's data types?
Common types include int, float, string, list, tuple, dict, set, and bool.
 Explain the significance of indentation in Python.
Indentation defines code blocks in Python, unlike braces {} in other languages.
 How do you write comments in Python?
Single-line with #, multi-line with '''...''' or """...""".
 What are some common IDEs or code editors for Python programming?
PyCharm, VS Code, Jupyter Notebook, IDLE.
 What is PEP 8, and why is it important?
PEP 8 is Python's style guide, helping to keep code clean and readable.
 How do you install external libraries in Python?
Use pip install <library-name>.
 What is the difference between == and = in Python?
= assigns a value, == checks equality.
 Explain mutable and immutable types in Python.
Mutable types (e.g., lists) can change; immutable types (e.g., tuples) cannot.
 What are modules and packages in Python?
Modules are files with Python code, and packages are directories containing multiple
modules.
 How do you handle errors in Python?
Use try-except blocks.
 What is the purpose of None in Python?
None represents the absence of a value.
 What is the difference between list and tuple?
Lists are mutable, tuples are immutable.
 Explain the use of functions in Python.
Functions encapsulate code for reuse and organization.
 How do you convert data types in Python?
Use functions like int(), float(), str(), list(), etc.

2. Conditional Statements:

 What are conditional statements in Python?


They allow code to execute based on conditions, using if, elif, and else.
 Explain the syntax of the if statement in Python.
python
Copy code
if condition:
# code

 What is the difference between if, elif, and else statements?


if checks a condition, elif checks another if the first is false, else runs if all are false.
 Can you use multiple conditions in an if statement? How?
Yes, using and, or, or not.
 What is a nested if statement?
An if inside another if.
 How do you implement a switch-case-like structure in Python?
Use if-elif statements or dictionaries.
 How does Python handle Boolean values?
Booleans are True and False.
  What is the difference between and and or in Python?
and requires both conditions to be true; or requires at least one.
  How does the not operator work in Python?
not inverts a Boolean value, making True False and vice versa.
  Can you use an if statement without an else? Why or why not?
Yes, else is optional if there’s no alternate action needed.
  How do you check if a value is within a range?
Use comparison operators, e.g., if 1 <= x <= 10.
  What is the purpose of chaining conditions with and/or?
To check multiple conditions in a single statement.
  How does Python evaluate expressions with multiple conditions?
Left to right, with and and or having different priorities.
  Can you use an else statement with just an if without elif?
Yes, elif is optional; you can use just if-else.
  How would you write an if statement that checks multiple conditions?
Use if condition1 and condition2: or if condition1 or condition2:.

3. Loops:

 What is a loop in programming?


A loop repeats code multiple times.
 What are the main types of loops in Python?
for and while.
 Explain the syntax of a for loop with an example.

python
Copy code
for variable in iterable:
# code
 What is the difference between for and while loops?
for is for fixed iterations, while runs until a condition is false.
 Can you use an else block with loops?
Yes, it runs if the loop doesn’t hit break.
 How do you exit a loop prematurely in Python?
Use break.
 What is the role of the continue statement?
Skips to the next iteration.
 How does a nested loop work?
A loop inside another loop, running multiple layers.
 How do you loop through a list in Python?
Use for item in list:.
 What is an infinite loop? How do you create one?
A loop that never ends, e.g., while True:.
 What is the range function, and how is it used in loops?
range() generates a sequence of numbers, often used in for loops.
 How can you loop through a dictionary in Python?
Use for key, value in dict.items():.
 What is the purpose of enumerate() in loops?
Provides both index and value when looping through an iterable.
 What is a do-while loop, and does Python have it?
Python lacks do-while, but similar behavior can be achieved with while.
 How do you reverse a loop in Python?
Use reversed(), e.g., for i in reversed(range(n)).
 Can you nest a for loop inside a while loop, and vice versa?
Yes, Python allows nested loops.
 What is the difference between break and return in a loop?
break exits the loop; return exits the function.
 How can you skip an iteration in a loop?
Use continue to skip to the next iteration.

4. Patterns:

 How do you create patterns in Python?


Use nested loops, with outer for rows and inner for columns.
 Write a simple program to print a triangle pattern with *.

python
Copy code
for i in range(1, 6):
print("*" * i)

 What are some common patterns created with loops?


Triangles, squares, pyramids, and number patterns.
 How do you use nested loops to create complex patterns?
Outer loop for rows, inner loop for columns, customizing based on pattern needs.
 How would you print a pattern that requires a change of character?
Adjust loops to print based on row/column count.
 Can you explain a reverse triangle pattern and provide code?

python
Copy code
for i in range(5, 0, -1):
print("*" * i)

 Write a program to print the pattern:

yaml
Copy code
1
12
123
1234
python
Copy code
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end="")
print()

 How would you print a square pattern with * in Python?

python
Copy code
for i in range(n):
print("*" * n)

 Write a program to print the following pattern:

Copy code
1
2 3
4 5 6
7 8 9 10
python
Copy code
num = 1
for i in range(1, 5):
for j in range(i):
print(num, end=" ")
num += 1
print()

 How would you print a pyramid pattern in Python?

python
Copy code
for i in range(1, n+1):
print(" " * (n-i) + "*" * (2*i-1))
 Write a program to print an inverted pyramid pattern.

python
Copy code
for i in range(n, 0, -1):
print(" " * (n-i) + "*" * (2*i-1))

 How would you create a diamond shape using nested loops?


Combine a pyramid and inverted pyramid pattern.
 What is the logic for printing a checkerboard pattern?
Alternate * and (spaces) in rows and columns.
 How do you print patterns with alphabets (e.g., A, B, C, ...)?
Use chr(65 + j) for letters, where 65 is ASCII for 'A'.
 Write a pattern that alternates characters, like a zigzag.
Use conditional checks within the inner loop to alternate characters.
 Explain how to print a hollow square pattern.
Print * for borders (first and last rows/columns) and spaces for inner cells.
 How do you print numbers in descending order in a pattern?
Use a decrementing sequence in the loop.

You might also like