Basic Python Viva Questions and Answers
Q: What is Python?
A: Python is a high-level, interpreted programming language known for its simplicity and readability.
Q: What are variables in Python?
A: Variables are containers for storing data values. You don't need to declare their type explicitly.
Q: What is a data type?
A: A data type defines the type of value a variable holds, such as integer, float, or string.
Q: List different data types in Python.
A: int, float, str, bool, list, tuple, dict, set, complex.
Q: What is the difference between int and float?
A: int represents whole numbers, while float represents numbers with decimal points.
Q: How do you write a comment in Python?
A: Single-line comments start with #, and multi-line comments use triple quotes.
Q: What is a function?
A: A function is a block of reusable code that performs a specific task.
Q: What is the use of print() function?
A: It is used to display output on the screen.
Q: How do you take input from a user?
A: Using the input() function.
Q: What is the type() function used for?
A: It returns the data type of the object passed to it.
Q: What is an if-else statement?
A: It is a control flow statement used to execute blocks of code based on a condition.
Q: What is the syntax of a while loop?
A: while condition:
# code block
Q: What is the difference between for and while loop?
A: for is used to iterate over sequences; while runs as long as a condition is true.
Q: Does Python have switch statements?
A: No, Python does not support switch statements; use if-elif-else instead.
Q: What is indentation in Python?
A: Indentation indicates a block of code and is mandatory in Python.
Q: How do you define a function?
A: Using the def keyword: def function_name():
Q: What is return used for?
A: It sends back the result from a function to the caller.
Q: What is a lambda function?
A: A lambda function is an anonymous, one-line function defined using the lambda keyword.
Q: Can a function return multiple values?
A: Yes, using tuples or lists.
Q: What is a global variable?
A: A variable declared outside of all functions, accessible throughout the program.