Lesson 5 of 4013%

(Progress persistence is disabled until Phase 9)

Python
Beginner

Variables

Learn how to store data values in variables.

Variables in Python

What You’ll Learn

Variables are containers for storing data values. In this lesson, you will learn how to create and use them.

Creating Variables

Python has no command for declaring a variable (unlike let or const in JavaScript). A variable is created the moment you first assign a value to it.

x = 5
name = "John"
print(x)
print(name)

Expected Output:

5
John

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for Python variables:

  • Must start with a letter or the underscore character.
  • Cannot start with a number.
  • Can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
  • Case-sensitive (age, Age and AGE are three different variables).

Best Practices

In Python, the standard naming convention for variables is snake_case (all lowercase, words separated by underscores).

# Good variable names
user_age = 25
is_active = True
first_name = "Alice"

Reassigning Variables

Variables do not need to be declared with any particular type, and can even change type after they have been set.

x = 4       # x is of type int
x = "Sally" # x is now of type str
print(x)

Lesson Progress

Practice This TopicTake QuizBuild a Python Project

Ready to practise?

Apply what you learned with a hands-on challenge.

Practise This Topic