All labs · lesson 6 of 9

Pythonbeginner20 min

Python Basics: Variables and Data Types

Learn the fundamentals of Python programming including variables, strings, numbers, and basic operations.

Edit any code block and press Run (or Ctrl/⌘ + Enter, or Shift + Enter). The code runs in your browser, so there is nothing to install. The first run downloads the language once; later runs are instant.

Welcome to your first Python tutorial! In this lesson, you’ll learn about variables and basic data types.

What are Variables?

Variables are containers for storing data values. In Python, you don’t need to declare the type of a variable.

Exercise 1: Creating Variables

Try creating a variable and printing it:

# Create a variable called 'name' with your name
name = "Alex"
print("Hello, " + name)

Numbers in Python

Python has two main numeric types: integers (whole numbers) and floats (decimal numbers).

Exercise 2: Math Operations

Let’s do some basic math:

# Calculate the area of a rectangle
width = 5
height = 10
area = width * height
print("The area is:", area)

Strings

Strings are sequences of characters enclosed in quotes.

Exercise 3: String Operations

# Combine strings and use string methods
first_name = "Python"
last_name = "Programming"
full_name = first_name + " " + last_name
print(full_name.upper())
print(full_name.lower())

Great job! You’ve learned the basics of Python variables and data types.

Next: Python Lists and Loops