Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python_Viva_Questions

This document contains a comprehensive set of Python programming viva questions and answers covering various topics such as basics, conditional statements, looping, string manipulation, lists, tuples, dictionaries, functions, modules, file handling, and exception handling. It provides essential information about Python's features, syntax, and common practices. Each section includes specific questions with concise answers to aid in understanding Python programming concepts.

Uploaded by

ytsubscribers.sg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python_Viva_Questions

This document contains a comprehensive set of Python programming viva questions and answers covering various topics such as basics, conditional statements, looping, string manipulation, lists, tuples, dictionaries, functions, modules, file handling, and exception handling. It provides essential information about Python's features, syntax, and common practices. Each section includes specific questions with concise answers to aid in understanding Python programming concepts.

Uploaded by

ytsubscribers.sg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Programming Viva Questions and Answers

### Programming with Python: Basics


1. What is Python, and why is it popular?
- Python is a high-level, interpreted programming language known
for its simplicity and readability. Its popularity comes from its
versatility, extensive libraries, and active community support.

2. Who developed Python, and when?


- Python was developed by Guido van Rossum in 1991.

3. What are some key features of Python?


- Easy to learn and use, interpreted language, platform-independent,
supports object-oriented programming, has extensive libraries, and is
open-source.

4. How do you set up Python on your system?


- Download and install Python from python.org. Add Python to the
system PATH during installation for easy execution from the
command line.

5. What are the basic data types in Python?


- Integer, Float, String, Boolean, List, Tuple, Dictionary, and Set.

### Conditional Statements


6. What is the syntax for an if-else statement in Python?
if condition:
# code block
else:
# another code block

7. Can you nest if-else statements in Python? Provide an example.


- Yes, nested if-else statements are allowed.
if x > 0:
if x % 2 == 0:
print("Positive Even")
else:
print("Positive Odd")
else:
print("Non-positive")

### Looping
8. What are the differences between for and while loops in Python?
- A for loop is used when the number of iterations is known
beforehand (e.g., iterating over a list). A while loop is used when the
number of iterations depends on a condition.

9. What is a nested loop? Provide an example.


- A loop inside another loop is called a nested loop.
for i in range(3):
for j in range(3):
print(i, j)
10. What does the break statement do in a loop?
- It terminates the current loop prematurely.

### String Manipulation


11. How do you access individual characters in a string?
- Using indexing: string[index]
Example:
s = "Python"
print(s[0]) # Output: P

12. What is string slicing?


- Slicing extracts a part of the string.
s = "Python"
print(s[1:4]) # Output: yth

13. Name some string methods.


- upper(), lower(), strip(), split(), join(), find(), replace().

### Lists
14. How do you declare a list in Python?
- Using square brackets:
my_list = [1, 2, 3]

15. What are some common list methods?


- append(), extend(), insert(), remove(), pop(), sort(), reverse().

16. How can you access elements in a list?


- Using indexing and slicing:
my_list = [10, 20, 30]
print(my_list[1]) # Output: 20

### Tuples
17. What is the difference between a list and a tuple?
- A list is mutable (modifiable), while a tuple is immutable (cannot
be modified).

18. How can you access elements in a tuple?


- Using indexing and slicing, similar to lists.

### Dictionaries
19. What is a dictionary in Python?
- A dictionary is a collection of key-value pairs.
Example:
my_dict = {"name": "John", "age": 30}

20. How do you access values in a dictionary?


- Using keys:
print(my_dict["name"]) # Output: John

### Functions
21. What are the types of functions in Python?
- Built-in functions and user-defined functions.

22. What is an anonymous function?


- A function defined using the lambda keyword.
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5

23. What are function arguments?


- Arguments can be positional, keyword, default, or variable-length.

### Modules
24. How do you import a module in Python?
- Using the import keyword.
import math
print(math.sqrt(16))

25. What is the difference between a module and a package?


- A module is a single file, while a package is a collection of
modules organized in directories.

### File Handling


26. How do you open a file in Python?
- Using the open() function:
file = open("example.txt", "r")

27. What are the different file modes in Python?


- r (read), w (write), a (append), rb (read binary), etc.

28. How do you read and write data to a file?


- Use read() or write().
Example:
with open("example.txt", "w") as file:
file.write("Hello, World!")

### Exception Handling


29. What is exception handling in Python?
- A mechanism to handle runtime errors using try, except, finally.

30. Can you define a custom exception?


- Yes, by inheriting from the Exception class.
class MyError(Exception):
pass

You might also like