Python Variables

A variable is a name that refers to a value stored in the computer’s memory. It is like a labeled box that stores information you can use later.

You use variables to store values or the results of evaluated expressions, allowing you to reuse them later in the program.

Variables in Python can hold different types of data, such as numbers (int and float), text (str), collections like lists and dictionaries, and more.

Creating Variables

To create a variable, you write the variable name, add an equal sign, and then the value you want to store.

For example:

number = 10

In this example,

  • number is the variable name.
  • 10 is the value assigned to number.

You can imagine this like putting the number 10 in a box labeled "number". Once it is stored there, you can use the value inside the variable to perform calculations.

For example:

result = number * 5
print(result) # Output: 50

We are telling Python to take the value inside number (which is 10) and multiply it by 5, and store the result in the variable result. Finally, we use the print() function to display the value of result. So when you run the code, you will see 50 printed on the screen.

Dynamic Typing

Python is dynamically typed, meaning you don’t need to declare the type of the variable. The type is inferred when a value is assigned.

For example:

x = 10  # x is an integer
y = "hello"  # y is a string

Variable Naming Conventions and Rules

When naming variables in Python, it’s essential to follow specific rules and conventions. Violating these rules can lead to syntax errors, while following the conventions makes the code easier to read and maintain.

Here’s a breakdown of those rules and conventions:

(1) Naming Rules (Syntax Rules)

  • Variable names must start with a letter (a-z, A-Z) or an underscore (_) but not with a number. For example, person1 is a valid variable name, while 1person is not.
  • Variable names can contain only letters (a-z, A-Z), numbers(0-9), and underscores(_). For example, person, person1, person_name
  • Variable names are case-sensitive. For example, age, Age, and AGE are three different variables.
  • Spaces are not allowed in variable names but underscores can be used to separate words. For example, person_name is a valid variable name, whereas person name will result in an error.
  • Reserved words (keywords) cannot be used as variable names. For example, you cannot name a variable class, for, or print, since they are reserved.

(2) Naming Conventions (Best Practices)

  • Use descriptive names that reflect the purpose of the variable. For example, user_age is better than x.
  • Use an underscore to separate words in variable names (snake_case), following the PEP 8 convention. For example, user_age instead of userAge (Camelcase is not preferred in Python).
  • Private variables can be indicated by starting the variable name with a single underscore (_). For example, _user_address
  • Variables meant to be “strongly private” (internal use only) can be prefixed with a double underscore (__). For example, __db_url
  • Constants are typically written in all uppercase letters. For example, PI, MAX_SIZE

Variable Reassignments

You can reassign variables in Python freely, even changing the type of the value.

For example:

x = 10  # x is an integer
x = "ten"  # x is now a string

Multiple Variable Assignments

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

For example:

a, b, c = 1, 2, 3

You can also assign the same value to multiple variables in a single line.

For example:

a = b = c = 5

Type Checking of a Variable

You can get the data type of a variable using the type() function.

For example:

x = 10
y = "James"

print(type(x))
print(type(y))

Output:

<class 'int'>
<class 'str'>

Constants in Python

In Python, constants are variables whose values are intended to remain unchanged throughout the execution of the program. Unlike some other languages, Python doesn’t have a built-in constant type. However, by convention, constants are typically written in all uppercase letters with underscores separating words.

For example:

PI = 3.14159
GRAVITY = 9.81
MAX_CONNECTIONS = 100

Python doesn’t enforce immutability, meaning you can technically change the value of a constant, but by convention, you should avoid doing so.