Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program to Swap Two Variables
Next article icon

Variables under the hood in Python

Last Updated : 01 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

In simple terms, variables are names attached to particular objects in Python. To create a variable, you just need to assign a value and then start using it. The assignment is done with a single equals sign (=):

Python3
# Variable named age age = 20 print(age)  # Variable named id_number id_no = 4067613 print(id_no) 

Output:

20  4067613  

From the above program it can be clarified that  "age is assigned a value of 20" and "id no is assigned a value of 4067613". Now age and id_no can be used in a statement or expression, and its value will be substituted.

In python variables are dynamic i.e The type of variable is known at the run time and also a variable may be assigned a value of one type and then later re-assigned a value of a different type.

What is actually happening when you make a variable assignment in Python? 

Python is an object-oriented language. Any data item in a Python program is an object of a certain type or class like integer, float, string, etc.

From the above variable example when we are assigning value 20 to variable age the interpreter does the following:

  1. Creates an integer object
  2. Gives it the value 20
  3. Displays it to the console
Python3
# type() built in function  # say the type of variable age = 20 print(type(age)) 

Output:

<class 'int'>  

Actually python variable is a symbolic name that is a reference or pointer to an object. When an object is assigned to a variable, the reference of the object can be used by its name, the data is also stored within the object itself.

Multiple object reference:

It refers to referencing an object to a previously assigned object.

Let's have a look at this code below:

Python
# Multi object reference age=20 new_age=age print(age) print(new_age) 

Output:

20  20  

Here, Python does not create another object. It simply creates a new symbolic name or reference, new_age which points to the same object that was previously assigned.

If we change the value of new_age for example new_age=30, then Python creates a new integer object with the value 30, and new_age becomes a reference to it.

Garbage collection:

If we change the value of the above variable age=25, then new integer object with value 25 is created and age is referenced to it. Now the integer object that has 20 in it has been orphaned or lost the reference. There is no way to access it. If there is at-least a single reference to the object then it can be alive. When the number of references to an object drops to zero python will eventually notice that it is inaccessible and reclaim the allocated memory so it can be used for something else. This is how garbage collection is done in Python.

Note: For more information, refer Garbage Collection in Python


Next Article
Python Program to Swap Two Variables
author
koushikd1590
Improve
Article Tags :
  • Python
  • Python Programs
  • python-basics
Practice Tags :
  • python

Similar Reads

  • Variable Operations Dictionary Update - Python
    The task of performing variable operations in a dictionary in Python involves updating the values associated with specific keys based on certain operations. For example, consider a scenario where x = 6 and y = 7. The goal could be to store the sum of x and y under the key 'Gfg' and the product of x
    3 min read
  • Using Python Environment Variables with Python Dotenv
    Python dotenv is a powerful tool that makes it easy to handle environment variables in Python applications from start to finish. It lets you easily load configuration settings from a special file (usually named .env) instead of hardcoding them. This makes your code more secure, easier to manage, and
    3 min read
  • Insert a Variable into a String - Python
    The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectiv
    2 min read
  • Python Program to Swap Two Variables
    The task of swapping two variables in Python involves exchanging their values without losing any data . For example, if x = 10 and y = 50, after swapping, x becomes 50 and y becomes 10. Using Tuple UnpackingTuple unpacking is the most efficient method as it eliminates the need for extra memory or te
    3 min read
  • Python | Accessing variable value from code scope
    Sometimes, we just need to access a variable other than the usual way of accessing by it's name. There are many method by which a variable can be accessed from the code scope. These are by default dictionaries that are created and which keep the variable values as dictionary key-value pair. Let's ta
    3 min read
  • Python | Using variable outside and inside the class and method
    In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let's see, how to use and access these variables throughout the program. Variable defined outside the class: The variables that are defined outside the class can be accessed by any class or any me
    3 min read
  • Write Multiple Variables to a File using Python
    Storing multiple variables in a file is a common task in programming, especially when dealing with data persistence or configuration settings. In this article, we will explore three different approaches to efficiently writing multiple variables in a file using Python. Below are the possible approach
    2 min read
  • Shared Reference in Python
    Let, we assign a variable x to value 5, and another variable y to the variable x. x = 5 y = x When Python looks at the first statement, what it does is that, first, it creates an object to represent the value 5. Then, it creates the variable x if it doesn't exist and made it a reference to this new
    4 min read
  • Declare variable without value - Python
    A variable is a container that stores a special data type. Unlike C/C++ or Java, a Python variable is assigned a value as soon as it is created, which means there is no need to declare the variable first and then later assign a value to it. This article will explore How to Declare a variable without
    2 min read
  • Tuple within a Tuple in Python
    In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python. Using Tuple within a Tup
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences