Python Basics

Variables and Data Types in Python

A variable gives a name to a value so you can use it later. Python decides the type from the value, so you never declare it yourself.

What is Variables and Data Types?

A variable gives a name to a value so you can use it later. Python decides the type from the value, so you never declare it yourself.

Store values in variables and learn Python's basic types: str, int, float and bool.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

Example code

Run code →
main.py
# A variable is a name that refers to a value. Python works out the type for you.
name = "Ada"          # str: text
age = 36              # int: whole number
height = 1.68         # float: decimal number
is_student = False    # bool: True or False

print(name, age, height, is_student)
print(type(name), type(age), type(height), type(is_student))

# Variables can be reassigned, even to a value of another type.
age = age + 1
print("Next year:", age)

# Convert between types with int(), float(), str() and bool().
print(int("42") + 1)
print(str(3.5) + " stars")
print(float(7) / 2)

Expected output

Ada 36 1.68 False
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Next year: 37
43
3.5 stars
3.5

How it works

type() shows a value's type. int(), float() and str() convert between types, which you need when text such as "42" must become a number.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in Python IDE →

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges