Creating a Variable0%

Creating a Variable

Beginner8 min readUpdated: 2026-09-03
Study Materials

Creating a Variable

Variables in Python are created at the exact moment a value is bound to them with the '=' operator.


Key Concepts & Detailed Explanation

Python uses dynamic typing. A variable can reference an integer at one moment and be rebound to a string later. Every object has a unique integer identity in memory, discoverable with id().


Code Examples & Output

Python
# Dynamic re-binding
data = 100
print("Type:", type(data), "Memory ID:", id(data))
 
data = "MSK Institute"
print("Type:", type(data), "Memory ID:", id(data))

Expected Output:

Output
Type: <class 'int'> Memory ID: ...
Type: <class 'str'> Memory ID: ...

Best Practices & Common Pitfalls

Although Python allows rebinding a variable to a different type, keeping types consistent improves code readability.


Practice Quiz

1. What function reveals the unique memory address of a Python object?

  • A) address()
  • B) id()
  • C) pointer()
  • D) mem()
Answer: B Explanation: id() returns the object's unique memory identity.

2. Do you have to declare variable types in Python before assigning values?

  • A) Yes
  • B) No, Python is dynamically typed
  • C) Only inside classes
  • D) Only in scripts
Answer: B Explanation: Python infers types dynamically upon assignment.

Practice Challenge

Create three variables of different types, print their values, their types using type(), and their memory IDs using id().

Next Lesson

Output Variables & Printing Techniques

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 1 questions. Each question has one correct answer.

PrevNext