☼
⌃
☰

Python Variables

Table of Contents

Python Variables

Description

Variables are fundamental to programming in Python. They are created the moment you first assign a value to them, so there is no need for an explicit declaration command. Python is a dynamically typed language, which means you don't have to declare the type of a variable; the type is inferred from the value assigned to it.

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive (age, Age and AGE are three different variables).
  • Variable names cannot be any of the Python keywords.

Examples

Used to store data values.

Examples

PYTHON
# Declaring and assigning values to variables
x = 5
y = "Hello, World!"

print(x)
print(y)

# Reassigning a variable
x = 10
print(x)

# Multiple assignment
a, b, c = "apple", "banana", "cherry"
print(a)
print(b)
print(c)

# One value to multiple variables
d = e = f = "orange"
print(d)
print(e)
print(f)

# Variable names are case-sensitive
age = 30
Age = 35
print(age)
print(Age)