Python Viva
Python Viva
A: Python was introduced by Guido van Rossum in 1991. He developed it as a high-level, easy-to-read, and versatile
programming language.
A:
Indentation in Python refers to the spaces or tabs at the beginning of a line that define the block of code. Unlike
many other programming languages that use curly braces {} to mark code blocks, Python uses indentation to
group statements.
Q: What is an operator in Python?
A:
An operator in Python is a symbol that performs a specific operation on one or more operands (values or
variables). Operators are used to perform operations like addition, subtraction, comparison, logical
operations, etc.
Q6: What is the difference between a list and a tuple?
A:
Intermediate Level
A:
A list in Python is an ordered, mutable (changeable) collection of items. Lists can store elements of different data
types such as integers, strings, or even other lists. Lists are defined using square brackets [].
Lists support indexing, slicing, and many built-in methods (like append(), remove(), sort(), etc.).
Q: Define tuple in Python.
A:
A tuple in Python is an ordered, immutable (unchangeable) collection of items. Tuples can store elements
of different data types. Once a tuple is created, its elements cannot be modified. Tuples are defined using
parentheses ().
Tuples support indexing and slicing but do not allow item assignment or modification.
: Define dictionary in Python.
A:
A dictionary in Python is an unordered collection of key-value pairs. Each key in a dictionary is unique,
and each key maps to a value. Dictionaries are mutable, which means you can change, add, or remove
items. Dictionaries are defined using curly braces {}.
Keys must be immutable (e.g., strings, numbers, tuples).
Values can be of any data type.
Q1: What is a module in Python?
A:
A module is a file containing Python code (functions, classes, variables) that can be imported and reused
in other Python programs. It helps in code organization and reusability.
Example: math, os, sys are built-in modules.
A:
A function is a reusable block of code that performs a specific task. It helps to divide a program into small,
manageable parts.
A:
Using the def keyword:
python
CopyEdit
def function_name(parameters):
# function body
return result
Q3: What is the difference between built-in functions and user-defined functions?
A:
A:
A:
GUI (Graphical User Interface) allows users to interact with applications visually using buttons, menus, text
boxes, etc., instead of typing commands. Python provides several libraries to create GUI applications.
A:
Tkinter (built-in)
PyQt / PySide
Kivy
wxPython
A:
Tkinter is Python’s standard built-in GUI library. It provides tools to create windows, labels, buttons, text fields,
etc., easily.
A:
In GUI, event-driven programming means the program waits for user actions (events) and responds
accordingly (e.g., clicking a button triggers a function).
Q: Define range() function in Python.
A:
The range() function in Python returns a sequence of numbers. It is commonly used in loops to iterate a specific
number of times.
Syntax:
python
CopyEdit
range(start, stop, step)
A:
An array is a collection of items stored at contiguous memory locations. It is used to store multiple items of the
same data type together.
In Python, we usually use lists instead of arrays for most purposes. But if we need arrays (with better
performance for large numeric data), we can use:
A:
A loop is used to execute a block of code repeatedly as long as a condition is true.
A:
Python supports two main types of loops:
for loop
while loop
A:
The for loop is used to iterate over a sequence (like list, tuple, string, or range).
Example:
python
CopyEdit
for i in range(5):
print(i)
A:
The while loop continues to execute as long as the condition is true.
Example:
python
CopyEdit
i = 0
while i < 5:
print(i)
i += 1
A:
break is used to exit the loop prematurely when a certain condition is met.
Example:
python
CopyEdit
for i in range(10):
if i == 5:
break
print(i)
A:
continue skips the current iteration and moves to the next iteration.
Example:
python
CopyEdit
for i in range(5):
if i == 2:
continue
print(i)
A:
The else block after a loop executes when the loop completes normally (without break).
Example:
python
CopyEdit
for i in range(3):
print(i)
else:
print("Loop finished")
A:
Yes, we can use loops inside another loop.
Example:
python
CopyEdit
for i in range(3):
for j in range(2):
print(i, j)
A:
A loop that never ends because its condition always remains true.
Example:
python
CopyEdit
while True:
print("Infinite")