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.
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
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.
Using the interactive Python shell (REPL), you can also retrieve its value directly.
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.
![Understanding Python Variables](../wp-content/uploads/2020/02/define variable.png)
Deep Dive into Python: Complete Python Programming Course & Exercises
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.
Notice how the type of
y transitions? You can inspect a variable’s type with the
y changes from a
str (string) to an
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.In the above code, the variables x, price, and word are assigned different types.
floatrepresents numbers with decimal points.
Advance Your Python Skills: Complete Python Programming Course & Exercises
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.
However, initiating a variable name with a number is invalid.
In Python, variable names are case-sensitive. Thus,
NAME are distinct.
Explicit naming, which makes the code self-explanatory, is preferable. For instance, using camel casing:
Naming conventions include:
- Camel Case:
- Snake Case:
- Pascal Case:
The style chosen often depends on personal or organizational preference. Notably, camel case is widely adopted in languages like Java and C++.
Certain words are reserved by Python, meaning they cannot be used as variable names. To view these, use
Strings, which hold textual content, can be defined in three different ways:
word = 'Hello world'
Though all three declarations are valid, double quotes are most commonly used.
Once variables are assigned, they can be utilized and manipulated across the program. Python supports standard arithmetic operators.
x = 2
Variables can be displayed using the
print() function. To embed variables within textual content, we use formatted strings.
x = 5
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