...

/

Variables and Data Types

Variables and Data Types

This lesson will focus on variables and the different data types in Python.

Variables

A variable is a name to which a value can be assigned. It is basically a placeholder to store information. We can store information in a variable and refer to it later by using the variable name. Variables are assigned values using the = operator.

Press + to interact
x = 'EDUCATIVE'
print(x)

When you run the above code, you will see EDUCATIVE printed on the screen. In line 1, we created a variable which we named x and assigned it the value EDUCATIVE. In the next line, when we said to Python to print(x), it printed what was assigned to the variable x.

As the name suggests, the value of a variable can be changed. Let’s see an example of that below.

Press + to interact
x = 'EDUCATIVE'
print(x)
# Change value of x
x = 99
print(x)

We have extended the code of the above example. When line 1 runs the variable x is assigned the value EDUCATIVE. The value of x is printed in line 2. But in line 5, we assign the value 99 to x. Now when we print x in line 6, the changed value of x, i.e., 99, is printed on the screen.

Naming rules

There are some naming rules and conventions that need to be kept in mind for naming variables. If these rules are not followed, Python will give an error.

  • The name can start with an upper- or lower-case alphabet.

  • A number can appear in the name, but not at the beginning.

  • The _ character can appear anywhere in the name.

  • Spaces are not allowed. Instead, we must use ...

Access this course and 1400+ top-rated courses and projects.