Python Curriculum
Variables And Assignment
When you write scripts to manage systems, you need a way to store information. This could be a server's IP address, a backup folder path, or the number of active alerts. In programming, we store this information in variables. Think of a variable as a labeled box where you can keep data and change it whenever you need.
In this guide, we will learn how to create and use variables in Python. We will look at how Python stores variables in memory, how to name them clearly, and how to update them as your script runs.
What is a Variable?
A variable is just a name that points to a value. You assign a value to a name using the equals sign (=). Once assigned, you can use that name throughout your script instead of typing the raw value over and over.
Let's look at a comparison between hardcoded values and using variables in your scripts.
Here is a diagram showing how Python links variable names to values in your computer's memory:
Assigning Your First Variables
In Python, you do not need to declare a variable before using it or specify what kind of data it holds. Python looks at the value and figures out the type automatically.
Create a file named variables.py and write the code below.
Run this file in your terminal.
Expected output:
Reassigning Variables
Variables are called variable because their values can change. You can update a variable by assigning a new value to it. The old value is discarded, and the variable points to the new one.
Let's write a script that tracks the number of failed connection attempts.
Run the script to see how the count updates.
Expected output:
Naming Your Variables Natively
You should name your variables so that any other developer can understand what they store. Python community guidelines recommend using snake_case for variable names, which means using lowercase letters and separating words with underscores.
Let's compare good naming practices against bad ones.
- The Bad Way:
- The Good Way:
Real-World Example: Server Status Reporter
Let's write a script that manages system metadata using variables. This script stores server environment details, calculates resource usage, and prints a formatted health report.
Create a file named status_report.py and write the code below.
Run the script.
Expected output:
Common Mistakes
Using a Variable Before Defining It
If you try to print or use a variable before you assign a value to it, Python will crash with a NameError.
- The Bad Way:
- The Good Way:
Using Reserved Keywords
Python has a set of reserved words that you cannot use as variable names because they are part of the language structure. Examples include if, else, import, for, and class.
- The Bad Way:
- The Good Way:
Case Sensitivity Issues
Python treats uppercase and lowercase letters as completely different characters. Server_IP, server_ip, and SERVER_IP are three separate variables.
- The Bad Way:
- The Good Way:
Summary
- Variables store data values in labeled boxes so you can reuse them throughout your scripts.
- You assign a value to a variable name using the single equals sign (
=). - Python figures out the data type of your variable automatically at runtime.
- You can update the value of an existing variable by reassigning a new value to it.
- Use snake_case for variable names to keep your scripts easy to read.
- Avoid short, generic variable names like
xortempthat do not describe the data. - Do not try to use variables before you have assigned them a value.
- Avoid using Python's reserved keywords like
import,if, orforas variable names. - Remember that variable names in Python are case-sensitive.
- Using variables allows you to update connection details in one place instead of editing multiple files.