The document explains various Python programming concepts, including the differences between mutable and immutable data types, local and global variables, and how to use the sys.exit() function. It also covers data structures like lists, tuples, and dictionaries, as well as string manipulation methods like split() and join(). Additionally, it discusses modules for file operations, assertions, logging, and object-oriented programming concepts such as classes, objects, and polymorphism.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
python viva
The document explains various Python programming concepts, including the differences between mutable and immutable data types, local and global variables, and how to use the sys.exit() function. It also covers data structures like lists, tuples, and dictionaries, as well as string manipulation methods like split() and join(). Additionally, it discusses modules for file operations, assertions, logging, and object-oriented programming concepts such as classes, objects, and polymorphism.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
1.what is the difference between mutable and immutable data types? Give example.
Mutable data types are those whose values can be changed ,
Whereas immutable data types are ones in which the values can’t be changed. Example: Lists: can be changed by adding or removing elements Tuples: cannot be changed by adding ,removing ,or replacing elements. 2.Describe the difference between local and global variables in python. Local variables: it is defined within the function or block of code and it is available with in the function. Global variables: it is defined outside the function or block of code and it is available any where in the function. 3.Explain how the sys .exit() function works and when it should be used. How it works : The sys . exit() function is part of the sys module. you need to import the sys module. You can pass an optional exit code argument to the function. When to use it : Use sys .exit() when you want to terminate a program or function. Use sys .exit() when you want to signal the reason for termination to other programs. 4.what are the difference between lists, tuple, and dictionaries? Lists : a list is a data structure in python that is a mutable, or changeable, ordered sequence of elements with in the square brackets. Tuple : a tuple in python is a data structure that stores a collection of items that cannot be changed(immutable). Dictionaries : a dictionary in python is a data structure that stores items as key-value pairs. 5.Explain the concept of list slicing. Provide an example. List slicing in python is a way to extract a part of a list by specifying a range of indices. List slicing is used to access, modify and manipulate elements based on their indices. It is used to create a sublist from another list. It is used to pick a specific element from the list. Example: Number = [0,1,2,3,4,5,6,7,8,9] Subset = number[2:5] Print(subset) Output : [2,3,4] 6.what is the difference between = and == operator? = operator : it is a assignment operator and it is used to assign value to a variable. == operator : it is a equality operator and is used to compare two values to check if they are equal. 7.Explain how dictionaries are used to store key-value pair. Keys : kays are unique identifiers for items in a dictionary . they can be of any data type, such as an integer, float, string, or tuple. Values : values are the data associated with each key. They can be of any data type and can be duplicated. 8.what is pretty printing in python? Pretty print creates a representation of source code that can be easily analyzed as well as easily read by human. 9.How do the strip(), lstrip(), and rstrip() methods work? Strip() : it is used to remove the whitespace from the beginning and at the end of string. lstrip() : it is used to remove the leading whitespace(on the left) in the string. rstrip() : it is used to remove the trailing whitespace(on the right). 10.Explain the split() and join() methods. Provide an example for each. Split() : this method is used to breaks a string into a list of substrings. Example: string = “I am good” Print(string.split()) Output : [ ‘I’, ‘am’, ‘good’,] Join() : this method is used to joins a list of substrings into string. Example: list = [‘I’, ‘am’, ‘good’] Print(list.join()) Output : [“I am good”] 11.waht are escape character in string? Name a few commonly used escape sequence. Escape character is a backslash\ followed by the character you want to insert. Commonly used escape sequences are: Newline : \n Horizontal tab : \t Vertical tab : \v Single quote : \’ Double quote: \” Backslash: \ 12.what is the purpose of the shelve module? The shelve module creates a persistent, file based version of an object very similar to a python dictionary. 13.Explain the purpose of the shutil and zipfile modules. Shutil module : it is used in python performs high level file operations, such as copying, moving, renaming ,and deleting files. Zipfile module : it is used in python works with ZIP aechives, which are a common file format for compressing and storing files. 14.what are assertions, and how are they different from exceptions? Provide an example. Assertions are statements that check if a program is running correctly, while exceptions are objects that represent errors. Assertions are generally disabled at run time, while exception are used to handle errors that occurs during program execution. 15.describe how the logging module can help in debugging python programs. It is provides tracking for events that occurs while software runs, and can output these events to a separate log file to allow you to keep track of what occurs while your code runs. 16.Explain the process of creating a ZIP file backup of a floder. What methods are used? To create a zip file backup of a folder, you can use right click menu on a windows computer. Navigate to the folder you want to zip Highlight the files you want to zip Right click on the highlighted files Select send to Select compressed folder The zipped folder will appear in the same location as the original folder ZIP method are used. 17.what is the difference between a class and an object? Provide an example. A class is a blueprint for declaring and creating objects. An object is a class instance that allows programmers to use variables and methods from inside the class. Example : class point: ‘’’Represents a point in 2D- space.’’’ 18.explain the purpose of the __init__method in a class. The __ init __ method in python is a special method used to initialize of an object when it is created. It is automatically called when an object of a class is instantiated. 19.what is operator overloading? Operator overloading is a programming technique that allows the same operator symbol to be used for multiple operations. 20.what is polymorphism? Polymorphism in python is the ability to use the same function name for different types of data.