Python Variables Demystified:
Discover the Key to Efficient and Dynamic  Python Programming

Python Variables Demystified: Discover the Key to Efficient and Dynamic Python Programming

A Comprehensive Guide on Python Variables

In this article, we'll learn about variables, python variables, identifiers, naming conventions and best practices.

In the previous article titled: Understanding Python Syntax: A Comprehensive Guide for Python Programming, we did a comprehensive guide on Python Syntax.

Variable

A Variable is a temporary storage location for a value. Values are stored in a temporary location (memory) during the execution of a program. Several programming languages have their rules for defining variables and referencing them same as Python.

Variable Scope

Variable scope is the extent to which a variable is available for access or modification. The global and local scope are the two categories of variable scope.

Global scope: are variables defined outside of any class or function in a program. They can be modified or accessed from within or outside the functions and classes.

Local scope: Variables within a function or class have local scope. They can only be accessed or changed within the function or class where they're defined.

Python Variable

Python variable is a variable declared using Python syntax whose value can be modified during program execution. Python variables are initialized at the point of definition compared to other languages where variables can be defined and initialized at a later point.

For example, the statement (line of code) below:

name = "Python Variable"
print(name)

In layman's terms, this is like saying hey "name" hold "python variable" I'll need it later. A simple call on name using the print function on the second line will return the value stored.

Assigning Values to Variables in Python

In Python, the equal sign \= is used to assign a value to a variable.

Initialization occurs when a value is assigned to a variable using the = sign.

In Python, there's no data type specification or semi-colon to end the statement (line of code).

Reassigning Values to Python Variables

What would happen if you want to change what "name" is holding is to assign the new value (re-initialization). Python is a dynamically typed programming language.

name = "Python is simple"
print(name)

Printing the variable "name" will output:

Worthy of Note: In Python, values are treated as objects. When a value is typed the Python interpreter assigns an object of the data type and stores the value.

For example,

print(type("Python coding"))
#output <class 'str'>

NB: Since Python is an infer programming language the data type of the value can be detected by the interpreter.

Python interpreter creates a string object and stores "Python coding".

name = "Python coding"
print(name)
print(type(name))

#output <class 'str'>

What happens when we assign that value to a variable in Python is that it becomes a pointer or reference to the string object.

Each of these objects has a unique ID and becomes available once the variable or object is no longer in use or reassigned.

If the variable is assigned to another variable they both have the same identity which signifies they're both pointing to the same object.

name = "Python coding"
my_name = name
print("The id for name:")
print(id(name))
print("and my_name is same:")
print(id(my_name))

my_name = "Python Variable"
print("The id for my_name changes:")
print(id(my_name))

Output:

Python Assignment Chaining

name = "Alemsbaja"
my_name = "Alemsbaja"
current_name =  "Alemsbaja"
print(name, my_name, current_name)

In Python, a single value can be passed to multiple variables in a single statement (one line of code)

name = my_name = current_name =  "Alemsbaja"
print(name, my_name, current_name)

Output:

Variables and their corresponding values can be defined in a single statement in Python.

name, age, location = "Baja EC2", 10, "Germany"

Deleting Python Variables

Variables can be deleted in Python during the execution of the program when it's no longer needed.

del name

Python Variables datatype values

The data type in some programming languages restricts the kind of value that can be stored in it after defining the data type but in Python, the name variable can be assigned to hold a numeric value (integer, float) even if it formerly holds a string object as a value.

name = 30
print(name)

Python variables can hold other data types such as string, list, tuple, range, boolean, and dictionary.

Python Names

Python Names or Identifiers are symbols or characters that can be used to identify a variable. Any identifier can start with an alphabet (A to Z or a to z), an underscore (_) or numbers | digits ( 0 to 9).

_name = "Python Variables"
_2user = "Two Users"

Other symbols such as @, $, &, single usage of ), (, [, ], and % etc.. are not allowed as variable naming or identifiers.

An identifier with "name" is different from "NAME" because Python is a case-sensitive language.

Reserved keywords as shown in the image below cannot be used as variable names in Python.

It's best and most recommendable to stick to any of the following for variable naming conventions with appropriate intent in Python for readability and scalability purposes:

  • Snake Case: my_name, user_name

  • Camel Case: myName, userName

  • Pascal Case: MyName, UserName

A variable with my_name indicates the value of someone's name and not their age or location.

Python Casting

Casting is the process of converting the data type of a value. A Python variable can hold the casted data type of a value.

age = 5
age_to_integer = int(age)   
age_to_string = str(age)  
age_to_float = float(age)

Conclusion

Python Variables are memory spaces defined with a unique reference to hold values temporarily during the execution of a program. The assigning of values without data type definition makes Python pretty simple since it can infer the data type automatically. Reserved keywords cannot be used as variable names. It's best practice to stick to a particular naming style (camel case, snake case, pascal case) for code readability and scalability while ensuring the names can be used to deduce their value. Mastering Python variables is gained by practicing, debugging and reading the documentation consistently.

Watch out for the next article to continue your Python developer journey.

If you're looking forward to developing applications in Python using the OOP (Object-oriented programming), the major concepts are simplified on my blog here.

Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!