Variables & Objects
Introduction
1. Explain python variables.
Python variables are used to store and manage data. They are the containers to store information.
2. Define local variable.
Local variables are inside a function, can only be used inside that function.
3. Define global variable.
Global variables are declared outside any function, and they can be accessed on any function in the program.
4. What are the local variable naming conventions?
- Start with alphanumeric characters.
- Cannot start with a number.
- No white space character.
- No special signs (
%,$,#,@)
5. Define environment variable.
Environment variables are variables you store outside of your program that can affect how it runs.
6. What are the environmental variable’s common use cases?
- To store configuration settings outside of your code.
- To store sensitive information, like secrete keys or passwords.
Object Memory Address
1. Explain memory addresses in the context of python’s object or variable.
Memory addresses refer to the locations in the computer’s memory where the data is stored. Every object in Python is stored in a specific memory location.
2. Explain the function id(x).
To obtain the memory address of an object (
x).
3. Explain Memory References in the context of variables.
A memory reference is a value that represents the location of an object in memory. In python, variables typically store references to objects rather than the objects themselves. The memory reference is essentially a way of pointing to the memory address of an object.
a = [1, 2, 3]
b = a # b now holds a reference to the same list as a
4. Explain Reference Counting. Provide an example to illustrate.
Python memory manager performs reference counting to all objects stored in memory. For example, when there are two references (variables) referring to the same object, the reference count for that object is 2.
5. Explain the term shared reference.
Shared reference indicates that the two or more variables are referencing to the same object in memory.
6. Explain the purpose of the Garbage Collector.
It periodically cleans up the memory on a periodic basis. Do note that it can be called manually.
7. Python is dynamically typed. Elaborate the sentence.
Python is dynamically typed. Therefore, when assigning object to variables, the variable is purely a referenced to the object, it does not attach the datatype.
8. Boolean values (True or False) are singleton objects, explain what does this means in terms of the object’s memory address?
The concept of singleton objects refers to instances of a class for which there is only one instance in the entire program. This means that there is only one instance of
True and False object in the entire program and all variable that evaluates True or False will reference to the same memory address.
Object Mutability
1. Explain the concept of mutability in python.
Mutability refers to whether an object’s state can be modified after it is created.
2. Describe mutable objects and provide examples.
- Definition: Mutable objects are objects whose state or value can be modified after they are created.
- Examples: Lists, dictionaries and sets.
3. Describe immutable objects and provide examples.
- Definition: Immutable objects are objects whose state or value cannot be modified after they are created.
- Examples: Integers, floats and strings.
4. What happens to the variable(s) referencing to the mutable object when we modify it?
When a mutable object is modified, the object is altered in-place. This means any variable referencing to the object will see the changes, as they all refer to the same object in memory.
5. What happens to a mutable object’s memory address when we modify the object in place?
The object’s memory address will remain the same despite having the internal state(data) change.
6. What happens when we reassign a variable to a new a mutable object?
When you reassign a variable to a new mutable object, the variable now references the new object, and any changes made to the object will be reflected through that variable.
7. What happens when there isn’t any more reference pointing to the mutable object?
When there are no references pointing to the object. Python will discard the object automatically.
8. Tuples are immutable. How about the elements inside the tuples?
Tuples are immutable but the elements in the tuple can be mutable objects. This means that we are able to modify the mutable elements and the changes will be reflected in the tuple.
9. Provide the syntax to compare variable equality between var1 and var2 in terms of their memory address and the object state(data).
- Memory Address:
var1 is var2 - Object State:
var1 == var2
10. What does it mean to “unpack” the variables?
Unpacking variables in Python refers to the process of extracting values from a container (like a tuple, list, or dictionary) and assigning them to separate variables.
11. Given a list [1,2,3,4,5], unpack the elements and print it .
list = [1,2,3,4,5]
print(*list)
#Output -> 1 2 3 4 5
12. Given a dictionary {"x":5, "y":5}, unpack the dictionary variables into the function add(x,y)
dic = {"x":5, "y":5}
def add(x,y):
return x + y
add(**dic)
#Output -> 10