Post

A Quick Note on Python Variables

In Python, a variable is a named location used to store data in memory. Variables are fundamental to programming, allowing you to label and manipulate data in your code.

Creating Variables

Creating a variable in Python is as simple as assigning a value to a name:

1
2
3
4
5
6
7
8
# A variable storing a number
x = 10

# A variable storing a string
name = "Shiv"

# A variable storing a boolean
is_active = True

Python is dynamically typed, so you don’t need to declare the type of the variable. The type is inferred from the value you assign.

Naming Rules

When naming variables, there are a few rules to follow:

  • 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 alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive (age, Age, and AGE are three different variables).

Using Variables

Once you’ve created a variable, you can use it in your code:

1
2
print(x)        # Output: 10
print(name)     # Output: Shiv

Variables make your code more readable and easier to maintain. They are a core concept you’ll use in every Python program you write.

This post is licensed under CC BY 4.0 by the author.