Skip to content
CodeNest

FoundationsLesson 2 of 1914 min

Variables and Types

Give values names so you can reuse them, and learn the four core types you will use constantly.

By the end you will be able to

  • Assign values to variables and reassign them
  • Identify int, float, str, and bool values
  • Convert between types deliberately with int(), float(), and str()

A variable is a name attached to a value. You create one with =, which is not the equals of mathematics — read it as "gets" or "is assigned".

Python
Output
Python 20 4.8 True
— expected output; press Run to execute it yourself

The name goes on the left, the value on the right. Python evaluates the right side first, then binds the result to the name.

A variable can be reassigned at any time, and the new value can even be a different type.

Python
Output
10
15
ten
— expected output; press Run to execute it yourself

The four core types

TypeMeaningExamples
intwhole number0, 42, -7
floatnumber with a decimal point3.14, -0.5, 2.0
strtext, in quotes"hi", 'a', ""
booltruth valueTrue, False

type() tells you what you are holding — invaluable when debugging.

Python
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
— expected output; press Run to execute it yourself

Converting on purpose

When you do need to cross between types, say so explicitly with int(), float(), or str().

Python
Output
31
39.98
Year 2026
7
— expected output; press Run to execute it yourself

Naming rules and habits

Names may contain letters, digits, and underscores, and may not start with a digit. They are case-sensitive: total and Total are two different variables.

The Python convention is snake_case — lowercase words joined by underscores. Prefer items_sold over x; the extra typing pays for itself the first time you reread the code.

Exercise

Create a variable minutes holding the integer 150. Then create hours holding that value converted to a float number of hours (150 / 60). Print hours.

Python
Output

Press Run to execute this code.

Check your understanding

0/3 answered
  1. 1.After x = 5 then x = x + 2, what is x?
  2. 2.What is the type of 3.0?
  3. 3.What does int(9.7) produce?