0% found this document useful (0 votes)
17 views3 pages

Python Programming Question Bank

The document outlines a comprehensive introduction to Python programming, divided into five modules covering essential topics such as data types, flow control, functions, file handling, and object-oriented programming. Each module includes detailed explanations, examples, and programming tasks to reinforce learning. Key concepts include variable definitions, string methods, list and dictionary operations, and the use of modules like shutil and logging.
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)
17 views3 pages

Python Programming Question Bank

The document outlines a comprehensive introduction to Python programming, divided into five modules covering essential topics such as data types, flow control, functions, file handling, and object-oriented programming. Each module includes detailed explanations, examples, and programming tasks to reinforce learning. Key concepts include variable definitions, string methods, list and dictionary operations, and the use of modules like shutil and logging.
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

Introduction to Python programming (BPLCK205B/ 105B)

***Lab programs are most important***


MODULE - I

1. Explain how to evaluate an expression into the interactive shell with examples.
2. Explain different data types with an example.
3. Briefly explain string concatenation and replication with examples.
4. Define variables. Explain the different rules to define a variable with examples.
5. Explain print (), input (), len (), str (), int (), and float () functions with examples.
6. Explain Boolean values with examples.
7. Explain comparison and Boolean operators with examples.
8. Solve the following expression: 2+2==4 and not 2+2 == 5 and 2*2 == 2+2
9. Explain the different flow control statements with its flowchart and examples.
10. Explain the difference between break and continue statements with examples.
11. Explain the different arguments passed to the range () function with example.
12. Briefly explain how to import modules with examples.
13. Explain how to terminate program early with [Link]() function with example.
14. Define function. Explain def statements with its parameters with example.
15. Explain briefly about return values and return statements with example.
16. Explain the different keyword arguments passed to the print() function.
17. Explain elif, for , while , break and continue statements in python with examples for
each and flowchart.
18. What is a function? How to define a function in python? How can we pass the parameters to the
user defined function explain with an example
19. List the rules to define a variable in python? What is local and global scope of variable in
Python. Explain the different scenarios with an example snippet.
20. How python handles the exception? Explain with an example program
21. Differentiate between parameters and arguments. Illustrate the flow of execution of a
python function with an example program
22. Demonstrate with an example len(), Int(), str() and Range()
23. List and explain operators used in python with a suitable example

MODULE – 2

1. Explain tuple data types with examples and Briefly explain how to convert types using list() and tuples()
functions.
2. Explain List data type with examples and Explain List concatenation and replication with examples.
3. Explain get() and setdefault() methods with examples.
4. Explain copy() and deepcopy() functions of copy module.
5. Explain nested dictionary with example.
6. Explain mutable and immutable data types with examples.
7. Explain append() and index() functions with respect to lists in Python.
8. Explain different ways to delete an element from a list with suitable Python syntax and programming
examples.
9. Explain keys(), values(), and items() methods of dictionaries with examples.
10. What are the different methods supports in python List. Illustrate any 3 methods with an example.
11. Demonstrate the working of tic-tac-toc board using Dictionaries
12. How tuples are created in python? Explain the different ways of accessing and creating
them
13. Discuss get(), Item(), values() and keys() dictionary methods in python with example
for each.
14. Explain (i) copy and deepcopy() (ii) passing references (iii) list Concatenation and
replication
15. Compare and contrast lists, tuples and dictionaries

MODULE – 3

1. Explain python string handling methods with examples: split(), endswith(), ljust(), center(), lstrip(), join(),
startswith(), rjust(), strip(), rstrip().
2. Explain with suitable python program segments: a) [Link]() b) [Link]()
c) [Link]() d) [Link]().
3. Develop a python program to find the total size of all the files in the given directory.
4. Develop a python program to read and print the contents of a text file.
5. Explain the three steps for reading or writing files in python with examples.
6. Explain how to save variables using [Link]() functions with suitable code snippet.
7. Explain the different functions for finding the size of a file and its folder contents with examples.
8. Explain indexing and slicing technique using strings with examples.
9. Explain string methods the upper(), lower(), isupper(), and islower().
10. List out all the useful string methods which supports in python. Explain with an example for each method.
12. What are the key properties of a file? Explain in detail file reading/writing process with an example of
python program
13. With code snippet, Explain saving variables using the shelve module and pprint pfromat() functions
14. Write an algorithm to implement password locker functionality
15. Develop a program to find total size of all the files in the directory
16. Discuss the different paths in the file system
17. Explain in briefly, what are the different methods of file operations supports in python shutil module.

MODULE – 4

1. Explain the different functions provided by the shutil module with examples.
2. Explain the different functions for permanently deleting files and folders with examples.
3. Briefly explain about [Link] function to walk through the entire directory tree with examples.
4. Explain send2trash module function with examples.
5. Explain how to read and extract the contents from Zip file using zipfile module with examples.
6. Explain the support for Logging with logging module in Python.
7. List and explain the logging levels in python
8. What is meant by compressing file? Explain creating,reading,extracting zip file with code snippet
9. Explain a role of assertions in python with a suitable example code
10. What is the use of ZIP? How to create a Zip folder explain.
11. Explain the functions with examples: (i) [Link] (ii) [Link] (iii) [Link]()
12. Explain the buttons in of IDLE Debugger window
MODULE – 5
1. Explain init and str method with an example python program
2. Explain program development concept ‘prototype & patch’ with suitable example.
3. Explain the following with syntax & code snippet
a) Class definition b) instantiation c) passing an instance (or objects) as an argument
d) instances as return values
4. Define polymorphism. Demonstrate polymorphism with function to find histogram
to count the numbers of times each letter appears in a word and in sentence
5. What is type based dispatch? Illustrate with python program.
6. What is a class? How to define class in python? How to initiate a class and how the
class members are accessed?
7. Demonstrate pure function and modifiers with examples.
8. Explain operator overloading and polymorphism with examples.
9. Write a function called print time that takes a time object and print it in the form

Common questions

Powered by AI

In Python, string concatenation can be performed using the '+' operator, which merges strings together but creates a new string object, impacting performance due to increased memory usage with large strings or repeated concatenations within loops . Alternatively, using ''.join()' is more efficient for concatenating multiple strings, like in a list, as it processes them in a single pass . String replication with '*' allows a string to be repeated a specified number of times, creating a new string each time, which can affect performance with very large results or excessive repetitions. Both methods affect readability, where '+' is intuitive for simple operations, while ''.join()' enhances readability in more complex scenarios like joining list elements . Each approach requires balancing simplicity against efficiency depending on context and frequency of operation use .

Mutable types, such as lists, allow modifications after their creation, which means elements can be added, removed, or changed without creating a new object . Conversely, immutable types, such as tuples, do not allow changes once created; instead, any 'change' creates a new object entirely . This difference affects memory usage and performance. For example, using a list for data that frequently changes can be more efficient due to their ability to change in place, whereas using a tuple can be more appropriate for fixed collections of items. The choice between mutable and immutable types also impacts how functions handle these objects, as immutable objects are safer from inadvertent modification when passed as arguments .

The 'shutil' module in Python provides several high-level file and directory handling functions that simplify tasks such as copying and deleting files . The 'shutil.copytree()' function can recursively copy a directory tree, preserving metadata, while 'shutil.move()' can move a file or directory to another location . For deleting files or directories, 'shutil.rmtree()' can remove an entire directory tree. These functions abstract away the complexity of file manipulation, allowing developers to handle files with simple commands . Using 'shutil.copyfile()', for instance, makes it straightforward to copy files, improving code readability and reducing error-prone code .

Comparison operators evaluate the values to determine their relation, while Boolean operators such as 'and', 'or', and 'not' evaluate the truth value of expressions . The given expression '2+2==4 and not 2+2 == 5 and 2*2 == 2+2' checks multiple conditions: '2+2==4' is True, and 'not 2+2==5' negates the Falsehood of '2+2==5', resulting in True. The '2*2==2+2' checks if both sides of the multiplication and addition yield the same result, which is True. Since all sub-expressions are true, the whole expression evaluates to True .

The 'prototype & patch' development process involves creating a basic, working model of a program quickly ('prototype'), then iteratively refining and enhancing it ('patching' phase). This method emphasizes quick iteration and gradual feature incorporation, reducing complexity by focusing on incremental improvements. It facilitates user feedback early in the process and allows developers to address issues incrementally, improving code quality over time . For example, building a simple web scraper with no error handling as a prototype, then iteratively adding features like timeout handling, logging, and data caching during the patching phase, illustrates this concept. It enhances coding by balancing creativity with stability, leading to robust applications adaptable to new requirements .

In Python, the local scope refers to the variables defined within a function, accessible only within that function's context. The global scope involves variables defined outside functions, accessible throughout the entire program. Python resolves the variable names by checking local scopes first, then outer scopes, and finally global scopes if no local match is found . For example, consider a function that modifies a global variable: ```x = 5 def change_var(): global x x = 10 change_var() print(x)``` Initially, 'x' is 5 globally, but 'change_var()' updates 'x' to 10 using the 'global' keyword, reflecting this change outside the function .

Using 'sys.exit()' allows for immediate program termination, which can be useful when ending a program if a critical error occurs or after successfully completing all tasks . It makes the code's intent explicit where and why the program stops, aiding in debugging and readability. However, using 'sys.exit()' carries drawbacks, such as abruptly halting the program without releasing resources properly or performing necessary clean-up tasks. It can bypass exception handling and can stop a program's usual flow control, potentially causing loss of data or incomplete processing of information . Therefore, careful consideration is needed when integrating 'sys.exit()' in larger codebases. Additionally, using exceptions for error handling may offer more controlled termination while providing feedback to the program or user .

Python handles exceptions using try-except blocks, which encapsulate potentially error-generating code, allowing the program to continue execution or handle the error gracefully . The 'try' section contains code that may throw an exception, while 'except' blocks specify how to respond to particular exceptions. Python also provides 'finally' blocks, which execute code regardless of whether an exception was raised, and 'else' blocks, for code that executes only if no exception occurs. This robust structure prevents unexpected termination, allowing informative error handling and clean resource management . For example: ```try: x = 1/0 except ZeroDivisionError: print("Cannot divide by zero")``` This code catches the ZeroDivisionError, preventing a crash and allowing for a user-friendly error message .

The 'logging' module in Python provides a flexible framework for emitting log messages from Python programs, crucial for tracking code execution and identifying issues . It helps maintain code without extensively using 'print()' statements, allowing developers to categorize issues by severity and maintain a timestamped record of program execution . Its logging levels are: 'DEBUG', 'INFO', 'WARNING', 'ERROR', and 'CRITICAL', which help prioritize messages for normal information, debugging purposes, warnings, errors, and serious failures, respectively . Logging facilitates debugging by capturing detailed execution traces and enables developers to monitor applications at various granularity levels, enhancing maintainability by adjusting the level of detail in logs without changing code structure extensively .

The 'zipfile' module provides tools for creating, reading, and extracting ZIP files in Python, allowing efficient file handling and compression . To create a ZIP file, 'ZipFile()' is used along with 'write()', which adds files to the archive: ```import zipfile with zipfile.ZipFile('example.zip', 'w') as zf: zf.write('file.txt', compress_type=zipfile.ZIP_DEFLATED)``` Reading ZIP files uses 'ZipFile()' with 'r' mode: ```with zipfile.ZipFile('example.zip', 'r') as zf: print(zf.namelist())``` Extraction is managed by 'extract()' or 'extractall()', which retrieve files back from the archive: ```with zipfile.ZipFile('example.zip') as zf: zf.extract('file.txt', 'extracted/')``` These functions enhance file manipulation capabilities by enabling efficient storage, transmission, and handling of multiple files as a single archive, improving performance and organization .

You might also like