Skip to main content

Variables

Variables are used to store data to the device.

Think of a box that you put stuff in, and then you can take stuff out.

Here, data means the stuff you put in the box.

It could be numbers, letters, lists, set, functions and more.

Declaring Variables

Python uses the following syntax to store data into name variable.

name = data

A real-world example looks like the following.

Using Variables

After saving the data into the variable, use can use whatever were saved in the variable.

However, the point of the variable is that you could change it (it is dynamic).

So, to access the variables, just reference it using the name of the variable.

name = 'John'
print(name)

Rules for the name

There are a few rules for the name of the variable.

If you do not follow these rules, error will be thrown (you won't be able to run the code).

  1. Only English and other letters, numbers, underscores(_) is allowed.
  2. The first letter cannot be a number (Something like 1name is not allowed).
  3. The name cannot be a reserved word (Something like if is not allowed).

Python Quiz 2

2 Questions
Quiz about variables! Test your knowledge about variables!