GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python Variables - GeeksforGeeks Python VariablesLast Updated : 3 Nov, 2026 Variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value.
Output 5 Alex Rules for Naming VariablesTo use variables correctly, the following naming rules should be followed:
Below listed variable names are valid:
Below listed variables names are invalid:
Assigning Values to Variables1. Basic Assignment: Variables are assigned values using the = operator.
2. Dynamic Typing: Python is dynamically typed, so the same variable can store different data types during execution.
3. Assigning Same Value: Same value can be assigned to multiple variables in a single line.
Output 100 100 100 4. Assigning Different Values: Multiple variables can also be assigned different values in a single line.
Output 1 2.5 Python Concept of Object ReferenceLet us assign a variable x to value 5.
When x = 5 is executed, Python creates an object to represent the value 5 and makes x reference this object. ![]() Now, let's assign another variable y to the variable x.
This statement creates y and references the same object as x, not x itself. This is called a Shared Reference, where multiple variables reference the same object. ![]() Now, if we write
Python creates a new object for the value "Geeks" and makes x reference this new object. ![]() The variable y remains unchanged, still referencing the original object 5. Now, If we assign a new value to y:
![]() Python variables store references to objects, not the actual values themselves. When a variable is reassigned, it starts referencing a new object while the old unreferenced object becomes eligible for garbage collection. The scope of the variable determines where it can be accessed in Python program, such as within a function or throughout the program.
Understanding Variable ReassignmentIn this example, we check whether modifying one variable affects another when both initially reference the same object.
Output 1 2 Explanation:
Deleting a Variabledel keyword is used to delete a variable from memory. After deletion, the variable can no longer be accessed.
Output
Explanation: del x deletes the variable x. Accessing x after deletion raises a NameError because the variable no longer exists. Practical Examples1. Swapping Two Variables: Using multiple assignments, we can swap the values of two variables without needing a temporary variable.
Output 10 5 2. Counting Characters in a String: Assign the results of multiple operations on a string to variables in one line.
Output Length of the word: 6 Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |