Miscellaneous Topics
1. What is an Object in Python?
In general, anything that can be assigned to a variable in Python is referred to as an object.
Strings, Integers, Floats, Lists, Functions, Modules etc... are all objects.
Identity of an Object
Whenever an object is created in Python, it will be given a unique identifier (id). This unique id can be different each time you run the program.
Every object that you use in a Python program will be stored in Computer Memory
The unique id will be related to the location where the object is stored in the Computer Memory.
2. What are Modules in Python?
In the Python context, any file containing a Python code is called a Module.
Some examples of modules are collections, random, datetime, math, etc...
3. What are Packages in Python?
In the Python context, any file containing a Python code is called a Module. A Package is a collection of modules.
4. What are Namespaces?
A namespace is a collection of currently defined names along with information about the object that the name references.
It ensures that names are unique and won’t lead to any conflict.
Namespaces allow us to have the same name referring different things in different namespaces.
Code
Output
5. What is Scope?
The scope of a variable is the region in which that variable can be accessed.
6. What are Global and Local variables?
Global Variables:
If a variable is declared outside of all functions and conditional statements then that variable is known as Global variable.
These can be accessed anywhere in the program. If the value of the global variable is modified inside a function or conditional statement then the changes are reflected in the rest of the program.
Local Variables:
If a variable is declared inside of a function or conditional statement then that variable is known as Local variable.
These can be accessed inside a function or conditional statement in which they are declared. If the value of the local variable is modified in one function, then the changes are not reflected in another function.
7. What are Exceptions in Python?
Even when a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called Exceptions.
8. How to Handle Exceptions in Python?
Python provides a way to catch the exceptions that were raised so that they can be properly handled.
- Exceptions can be handled with try-except block.
- Whenever an exception occurs at some line in the try block, the execution stops at that line and jumps to except block.
Code
Output
Handling Specific Exceptions
We can specifically mention the name of the exception to catch all exceptions of that specific type.
Syntax
Example
Code
Output
Handling Multiple Exceptions
We can write multiple exception blocks to handle different types of exceptions differently.
Syntax
Example_1
Code
Input
Output
Example_2