This tutorial explains the concept of variables in Python, their types, and how to use them with examples in real-world projects.
In Python, a variable is a reserved memory location that stores a value.
They are names that can be assigned a value and used to reference it throughout your code. Using a variable makes a value accessible & gives values a context/meaning concerning your code.
Before you start, I hope you have Python installed and set up a basic editor. If not, do refer to my below guides:
Table of Contents
- Variables are case-sensitive.
- Variable names can only contain uppercase and lowercase letters (A–Z, a–z), digits (0–9), and underscores (_).
- They can not start with digits.
- Python variables are dynamically typed.
- Python supports Unicode variables (e.g. decorated letters like é and ü, and even Chinese, Japanese, and Arabic symbols).
- As per PEP 8 standard, variable names can be only lowercase, and words can be separated by underscores (e.g. total_price).
Python Variables: Examples
To create a variable in Python, we need to assign a value to it using the assignment operator (=). For example, the following line of code creates a variable named “x” and assigns it the value of 10:
x = 10
In Python, variables are dynamically typed, which means that the interpreter can determine the data type of a variable based on the value it is assigned. Python supports various types of variables, including integers, floats, strings, booleans, and complex numbers.
# Integer Variable age = 20 # Float Variable price = 4.99 # String Variable name = "John Doe" # Boolean Variable is_active = True # Complex Variable z = 2 + 3j
Variables are useful in programming because they allow us to store and manipulate data. For example, we can use variables to perform mathematical operations, concatenate strings, and make decisions based on the value of a boolean variable.
# Mathematical Operations x = 5 y = 10 z = x + y print(z) # Output: 15 # String Concatenation first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name print(full_name) # Output: John Doe
Variables can also be used in real-world projects to store and manipulate data. For example, in a web application, we can use variables to store user input, database queries, and output data to the user.
# User Input name = input("What is your name? ") print("Hello, " + name + "!") # Output: Hello, John! # Database Queries import sqlite3 conn = sqlite3.connect("example.db") cursor = conn.cursor() cursor.execute("SELECT * FROM users WHERE id = ?", (1,)) user = cursor.fetchone() print(user) # Output: (1, 'John Doe', 'firstname.lastname@example.org') # Output Data to User balance = 100.00 print("Your current balance is ₹" + str(balance)) # Output: Your current balance is ₹100.0
Common Errors while using variables
There are a few common errors you can face while dealing with variables. Here are some of them.
NameError: This error occurs when you try to access a variable that has not been defined. For example, if you try to print the value of a variable that has not been assigned a value yet, you will get a NameError. The following code gives a NameError because the variable “Full_name” is not defined.
# NameError demonstration first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name print(Full_name) # NameError
TypeError: This error occurs when you try to operate on a variable of the wrong data type. For example, if you try to concatenate a string and an integer, you will get a TypeError. The below snippet of code gives TypeError.
# TypeError demonstration first_name = "John" age = 10 print(first_name + age)
ValueError: This error occurs when you try to convert a variable to a different data type, but the conversion is impossible. For example, if you try to convert a string that contains letters to an integer, you will get a ValueError.
# ValueError demonstration first_name = "John" age = 10 print(int(first_name))
Understanding variables is essential for any Python developer. Variables allow us to store and manipulate data, perform mathematical operations, concatenate strings, etc. I hope this guide clarifies the concept if you are a beginner in Python programming.