Back to Python Course

Python Course

Variables are used to store data that can be used and modified throughout a Python program. Python is dynamically typed, meaning you don't need to specify the data type of a variable—it’s inferred at runtime based on the assigned value.

Syntax:

variable_name = value

Python uses the assignment operator = to assign values to variables.

Rules for Naming Variables

  • Must start with a letter (A–Z or a–z) or an underscore _.
  • The rest of the name can contain letters, digits (0–9), and underscores.
  • Variable names are case-sensitive (Name and name are different).
  • Avoid using Python keywords (like for, if, class) as variable names.

Good examples: name, _counter, age2, is_valid

Invalid examples: 2name, my-name, class

Let's see some examples,

num = 10 name = "Praveen" price = 99.99 is_valid = True

Use descriptive variable names: Instead of x = 10, prefer age = 10.

Variable names should reflect their purpose.

You can reassign a variable to a different data type:

data = 5 # Initially an integer data = "text" # Now it's a string

This flexibility is due to dynamic typing.