Home » Python Variables and Constants

Python Variables and Constants

Python Variables and Constants

Python Variables

A variable represents a named storage location in memory used to hold data. Python Variable act as containers that store data which can be altered during program execution. It is advisable to use mnemonic variables, which are easily remembered and associated. Essentially, a variable references a memory address where data resides. For instance,

num = 59
Python

Here, ‘num’ is a variable storing the value 59.

You can think of variables as bags capable of storing items, which can be replaced at any time.

num = 90
num = 9.1 #updating the num vaulue
Python

Initially, ‘num’ holds 90, which is later changed to 9.1.

Note: In Python, values are not assigned to variables; rather,Python gives the reference of the object(value) to the variable.

Python Variable Naming Rules

  • Variables must begin with a letter (A-z) or underscore (_).
  • They cannot commence with a digit (0-9).
  • Variable names can contain only alphanumeric characters and underscores (A-z, 0-9, and _).
  • – Variable names are case-sensitive (e.g., ‘firstname’, ‘Firstname’, ‘FirstName’, and ‘FIRSTNAME’ are distinct variables). Lowercase letters are recommended for variable names.

Valid variable names include:

firstname
lastname
age
country
city
first_name
last_name
capital_city
_if          # use underscore to utilize reserved words as variable
Python

Invalid variable names:

first-name
first@name
first$name
num-1
1num
Python

Most Python developers use snake case convention for variable naming, using underscores to separate words in variable names (e.g., first_name, last_name, engine_rotation_speed).

When we assign a certain data type to a variable, it is called variable declaration. For instance in the example below my first name is assigned to a variable first_name. The equal sign is an assignment operator. Assigning means storing data in the variable. The equal sign in Python is not equality as in Mathematics.

Declaring and assigning values to variables

num = 90
num = 9.1
name = "geekster"  # 'name' is the variable, 'geekster' is the value

print(website)       
Python

Output:

geekster
Python

Python is a type-inferred language, automatically determining variable types. For instance, it recognizes ‘geekster’ as a string and declares the ‘name’ variable accordingly.

Declaring Multiple Variables in One Line

a, b, c = 5, 10.5, "geekster"
print(a)
print(b)
print(c)
Python

Output:

5
10.5
geekster
Python

Declaring multiple variables:

a = 1; b = 2; c = 3
print(a,b,c) 
Python

Output:

 Output: 1 2 3
Python

Changing Variable Values

website = "github.com"
print(website)

website = "geekster.com"
print(website)
Python

Output:

github.com
geekster.com
Python

Python Constants

A constant represents a special kind of variable that holds a value which remains unchanged throughout the program. It’s like having a container that securely holds information, much like a bag holding books that cannot be swapped out once placed inside.

Declaring and assigning values to constants
Create constant.py:

PI = 3.14
GRAVITY = 9.
Python

Create main.py:

import constant
print(constant.PI)
print(constant.GRAVITY)
Python

Output:

3.14
9.8
Python

Note: Although Python supports constants, they are not extensively used. Naming them in all caps is a convention to distinguish them from variables, but it doesn’t prevent reassignment.

Python Variable and Constant Naming Conventions and Rules

  • Python Variables and constants should use a combination of lowercase (a to z) or uppercase (A to Z) letters, digits (0 to 9), or underscores (_). For example:
    • MACRO_CASE
    • snake_case
    • camelCase
    • CapWords
  • Create descriptive names; for instance, ‘vowel’ is preferable to ‘v’.
  • Variables spanning multiple words should employ underscores. For example:
    • my_name
    • current_salary
  • Constants should be in all caps when possible, such as:
    • PI
    • G
    • MASS
    • SPEED_OF_LIGHT
    • TEMP
  • Avoid special symbols like !, @, #, $ %, etc.
  • Variables should not begin with a digit.

Important: Python 3 supports full Unicode, permitting Unicode characters in variable names as well.

Examples of valid variable names:

name = "Bob"
Age = 54
has_W2 = True
Python

However, variables can’t begin with a digit:

1099_filed = False    # Invalid variable name starting with a number
Python

Variable case sensitivity and underscores are significant, with each combination defining a different variable:

age = 1
Age = 2
aGe = 3
AGE = 4
a_g_e = 5
_age = 6
age_ = 7
AGe = 8
print(age, Age, aGe, AGE, a_g_e, age, age, AGe)  # Output: 1 2 3 4 5 6 7 8
Python

While it’s possible to create variables like ‘age’ and ‘Age’ within the same program, it’s generally discouraged due to potential confusion among readers and programmers.

Conclusion

Understanding variables and constants in Python is fundamental to effective programming. Variables are dynamic storage locations that can hold different data types and whose values can be updated throughout a program’s execution. Constants, on the other hand, are used to store values that are not intended to change, serving as fixed references within the code. Adhering to Python’s naming conventions for variables and constants not only aids in writing clean and readable code but also helps in avoiding errors related to case sensitivity and name conflicts. By following these guidelines, programmers can ensure their code is efficient, maintainable, and understandable, facilitating easier debugging and collaboration.