Variables in Python
A variable is a named storage location in a Python program that stores a value or a reference to an object. Variables are used to hold data that may be changed during the execution of the program.
Variable names
In Python, variable names can consist of letters, digits, and underscores, but they cannot begin with a digit. Variable names are case-sensitive, so
myvar
and
myVar
are different variables. It is common practice to use lowercase letters for variable names, with words separated by underscores. For example:
x = 42
my_name = "Alice"
is_valid = True
In this example,
x
is a variable that holds the integer value
42
,
my_name
is a variable that holds the string value
"Alice"
, and
is_valid
is a variable that holds the boolean value
True
.
Assigning values to variables
In Python, you can assign a value to a variable using the equals (
=
) operator. For example:
x = 42
y = x + 3
z = x * y
print(x, y, z)
In this example,
x
is assigned the value
42
,
y
is assigned the value
x + 3
(which is
45
), and
z
is assigned the value
x * y
(which is
1890
). The
print
statement outputs the values of
x
,
y
, and
z
to the screen.
Variable types
In Python, variables can hold values of different types, including integers, floating-point numbers, strings, booleans, lists, tuples, dictionaries, and more. The type of a variable is determined automatically when a value is assigned to it. You can use the
type
function to determine the type of a variable. For example:
x = 42
y = 3.14
z = "Hello, World!"
is_valid = True
print(type(x))
print(type(y))
print(type(z))
print(type(is_valid))
In this example,
x
is assigned the integer value
42
,
y
is assigned the floating-point value
3.14
,
z
is assigned the string value
"Hello, World!"
, and
is_valid
is assigned the boolean value
True
. The
type
function is used to determine the type of each variable.
Conclusion
Variables are an important concept in Python programming, and understanding how to use them is essential for writing effective code. By using variables, you can store and manipulate data in your programs, making them more powerful and flexible.
In this article, we covered the basics of variables in Python, including variable names, assigning values to variables, and variable types. With this knowledge, you should be well-equipped to start writing Python programs that use variables to store and manipulate data.
# Example program that demonstrates variables
# Define some variables
name = "John Doe"
age = 42
height = 1.78
is_married = True
# Print out the values of the variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Married:", is_married)
# Change the value of a variable
age = 43
# Print out the new value of the variable
print("New age:", age)
In this example program, we define some variables to store information about a person, including their name, age, height, and marital status. We then print out the values of these variables using the
print
function. Finally, we change the value of the
age
variable and print out the new value.
Variables are a powerful tool in Python programming, and they allow you to store and manipulate data in a flexible and dynamic way. By mastering the basics of variables, you can take your Python programming skills to the next level.