In Python, variables are used to store data values. Unlike other programming languages, Python does not require you to explicitly declare the data type of a variable. Instead, the data type is inferred from the value assigned to the variable. Here's an example:
python# Assigning an integer value to a variable
x = 5
# Assigning a string value to a variable
y = "Hello, world!"
# Assigning a boolean value to a variable
z = True
In the above example, x
is an integer variable, y
is a string variable, and z
is a boolean variable.
You can also assign multiple variables in one line using commas:
python# Assigning multiple variables in one line
a, b, c = 1, "two", True
In this example, a
is assigned the value of 1, b
is assigned the value of "two", and c
is assigned the value of True.
Python also supports reassigning variables to new values:
python# Reassigning the value of a variable
x = 5
x = 10
In this example, x
is initially assigned the value of 5, but is then reassigned the value of 10.
Variable names in Python can consist of letters, numbers, and underscores, but they cannot start with a number. It is also a convention in Python to use lowercase variable names, with underscores separating words in multi-word variable names.