variables
---
id: variables
title: A Container for Data, Variables
description: The concept of variables and how to declare variables in Python
tags:
- Variables
- Python
- Programming
- Beginners
sidebar_position: 15
isPublic: false
---
# A Container for Data, Variables
In programming, a `Variable` is essentially a container for storing data.
You can assign a name to data to store a value, and you can modify or reference the stored value as needed.
<br />
## The Meaning of Variables
A variable is essentially a name that refers to a memory location used to temporarily store data and instructions.
In Python, you don't need any special keywords (e.g., `var`, `let`) to declare a variable. Simply use a variable name and assign a value with the equals sign (=).
```python title="Variable Declaration and Assignment"
my_variable = 10 # Assign 10 to the variable my_variable
name = "CodeFriends" # Assign "CodeFriends" to the variable name
Declaring and Initializing Variables
To declare
a variable means to prepare the variable for use.
When declaring a variable, assigning a value to it is referred to as initialization
.
Declaring and Initializing a Variable
# Declare the variable my_var
my_var = 10 # Initialize by assigning 10
In Python, if you declare a variable without initializing it, a NameError
will occur.
Declaring a Variable without Initialization
# Attempt to declare a variable without initialization
x # NameError: name 'x' is not defined
Therefore, when declaring a variable in Python, you must initialize it using the equals sign (=).
Characteristics of Python Variables
-
Dynamic Typing
: The data type (the form of data like text or numbers) of a variable is determined during the program's execution, and there's no need to pre-declare the variable type. -
Reassignable
: You can change the value stored in a variable or reassign it with a value of a different type.
Dynamic Typing and Reassignment
number = 5 # Assign an integer (number)
number = "five" # Reassign with a string (text)
Coding Exercise
Try assigning the string "hello" to the variable greeting
.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.