100+ Python Viva Questions and Answers for Students

By Btech Faqa

Published On:

100+ Python viva questions and answers for students

Join WhatsApp

Join Now

Python is one of the most popular programming languages used by students, developers, data scientists, and AI/ML engineers. It is also commonly included in B.Tech programming labs and practical examinations.

If you are preparing for a Python viva, Python practical exam, Python lab exam, or college interview, learning common viva questions can help you answer confidently.

This article contains 100+ Python viva questions and answers in simple language. The questions cover Python basics, variables, data types, operators, conditions, loops, strings, lists, tuples, dictionaries, sets, functions, OOP, exceptions, file handling, modules, and other important topics.

Python Viva Questions and Answers

1. What is Python?

Python is a high-level, interpreted, general-purpose programming language. It is known for its simple syntax and readability.

2. Who developed Python?

Python was created by Guido van Rossum. The first version of Python was released in 1991.

3. Why is Python popular?

Python is popular because it has:

  • Simple syntax
  • Easy-to-read code
  • Large standard library
  • Many third-party packages
  • Support for AI and Machine Learning
  • Support for web development
  • Large developer community

4. Is Python a programming language?

Yes. Python is a high-level general-purpose programming language.

5. Is Python interpreted or compiled?

Python is generally described as an interpreted language. Python source code is first compiled into bytecode and then executed by the Python virtual machine.

6. What is the extension of a Python file?

Python files normally use the .py extension.

Example:

hello.py

7. How do you print something in Python?

The print() function is used.

print("Hello World")

Output:

Hello World

8. How do you write a comment in Python?

A single-line comment starts with #.

# This is a comment
print("Hello")

9. What are variables in Python?

Variables are names used to store values.

name = "Ravi"
age = 20

10. Do we need to declare the variable type in Python?

No. Python automatically determines the type of a variable.

x = 10
name = "Python"

Here, x is an integer and name is a string.

Python Data Types Viva Questions

11. What are data types in Python?

Common Python data types include:

  • int
  • float
  • complex
  • str
  • list
  • tuple
  • set
  • dict
  • bool
  • NoneType

12. What is an integer?

An integer is a whole number without a decimal point.

x = 100

13. What is a float?

A float represents a number containing a decimal point.

price = 99.50

14. What is a string?

A string is a sequence of characters.

name = "Python"

15. What is a Boolean?

A Boolean has two values:

True
False

Example:

x = 10
print(x > 5)

Output:

True

16. What is None in Python?

None represents the absence of a value.

x = None

17. How can you check the data type of a variable?

Use the type() function.

x = 10
print(type(x))

Output:

<class 'int'>

18. What is type conversion?

Type conversion means changing one data type into another.

x = "10"
y = int(x)

Here, the string "10" is converted into an integer.

19. What is type casting?

Type casting is the process of converting a value from one data type to another.

Example:

x = float(10)

20. What is dynamic typing?

Python is dynamically typed, which means you do not have to specify the variable type while creating a variable.

x = 10
x = "Hello"

The same variable can refer to values of different types at different times.

Python Operators Viva Questions

21. What are operators?

Operators are symbols or keywords used to perform operations on values.

22. What are arithmetic operators?

Arithmetic operators include:

+
-
*
/
%
**
//

Example:

a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)

23. What is the modulus operator?

The % operator returns the remainder.

print(10 % 3)

Output:

1

24. What is the floor division operator?

The // operator performs floor division.

print(10 // 3)

Output:

3

25. What is the exponentiation operator?

The ** operator is used for powers.

print(2 ** 3)

Output:

8

26. What are comparison operators?

Comparison operators compare values.

Examples:

==
!=
>
<
>=
<=

27. What is the difference between = and ==?

= is an assignment operator.

x = 10

== compares two values.

x == 10

28. What are logical operators?

Python has three main logical operators:

and
or
not

Example:

age = 20

print(age > 18 and age < 30)

29. What are membership operators?

Membership operators check whether a value exists in a sequence.

They are:

in
not in

Example:

print("a" in "apple")

Output:

True

30. What are identity operators?

Identity operators are:

is
is not

They check whether two references refer to the same object.

Python Conditional Statements

31. What is an if statement?

An if statement executes code when a condition is true.

age = 20

if age >= 18:
    print("Adult")

32. What is an if-else statement?

It provides two possible execution paths.

age = 16

if age >= 18:
    print("Adult")
else:
    print("Minor")

33. What is elif?

elif means “else if”. It allows multiple conditions.

marks = 75

if marks >= 90:
    print("A")
elif marks >= 60:
    print("B")
else:
    print("C")

34. What is a nested if?

An if statement inside another if statement is called a nested if.

age = 20

if age >= 18:
    if age <= 25:
        print("Eligible")

Python Loops Viva Questions

35. What is a loop?

A loop repeatedly executes a block of code.

Python mainly provides:

  • for loop
  • while loop

36. What is a for loop?

A for loop is used to iterate over a sequence.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

37. What is a while loop?

A while loop executes as long as a condition is true.

i = 1

while i <= 5:
    print(i)
    i += 1

38. What is the range() function?

range() generates a sequence of numbers.

range(5)

generates numbers from 0 to 4.

39. What is the break statement?

break terminates a loop.

for i in range(10):
    if i == 5:
        break
    print(i)

40. What is continue?

continue skips the current iteration.

for i in range(5):
    if i == 2:
        continue
    print(i)

41. What is pass?

pass does nothing. It is used as a placeholder.

if True:
    pass

Python String Viva Questions

42. What is a string?

A string is a sequence of characters.

name = "Python"

43. How do you find the length of a string?

Use len().

name = "Python"
print(len(name))

Output:

6

44. What is string slicing?

String slicing extracts part of a string.

text = "Python"
print(text[0:3])

Output:

Pyt

45. Are strings mutable?

No. Python strings are immutable.

This means their contents cannot be directly changed after creation.

46. How do you convert a string to uppercase?

Use upper().

text = "python"
print(text.upper())

Output:

PYTHON

47. How do you convert a string to lowercase?

Use lower().

text = "PYTHON"
print(text.lower())

48. How do you remove spaces from a string?

The strip() method removes leading and trailing whitespace.

text = "  Python  "
print(text.strip())

49. How do you replace text in Python?

Use replace().

text = "I like Java"
print(text.replace("Java", "Python"))

50. What is string concatenation?

Combining two or more strings is called concatenation.

a = "Hello"
b = "World"

print(a + " " + b)

Python List Viva Questions

51. What is a list?

A list is an ordered and mutable collection.

numbers = [10, 20, 30]

52. Can a list contain different data types?

Yes.

data = [10, "Python", 5.5, True]

53. Are lists mutable?

Yes. List elements can be changed.

numbers = [10, 20, 30]
numbers[0] = 100

54. How do you add an item to a list?

Use append().

numbers = [10, 20]
numbers.append(30)

55. What is insert()?

insert() adds an item at a specific position.

numbers.insert(1, 15)

56. What is remove()?

remove() removes a specified value.

numbers.remove(20)

57. What is pop()?

pop() removes and returns an item from a list.

numbers.pop()

58. How do you sort a list?

Use sort().

numbers = [5, 2, 8, 1]
numbers.sort()
print(numbers)

59. How do you reverse a list?

You can use:

numbers.reverse()

60. What is list slicing?

List slicing extracts a portion of a list.

numbers = [1, 2, 3, 4, 5]
print(numbers[1:4])

Tuple Viva Questions

61. What is a tuple?

A tuple is an ordered and immutable collection.

numbers = (10, 20, 30)

62. What is the difference between a list and tuple?

The main difference is mutability.

List:

[1, 2, 3]

Lists are mutable.

Tuple:

(1, 2, 3)

Tuples are immutable.

63. Why are tuples useful?

Tuples are useful when you want a collection whose values should not be changed.

64. Can a tuple contain different data types?

Yes.

data = (10, "Python", 5.5)

Set Viva Questions

65. What is a set?

A set is an unordered collection of unique elements.

numbers = {1, 2, 3, 4}

66. Can a set contain duplicate values?

No. Duplicate values are automatically removed.

numbers = {1, 2, 2, 3}
print(numbers)

67. How do you add an item to a set?

Use add().

numbers.add(5)

68. What is set union?

Union combines elements from two sets.

a = {1, 2}
b = {2, 3}

print(a | b)

69. What is set intersection?

Intersection returns common elements.

print(a & b)

Dictionary Viva Questions

70. What is a dictionary?

A dictionary stores data in key-value pairs.

student = {
    "name": "Ravi",
    "age": 20
}

71. How do you access a dictionary value?

Use its key.

print(student["name"])

72. Can dictionary keys be duplicated?

No. Dictionary keys must be unique.

73. How do you add a new item to a dictionary?

student["course"] = "B.Tech"

74. How do you get dictionary keys?

Use keys().

print(student.keys())

75. How do you get dictionary values?

Use values().

print(student.values())

Python Functions Viva Questions

76. What is a function?

A function is a reusable block of code designed to perform a particular task.

77. How do you define a function?

Use the def keyword.

def greet():
    print("Hello")

78. How do you call a function?

greet()

79. What is a parameter?

A parameter is a variable defined in a function definition.

def greet(name):
    print(name)

Here, name is a parameter.

80. What is an argument?

An argument is the actual value passed to a function.

greet("Ravi")

81. What is return?

The return statement sends a value back from a function.

def add(a, b):
    return a + b

print(add(10, 20))

82. What is a default argument?

A default argument has a predefined value.

def greet(name="Student"):
    print(name)

83. What are keyword arguments?

Keyword arguments pass values using parameter names.

def student(name, age):
    print(name, age)

student(age=20, name="Ravi")

84. What is a lambda function?

A lambda is a small anonymous function.

square = lambda x: x * x

print(square(5))

Output:

25

Object-Oriented Programming Viva Questions

85. What is OOP?

OOP stands for Object-Oriented Programming. It organizes programs using classes and objects.

86. What is a class?

A class is a blueprint for creating objects.

class Student:
    pass

87. What is an object?

An object is an instance of a class.

class Student:
    pass

s1 = Student()

Here, s1 is an object.

88. What is a constructor?

A constructor is a special method used to initialize an object.

In Python, __init__() is commonly used for this purpose.

class Student:
    def __init__(self, name):
        self.name = name

89. What is self?

self refers to the current object.

class Student:
    def __init__(self, name):
        self.name = name

90. What is inheritance?

Inheritance allows one class to acquire properties and methods from another class.

class Animal:
    def speak(self):
        print("Animal sound")

class Dog(Animal):
    pass

91. What is polymorphism?

Polymorphism means the same interface or method name can work with different objects or types.

92. What is encapsulation?

Encapsulation means combining data and methods within a class and controlling access to the internal data.

93. What is abstraction?

Abstraction means hiding unnecessary implementation details and exposing only the required functionality.

Exception Handling Viva Questions

94. What is an exception?

An exception is an error that occurs during program execution.

Example:

print(10 / 0)

This produces a division-by-zero exception.

95. What is exception handling?

Exception handling allows a program to handle runtime errors without unexpectedly stopping.

96. What is try?

The try block contains code that may produce an exception.

try:
    x = 10 / 0
except:
    print("Error")

97. What is except?

except handles an exception generated inside the try block.

98. What is finally?

The finally block executes whether an exception occurs or not.

try:
    print("Hello")
except:
    print("Error")
finally:
    print("Completed")

99. What is raise?

raise is used to manually generate an exception.

raise ValueError("Invalid value")

File Handling Viva Questions

100. What is file handling?

File handling is used to create, read, write, and modify files using Python.

101. How do you open a file?

Use the open() function.

file = open("data.txt", "r")

102. What are common file modes?

Common modes include:

  • r – read
  • w – write
  • a – append
  • x – create
  • b – binary mode

103. How do you read a file?

file = open("data.txt", "r")
data = file.read()
print(data)
file.close()

104. How do you write to a file?

file = open("data.txt", "w")
file.write("Hello Python")
file.close()

105. Why is with open() useful?

It automatically handles closing the file.

with open("data.txt", "r") as file:
    data = file.read()

Python Module and Package Questions

106. What is a module?

A module is a Python file containing functions, classes, or variables that can be imported into another program.

107. How do you import a module?

import math

108. What is the math module?

The math module provides mathematical functions.

Example:

import math

print(math.sqrt(25))

109. What is a package?

A package is a collection of related Python modules organized together.

110. What is pip?

pip is a package management tool used to install Python packages.

Example:

pip install numpy

Important Python Viva Questions for Practical Exams

111. What is indentation in Python?

Indentation means spaces at the beginning of a line. Python uses indentation to define blocks of code.

Example:

if True:
    print("Python")

The indented statement belongs to the if block.

112. Why is indentation important in Python?

Unlike many programming languages that use braces to define blocks, Python uses indentation.

Incorrect indentation can result in an error.

113. What is PEP 8?

PEP 8 is the official style guide for Python code. It provides recommendations for writing readable and consistent Python programs.

114. What is __name__?

__name__ is a special built-in variable in Python.

A common pattern is:

if __name__ == "__main__":
    print("Program started")

This allows code to run when the file is executed directly rather than imported as a module.

115. What is recursion?

Recursion occurs when a function calls itself.

Example:

def countdown(n):
    if n == 0:
        return
    print(n)
    countdown(n - 1)

116. What is an iterator?

An iterator is an object that allows values to be accessed one at a time.

The iter() and next() functions are commonly associated with iterators.

117. What is a generator?

A generator is a special type of iterator that produces values lazily, usually using yield.

def numbers():
    yield 1
    yield 2
    yield 3

118. What is yield?

yield returns a value from a generator while preserving its state so execution can continue later.

119. What is a decorator?

A decorator is a function that modifies or extends the behavior of another function without changing its original code.

120. What is a virtual environment?

A virtual environment creates an isolated Python environment for a project.

It helps keep project dependencies separate.

Common Python Practical Viva Questions

121. How do you find whether a number is even or odd?

Use the modulus operator.

num = 10

if num % 2 == 0:
    print("Even")
else:
    print("Odd")

122. How do you find the largest of two numbers?

a = 10
b = 20

if a > b:
    print(a)
else:
    print(b)

123. How do you reverse a string?

One simple method is slicing:

text = "Python"
print(text[::-1])

124. How do you check whether a number is prime?

A prime number has exactly two positive factors: 1 and itself.

Example:

num = 7

for i in range(2, num):
    if num % i == 0:
        print("Not Prime")
        break
else:
    print("Prime")

125. How do you find the factorial of a number?

num = 5
fact = 1

for i in range(1, num + 1):
    fact *= i

print(fact)

Output:

120

126. What is a palindrome?

A palindrome is a value that reads the same forward and backward.

Example:

madam
121

127. How do you check a palindrome string?

text = "madam"

if text == text[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")

128. What is the Fibonacci series?

The Fibonacci series is a sequence where each number is the sum of the previous two numbers.

Example:

0 1 1 2 3 5 8 13

129. How do you swap two variables?

Python allows direct swapping.

a = 10
b = 20

a, b = b, a

130. How do you find the maximum value in a list?

Use max().

numbers = [10, 30, 20]
print(max(numbers))

131. How do you find the minimum value?

Use min().

print(min(numbers))

132. How do you calculate the sum of list elements?

Use sum().

numbers = [10, 20, 30]
print(sum(numbers))

Frequently Asked Python Viva Questions

133. What is Python used for?

Python is used for:

  • Web development
  • Data analysis
  • Artificial Intelligence
  • Machine Learning
  • Automation
  • Scientific computing
  • Scripting
  • Desktop applications
  • Education

134. What are Python libraries?

Libraries are collections of reusable code that provide functions and tools for specific tasks.

Examples include:

  • NumPy
  • Pandas
  • Matplotlib
  • Requests
  • OpenCV

135. What is NumPy?

NumPy is a Python library commonly used for numerical computing and working with arrays.

136. What is Pandas?

Pandas is a Python library commonly used for data manipulation and analysis.

137. What is Matplotlib?

Matplotlib is a Python library used to create charts and graphs.

138. What is OpenCV?

OpenCV is a computer vision library used for image and video processing.

139. What is a Python IDE?

An IDE is an Integrated Development Environment used for writing and running code.

Examples include:

  • IDLE
  • PyCharm
  • Visual Studio Code
  • Spyder

140. What is Jupyter Notebook?

Jupyter Notebook is an interactive environment commonly used for Python programming, data analysis, visualization, and machine learning.

Quick Revision: Top 20 Python Viva Questions

Before your practical exam, revise these questions first:

  1. What is Python?
  2. Who developed Python?
  3. What are Python data types?
  4. What is a variable?
  5. What is type casting?
  6. What is the difference between = and ==?
  7. What are conditional statements?
  8. What is a loop?
  9. Difference between for and while loop?
  10. What is a list?
  11. Difference between list and tuple?
  12. What is a dictionary?
  13. What is a set?
  14. What is a function?
  15. What is a lambda function?
  16. What is a class?
  17. What is an object?
  18. What is inheritance?
  19. What is exception handling?
  20. What is file handling?

Tips to Prepare for a Python Viva

Preparing for a Python viva is not only about memorizing answers. You should also understand how the code works.

1. Practice Basic Programs

Practice programs such as:

  • Even or odd
  • Prime number
  • Factorial
  • Fibonacci series
  • Palindrome
  • Armstrong number
  • Reverse a number
  • Sum of digits
  • Largest number
  • Sorting a list

2. Understand Python Syntax

Make sure you understand:

if
else
elif
for
while
def
class
try
except

3. Practice Lists and Dictionaries

Lists and dictionaries are frequently used in Python programs.

Practice:

append()
remove()
pop()
sort()
keys()
values()
items()

4. Understand Functions

Know the difference between:

  • Function
  • Parameter
  • Argument
  • Return value
  • Default argument
  • Keyword argument
  • Lambda function

5. Practice OOP

For practical exams, understand:

  • Class
  • Object
  • Constructor
  • self
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

6. Run Your Programs

Do not only read programs. Type them yourself and run them.

When you run programs, you can understand:

  • Syntax errors
  • Runtime errors
  • Logical errors
  • Output
  • Program flow

Common Python Viva Mistakes

Students often make mistakes during Python viva exams.

Mistake 1: Confusing = and ==

Remember:

=   Assignment
==  Comparison

Mistake 2: Forgetting indentation

Python uses indentation to identify code blocks.

Mistake 3: Confusing list and tuple

Remember:

List  → Mutable
Tuple → Immutable

Mistake 4: Not knowing the output

Examiners may show a short Python program and ask for its output. Practice tracing simple programs manually.

Mistake 5: Memorizing without understanding

Try to understand what each line does instead of memorizing only the final answer.

Frequently Asked Questions

Is Python difficult for beginners?

Python is generally considered beginner-friendly because its syntax is relatively simple and readable.

How many Python viva questions should I prepare?

For a practical exam, preparing 50–100 common questions along with basic programs can provide broad coverage of common topics.

Which Python programs should I practice for viva?

Practice programs such as factorial, Fibonacci, prime number, palindrome, Armstrong number, sorting, searching, string operations, list operations, and dictionary programs.

Are Python viva questions important for B.Tech students?

Yes. Python viva questions can help students prepare for programming lab practical examinations and oral assessments.

What topics should I study before a Python practical viva?

Focus on Python basics, data types, operators, conditions, loops, strings, lists, tuples, sets, dictionaries, functions, OOP, exception handling, file handling, modules, and basic Python programs.

Conclusion

Python viva preparation becomes easier when you understand the basic concepts and practice programs regularly.

This collection of 100+ Python viva questions and answers covers important topics including Python basics, variables, data types, operators, conditional statements, loops, strings, lists, tuples, sets, dictionaries, functions, OOP, exception handling, file handling, modules, and practical programming questions.

For your practical exam, do not focus only on memorizing definitions. Practice writing and executing Python programs yourself. Be comfortable explaining what each line of your program does and why you used a particular function, loop, condition, or data structure.

If you revise these questions and practice common Python programs, you will be better prepared for a Python lab viva, practical examination, B.Tech programming lab, or beginner-level Python interview.

🔴Related Post

Leave a Comment