0% found this document useful (0 votes)
63 views1 page

Python Lab Viva Questions and Answers

The document lists a series of Python viva questions covering fundamental concepts such as data types, string manipulation, loops, functions, and file handling. It also includes questions on advanced topics like regular expressions and exception handling. Each question prompts for explanations or examples to demonstrate understanding of Python programming.

Uploaded by

deekshasn18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views1 page

Python Lab Viva Questions and Answers

The document lists a series of Python viva questions covering fundamental concepts such as data types, string manipulation, loops, functions, and file handling. It also includes questions on advanced topics like regular expressions and exception handling. Each question prompts for explanations or examples to demonstrate understanding of Python programming.

Uploaded by

deekshasn18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Viva Questions

1. What are data types in Python? Name a few with examples.

2. How does string concatenation and replication work in Python?

3. How can you convert temperature from Fahrenheit to Celsius in Python?

4. Explain the use of conditional statements in Python with an example.

5. Write the syntax of a while loop. How is it different from a for loop?

6. What is a function in Python? How do you call a function?

7. How does a recursive function work? Give an example.

8. Explain how lists work in Python. How do you add, remove and access elements?

9. What is the difference between a list and a dictionary in Python?

10. How would you calculate mean, variance and standard deviation from a list of numbers?

11. What are string manipulation functions in Python? Name a few with examples.

12. How can you validate user input in Python?

13. What is a regular expression? How is it used in Python?

14. How can you retrieve lines from a file that match a particular pattern?

15. Write a Python regex to convert a date from 'YYYY-MM-DD' to 'DD-MM-YYYY'.

16. What is file handling in Python? Name different file operations.

17. How do you read, write and append to a text file in Python?

18. How can you identify misspelled words in a text file using Python?

19. What is the use of 'with open() as' syntax in file handling?

20. How would you handle exceptions in Python? Give an example.

Common questions

Powered by AI

Python uses open() function along with read(), write(), and append() methods to handle file operations. To read from a file, the mode 'r' is used, e.g., file = open('file.txt', 'r'). For writing, 'w' or 'a' can be used based on whether you wish to overwrite or append, e.g., file.write('new content'). It's crucial to always close the file using file.close() or use the 'with open() as' context manager for automatic management .

Regular expressions in Python are powerful tools for pattern matching and text processing. They are widely used in file processing tasks to search, replace, and manipulate strings based on specific patterns. Python's 're' module provides support for regular expressions enabling tasks like extracting emails from text files or formatting data correctly with minimal code .

Lists in Python are ordered collections of elements accessed by integer indices, e.g., list = [1, 2, 3]. They are highly versatile and can store mixed data types. Dictionaries, however, store key-value pairs with unique keys for fast lookups, e.g., dict = {'key1': 'value1'}. Dictionaries are optimal for associative arrays where retrieval by key is frequent, while lists are more suitable for ordered sequences .

String concatenation in Python refers to joining one string with another, which can be done using the '+' operator, e.g., 'Hello' + ' World' results in 'Hello World'. String replication, on the other hand, involves repeating a string multiple times using the '*' operator, such as 'Hi' * 3 which yields 'HiHiHi' .

The 'with open() as' syntax in Python provides a more efficient way to handle files. It ensures that files are properly closed after their suite finishes, even if an exception is raised, thereby reducing the risk of file corruption and ensuring resources are released cleanly. However, the automatic file closure can obscure exactly when the file closes, sometimes making it unsuitable in complex workflows requiring precise resource management .

Python provides a variety of string manipulation functions. Examples include str.upper() to convert all characters to uppercase, str.lower() for converting to lowercase, str.strip() to remove whitespace, and str.replace() for replacing specified parts of a string with another string. These functions are widely used for formatting and cleaning string data .

In Python, data types define the kind of data a variable can hold. Common data types include: integers, which hold numeric values like 5; floats, which hold decimal numbers such as 10.5; strings, which contain sequences of characters, for example 'hello'; and booleans, which represent True or False values .

In Python, user input validation is typically performed using conditional statements combined with input functions to ensure the input meets certain criteria before processing it. For example, a loop can repeatedly prompt for input until a valid value is entered. Also, try/except blocks can catch exceptions to handle invalid inputs without crashing the program .

Exception handling in Python, implemented using try, except, finally blocks, increases program reliability by allowing programs to continue execution or gracefully terminate in the face of errors. Common practices include catching specific exceptions instead of general ones, using finally blocks for cleanup, and providing informative descriptions of the error for debugging purposes. This helps prevent abrupt crashes and aids in debugging .

A recursive function in Python is one that calls itself in order to divide a problem into smaller, more manageable sub-problems. An example is the computation of factorials: a function factorial(n) calls itself as factorial(n-1) until it reaches the base case, factorial(0), which is 1. The recursion stops when the base case is reached, preventing infinite loops .

You might also like