Variables serve as a foundational concept in programming. In this guide, we delve deep into the nature and usage of Python variables, showing the distinction between constant numbers or literals.

>>> print(3.3)
3.3

The code above shows a direct usage of a number. However, when we build more complex applications, we inevitably rely on variables.

Master Python: Complete Python Programming Course & Exercises

How to Assign Variables in Python

Imagine a variable as a labeled box where you store a value. The label is the variable name and the content is the value. The assignment process uses the equals sign (=).

x = 3

This can be interpreted as ‘x holds the value 3’. After this assignment, you can refer to x throughout your program.

print(x)

Using the interactive Python shell (REPL), you can also retrieve its value directly.

>>> x
3

Python allows you to update the value of x or any variable as your program progresses.

x = 4

When we retrieve x again, we observe the updated value.

print(x)
4

![Understanding Python Variables](../wp-content/uploads/2020/02/define variable.png)

Deep Dive into Python: Complete Python Programming Course & Exercises

Exploring Variable Types in Python

A remarkable feature of Python is its dynamic typing system. Unlike some languages that mandate prior type declarations (statically typed), Python infers the type of a variable at runtime.

>>> y = "hello"
>>> print(y)
'hello'
>>> y = 2
>>> print(y)
2

Notice how the type of y transitions? You can inspect a variable’s type with the type() function.

>>> type(y)
<class 'int'>
>>> y = "hello"
>>> type(y)
<class 'str'>

Here, y changes from a str (string) to an int (integer).

  • str: Represents textual content, which can range from a single character to entire paragraphs.
  • int: Denotes whole numbers, e.g., 1, 2, 3.
    Python’s dynamic typing means the language determines the most appropriate type.
    >>> x = 3
    >>> price = 5.50
    >>> word = "hello"
    In the above code, the variables x, price, and word are assigned different types.
    >>> type(x)
    <class 'int'>
    >>> type(price)
    <class 'float'>
    >>> type(word)
    <class 'str'>
    Here, float represents numbers with decimal points.

Advance Your Python Skills: Complete Python Programming Course & Exercises

Guidelines for Naming Variables

While we’ve seen brief variable names like x and y, Python supports long, descriptive names comprising uppercase and lowercase letters, underscores, and numbers. But remember, they can’t include spaces or special characters.

>>> name = "Alice"
>>> age = 21
>>> livesInWashington = True

However, initiating a variable name with a number is invalid.
In Python, variable names are case-sensitive. Thus, name, Name, and NAME are distinct.
Explicit naming, which makes the code self-explanatory, is preferable. For instance, using camel casing:

>>> NameOfFriend = "Bob"
>>> NameOfDad = "Warren"

Naming conventions include:

  • Camel Case: MyVariableName
  • Snake Case: my_variable_name
  • Pascal Case: MyVariableName
    The style chosen often depends on personal or organizational preference. Notably, camel case is widely adopted in languages like Java and C++.

Words Reserved by Python

Certain words are reserved by Python, meaning they cannot be used as variable names. To view these, use help("keywords").

>>> help("keywords")

Strings in Python

Strings, which hold textual content, can be defined in three different ways:

word = 'Hello world'
word = "Hello world"
word = '''Hello world'''

Though all three declarations are valid, double quotes are most commonly used.

Mathematical Operators in Python

Once variables are assigned, they can be utilized and manipulated across the program. Python supports standard arithmetic operators.

x = 2
x = x + 1 # Update x by adding one.
x = 5 # Replace x with a new value.

Utilizing Formatted Strings in Python

Variables can be displayed using the print() function. To embed variables within textual content, we use formatted strings.

x = 5
y = 3 * x
print(f"x = {x}")
print(f"y = {y}")

Formatted strings provide a neat way to interleave variables and text for clearer output.

Pursue Mastery in Python: If you are a Python beginner, then I highly recommend this book.

Next Steps in Python Journey: Download exercises