GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python Variable

  • Home
  • ▼Introduction to Python
  • Python Home
  • Python 2 vs 3
  • Python Installation
  • Python IDLE
  • ▼Python Basic
  • Python Syntax
  • Python Print Statement
  • Python Variable
  • Python Data Type
  • Python Operators
  • Python If elif else
  • Python For Loop
  • Python While Loop
  • Python break, continue
  • ▼Data Handling and Structures
  • Python String
  • Python Lists
  • Python Dictionary
  • Python Tuples
  • Python Sets
  • Python Bytes, Bytearray
  • Python Advanced Data Structure
  • ▼Date and Time
  • Python Date and Time
  • Python Calendar Module
  • ▼Functions and Modules
  • Python User define function
  • Python Built-in Functions
  • Python Module
  • Python Functions Examples
  • ▼File Handling and I/O
  • Python File Handling
  • ▼Object-Oriented Programming
  • Python Classes
  • ▼Advanced Programming Constructs
  • Python Iterators
  • Python Generators
  • Python Functional Programming
  • Python Multithearding
  • ▼Regular Expressions
  • Python Regular Expressions
  • ▼Error Handling and Debugging
  • Python Exception Handling
  • Python Testing and Debugging
  • ▼Database Interaction
  • Python SQlite Database
  • Python SQLalchemy ORM
  • ▼Advanced Python Topics
  • Advanced Python Topics
  • Python Standard Library
  • ▼Web Programming
  • CGI Programming
  • ▼Python Miscellaneous
  • Python-docx: Create and Manipulate
  • Software Frameworks
  • Floor Division in Python
  • Thonny Python IDE
  • pip install mysql-connector-python
  • Astral Python in 2026
  • PyCharm 2026 The Ultimate Guide
  • Python for Data Analysis in 2026
  • Self-Driving Python Server
  • Python-Native Hypermedia UI
  • Building Dashboards with Pythons
  • Building an Educational HTTP Server
  • Python-First Rapid App Generator
  • Markdown-Native Personal Site Framework
  • pyRAP Framework
  • Hypermedia-Driven Python Framework
  • Shinylives Reactive Framework
  • ASGIMachine: HTTP Decision Graph Framework
  • Embedded Web Framework for MicroPython
  • ..More to come..

Python Variable

Last update on April 13 2026 12:53:05 (UTC/GMT +8 hours)

Variable and Value

  • A variable is a named memory location where data can be stored. For example: roll_no, amount, name.
  • A value is the data stored in a variable, which can be a string, numeric value, etc. For example: "Sara", 120, 25.36.

Key Points About Python Variables:

  • Variables are created when they are first assigned a value.
  • They must be assigned before being referenced.
  • The value stored in a variable can be accessed or updated later.
  • No declaration is required before using a variable.
  • The type of the variable (e.g., string, int, float) is determined automatically by Python based on the value assigned.
  • Python manages memory allocation based on the data type of the variable.

Python Variable

Python Variable Name Rules

  • Must begin with a letter (a-z, A-Z) or an underscore (_).
  • Subsequent characters can be letters, numbers, or underscores.
  • Case-sensitive: age, Age, and AGE are considered different variables.
  • Can be of any reasonable length.
  • Reserved words (like if, for, while, etc.) cannot be used as variable names.

Good Variable Naming Practices

  • Choose meaningful names, such as roll_no, which is clearer than rn.
  • Avoid unnecessarily long variable names (e.g., Roll_no_of_a_student is too long).
  • Be consistent in your naming convention: roll_no or RollNo, but not both.
  • Start variable names with an underscore (_) when you need a special case or private variables.

Python Assignment Statements

Assignment statements create variables and assign values to them. The basic syntax for an assignment is:

Syntax:

<variable> = <expr>

Where the equal sign (=) is used to assign value (right side) to a variable name (left side). See the following statements :

>>> Item_name = "Computer" #A String
>>> Item_qty = 10 #An Integer
>>> Item_value = 1000.23 #A floating point
>>> print(Item_name)
Computer
>>> print(Item_qty)
10
>>> print(Item_value)
1000.23
>>> 

Note: Assignment reads right to left. This is why a = 12 works, but 12 = a results in a syntax error.

Example:

a = 12 is correct, but 12 = a does not make sense to Python, which creates a syntax error. Check it in Python Shell.

>>> a = 12
>>> 12 = a
SyntaxError: can't assign to literal
>>> 

Multiple Assignment

The basic assignment statement works for a single variable and a single expression. You can also assign a single value to more than one variables simultaneously.

Syntax:

var1=var2=var3...varn= = <expr>

Example:

x = y = z = 1 

Now check the individual value in Python Shell.

>>> x = y = z = 1
>>> print(x)
1
>>> print(y)
1
>>> print(z)
1
>>> 

Alternatively, you can assign multiple values to multiple variables in a single line.

Syntax:

<var>, <var>, ..., <var> = <expr>, <expr>, ..., <expr>

Example:

x, y, z = 1, 2, "abcd"

In the above example x, y and z simultaneously get the new values 1, 2 and "abcd".

>>> x,y,z = 1,2,"abcd"
>>> print(x)
1
>>> print(y)
2
>>> print(z)
abcd 

You can reuse variable names by simply assigning a new value to them :

>>> x = 100
>>> print(x)
100
>>> x = "Python"
>>> print(x)
Python
>>>  

Other ways to define value

Python allows formatting of large numbers and decimal values for better readability.

>>> five_millions = 5_000_000
>>> five_millions

Output:

5000000
>>> small_int = .35
>>> small_int

Output:

0.35
>>> c_thousand = 10e3
>>> c_thousand

Output:

10000.0

Swap variables

In Python, you can swap the values of two variables in a single line.

Syntax:

var1, var2 = var2, var1

Example:

>>> x = 10
>>> y = 20
>>> print(x)
10
>>> print(y)
20
>>> x, y = y, x
>>> print(x)
20
>>> print(y)
10
>>>

Local and global variables in Python

  • Global variables are accessible throughout the entire program, even within functions.
  • Local variables are defined within a function and cannot be accessed outside it.
  • A variable is assumed to be local unless explicitly declared as global using the global keyword.

Example:


var1 = "Python"
def func1():
    var1 = "PHP"
    print("In side func1() var1 = ",var1)

def func2():
    print("In side func2() var1 = ",var1)
func1()
func2()

Output:

In side func1() var1 =  PHP
In side func2() var1 =  Python

You can use a global variable in other functions by declaring it as global keyword :

Example:


def func1():
    global var1
    var1 = "PHP"
    print("In side func1() var1 = ",var1)

def func2():
    print("In side func2() var1 = ",var1)
func1()
func2()

Output:

In side func1() var1 =  PHP
In side func2() var1 =  PHP

PREV : Python print statement
NEXT : Python Data Type

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — 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.