In Python, a variable is a named location in memory where a value can be stored and retrieved. When you create a variable, you give it a name, and you can use the name to access the value stored in the variable.
To create a variable in Python, give it a name and assign it a value using the equal sign (=).
For example:
1 2 | x = 10 y = "Hello, World!" |
Here,
x
is an integer variable with a value of
10
, and
y
is a string variable with a value of
"Hello, World!"
.
To access the value of a variable, simply use the variable name.
For example:
1 2 | print(x) # prints 10 print(y) # prints "Hello, World!" |
You can also perform operations on variables and assign the result to a new variable:
1 | z = x + 30 # z is now 40 |
When you create a variable in Python, you do not need to define its type. Based on the value you enter, the interpreter will automatically assign a type to the variable. In the code above, for example, x is an integer and y is a string.
It’s generally a good idea to give your variables descriptive names to make your code easier to understand. For example, instead of using
x
and
y
, you might use
price
and
item_name
to make it clear what the variables are used for.
Rules for creating Python variables:
- Variable names can only contain letters, numbers, and underscores. They cannot start with a number.
- Python is case-sensitive, so
my_variable_name
is not the same asmy_Variable_Name
. It’s a good practice to use lowercase letters and underscores for variable names. - You cannot use Python keywords (e.g.
def
,for
,while
, etc.) as variable names. - It’s a good idea to use descriptive names for your variables to make your code easier to read and understand. For example,
teacher_name
is a much clearer name thanx
.
Deleting variable in Python:
You can use the
del
statement to delete a variable.
For example:
1 2 3 4 | x = 40 print(x) # prints 40 del x print(x) # generates an error because x no longer exists |
Assigning a single value to multiple variables
You can also assign the same value to multiple variables in a single line of code.
For example:
1 2 3 4 | x = y = z = 20 print(x) # prints 20 print(y) # prints 20 print(z) # prints 20 |
Assigning different values to multiple variables
You can assign different values to multiple variables in a single line of code.
1 2 3 4 | x = y = z = 20, 10.2, "stringvalue" print(x) # prints 20 print(y) # prints 10.2 print(z) # prints stringvalue |
Python Variable Type:
You can use the
type()
function to check the type of a variable in Python. For example:
1 2 3 4 5 | x = 30 print(type(x)) # prints "<class 'int'>" y = 'hello' print(type(y)) # prints "<class 'str'>" |
In Python, you don’t need to specify the type of a variable when you create it. The interpreter automatically assigns the correct type to the variable based on the value you assign to it.
Python has a number of built-in data types that you can use to store values in variables. These include:
-
int
(for integers) -
float
(for floating point numbers) -
complex
(for complex numbers) -
bool
(for boolean valuesTrue
andFalse
) -
str
(for strings) -
list
(for lists) -
tuple
(for tuples) -
set
(for sets) -
dict
(for dictionaries)