Python Exam for CS Students
Python Exam for CS Students
Namespaces are containers for mapping names to objects, ensuring variable and function names do not collide. Scope determines the visibility of namespaces, with local, enclosing, global, and built-in scopes being the hierarchy. Variables are accessed according to this order, impacting how modules manage internal and external variable names .
Dictionaries store data in key-value pairs and allow for fast lookup based on a key, while lists store data in a sequential order where each element is accessed by its position index. Lists are useful for ordered collections and iteration, whereas dictionaries are optimized for retrieving data when the exact key is known .
Tuples are immutable, meaning their elements cannot be deleted or changed once assigned, unlike lists, which are mutable and allow for operations like insertion and deletion of elements. This immutability makes tuples generally more appropriate for fixed collections of items .
Lists can be updated through methods like 'append()', 'extend()', 'insert()', and using index assignments. Nested lists contain other lists, and operations may require iterative approaches or nested loops to access elements. The complexity of nested lists adds a layer of abstraction influencing operations and updates .
Python primarily deals with text and binary files. The 'open' function in Python can be used to open a file in different modes such as 'read', 'write', and 'append'. It returns a file object which can be used to interact with the file using various methods like 'read()' and 'write()' .
These functions perform basic operations: 'max()' returns the largest item, 'min()' the smallest, 'len()' returns the number of items, 'pow()' raises a number to a power, and 'sum()' returns the sum of all items in an iterable. Example: 'max([1, 2, 3])' returns 3, 'min([1, 2, 3])' returns 1, 'len([1, 2, 3])' returns 3, 'pow(2, 3)' returns 8, 'sum([1, 2, 3])' returns 6 .
'pip' stands for Preferred Installer Program, which is the package installer for Python. It allows users to install and manage additional libraries and dependencies that are not part of the standard Python library, facilitating easy distribution and installation of Python software .
Python uses 'call by object reference', which means that values (objects) are passed by reference. Mutable objects like lists can be changed inside a function, impacting the original object, whereas immutable objects like integers cannot be altered. This behavior resembles 'call by reference' for mutable objects and 'call by value' for immutable objects .
Jump statements like 'break', 'continue', and 'pass' control the flow of loops by altering or skipping iteration sequences. 'break' ends the loop, 'continue' skips to the next iteration, and 'pass' acts as a placeholder for future code without influencing control flow .
'For loops' iterate over items of a collection, 'while loops' continue until a condition is no longer true. 'If-else' and 'if-elif-else' structures execute blocks of code based on conditions, where 'if-else' provides a binary choice, and 'if-elif-else' allows for multiple conditions to be checked sequentially .