Python Viva Theory Answers
Python Viva Theory Answers
A: The 'if' statement checks the first condition. If it's false, 'elif' checks the next condition. 'else' runs when
A: 'for' loop is used for iterating over a sequence (like a list, tuple). 'while' loop runs as long as a condition is
true.
A: An infinite loop runs forever unless stopped. To prevent it, ensure loop conditions eventually become false.
A: Functions are blocks of reusable code. Default arguments take a value if none is provided. Parameters are
inputs to functions.
A: Local variables are declared inside functions and not accessible outside. Global variables are accessible
everywhere.
Q: Return statement
A: Use open('filename', 'r') to read a file. Then use read() or readline() to get its content.
Q: Modes: r, w, a
Q: Exception handling
Q: Types of errors
Q: Try-except-else-finally
A: 'try' runs code, 'except' handles errors, 'else' runs if no error, 'finally' runs no matter what.
A: Packages are directories with __init__.py. Modules are .py files. Both help organize code.
Q: Creating a package
A: Make a directory, add __init__.py and other .py files. Import using 'import package.module'.
A: Encapsulation hides data. Use '_' for protected, '__' for private, nothing for public.
Q: Inheritance
A: Inheritance lets one class inherit from another. Example: class Child(Parent):
Q: Types of inheritance
Q: Polymorphism
A: Same method name, different behaviors. Example: len() works on list and string.
Q: Overloading vs Overriding
A: Overloading = same function name with different params (not native in Python). Overriding = subclass
A: Class variables are shared, instance variables are unique to each object.
Q: self keyword
6. Python Libraries
Q: NumPy
Q: Advantages of Pandas
A: Pandas offers data structures and tools for data manipulation and analysis.
Q: Matplotlib
A: NumPy arrays are faster and consume less memory than Python lists.