TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 3 of 22 · By Ethan Cole · Updated November 21, 2026

Python Variables: Names, Assignment and Types

A variable gives a name to a value so you can use it later. In Python you create one simply by assigning to it; there is no declaration keyword and no type to state in advance.

Assignment binds a name to a value

city = "Denver" evaluates the right-hand side, then makes the name city refer to the resulting value. The single = means "bind", not "equals"; comparing values uses ==, covered in the booleans chapter.

Assigning again does not change the old value. It simply points the name at a new one. That is why population = population + 1000 works: Python computes the new number using the old binding, then rebinds the name.

Values have types, names do not

Every value has a type, such as str for text or int for whole numbers. The name itself has no fixed type, so the same variable could hold a number now and text later. That flexibility is convenient, but in practice keep one meaning per name to stay readable. The built-in type() function tells you what kind of value a name currently refers to.

Python 3 · editable

Expected output

Denver 715522
716522
20 10
<class 'str'> <class 'int'>

Two more features appear above. x, y = 10, 20 assigns several names at once by position. x, y = y, x swaps two values in one line, because Python builds the right-hand side completely before assigning anything. The underscore in 715_522 is only a visual separator for long numbers.

Naming rules and conventions

Names may contain letters, digits and underscores, and must not start with a digit. They are case-sensitive: total and Total are different. Reserved words such as if, for and class cannot be used. By convention, Python variables use snake_case: lowercase words joined by underscores, like max_speed. Choose names that say what the value means; price_usd beats p.

Common mistakes

  • Using a name before assigning it, which raises NameError.
  • Reading = as a comparison. x = 5 stores; x == 5 asks a question.
  • Shadowing built-ins, for example naming a variable list or print, which hides the built-in function for the rest of the program.

Summary

  • name = value creates or rebinds a variable; no declaration is needed.
  • Values have types; names can point at values of any type.
  • Multiple assignment and swapping work in a single line.
  • Use descriptive snake_case names and avoid reserved words.

Quiz: Variables

1. After a = 5 and then a = a * 2, what is a?

Python evaluates a * 2 with the old value 5, then rebinds a to 10.

2. What does x, y = y, x do?

The right side is evaluated first into a pair, then unpacked into x and y in that order.

3. Which of these is a valid variable name?

Names cannot start with a digit, cannot be keywords, and cannot contain hyphens.

4. What does type(3) return?

3 is a whole number, so its type is int.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.