Learn to Scode

2.Variables and Data Types

Understand how to store and work with different types of data in your programs.

20 minutes
Beginner
Article Content

What are Variables?

Variables are like containers that store information in your program. They have names and can hold different types of data.

Creating Variables

In Python, you create a variable by giving it a name and assigning a value:

name = "Alice"
age = 25
is_student = True

Basic Data Types

Python has several basic data types:

Numbers

Integers (whole numbers) and decimals:

age = 25          # integer
height = 5.8       # decimal (float)
temperature = -5   # negative number

Strings

Text enclosed in quotes:

name = "Alice"
message = 'Hello, World!'
favorite_color = "blue"

Booleans

True or false values:

is_student = True
is_working = False
has_car = True

Working with Variables

You can use variables in calculations and combine them:

# Math with numbers
x = 10
y = 5
sum = x + y        # 15
product = x * y    # 50

# Combining strings
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name  # "John Doe"

🎯 Practice Exercise

Try creating variables for your name, age, and favorite programming language. Then print them out!

Variable Naming Rules

  • Use descriptive names (e.g., user_age instead of a)
  • Use lowercase letters and underscores
  • Don't start with numbers
  • Avoid Python keywords (like if, for, while)