Python Variables (original) (raw)

Skip to content

Summary: in this tutorial, you’ll learn about Python variables and how to use them effectively.

What is a variable in Python? #

When you develop a program, you need to manage many values. To store these values, you use variables.

In Python, a variable is a label you can assign a value to. A variable is always associated with a value. For example:

`message = 'Hello, World!' print(message)

message = 'Good Bye!' print(message)`Code language: Python (python)

Try it

Output:

Hello, World! Good Bye!Code language: Python (python)

In this example, message is a variable. It holds the string 'Hello, World!'. The print() function shows the message Hello, World! to the screen.

The next line assigns the string 'Good Bye!' to the message variable and print its value to the screen.

The variable message can hold various values at different times. And its value can change throughout the program.

Creating variables #

To define a variable, you use the following syntax:

variable_name = valueCode language: Python (python)

The = is the assignment operator. In this syntax, you assign a value to the variable_name.

The value can be anything like a number, a string, etc., that you assign to the variable.

The following defines a variable named counter and assigns the number 1 to it:

counter = 1Code language: Python (python)

Naming variables #

When you name a variable, you need to adhere to some rules. If you don’t, you’ll get an error.

The following are the variable rules that you should keep in mind:

The following guidelines help you define good variable names:

Summary #

Quiz #

Was this tutorial helpful ?