Progress0%

3 of 20 topics completed

Python Variables and Data Types

In this tutorial, you'll learn about variables and data types in Python. We'll use real-world examples that you can relate to, making it easier to understand these fundamental concepts.

What is a Variable?

Think of a variable as a container or a labeled box where you can store different types of information. Just like how you might label a box "School Supplies" to store your notebooks and pens, in Python, we use variables to store data.

Python
1# Creating variables is like labeling boxes
2student_name = "Maria"
3age = 15
4grade = 9.5
5is_present = True

Common Data Types in Python

1. Strings (text)

Strings are used to store text. Think of your favorite social media platform - when you write a post or message, you're creating a string!

Python
1# Examples of strings
2message = "Hello, World!"
3favorite_food = 'Pizza'
4long_text = """This is a
5multi-line
6text."""
7
8# You can combine (concatenate) strings
9greeting = "Hi, " + student_name
10print(greeting) # Output: Hi, Maria

2. Numbers

Python has two main types of numbers: integers (whole numbers) and floats (decimal numbers). Think of counting money - whole dollars would be integers, while cents would make it a float.

Python
1# Integers (whole numbers)
2age = 15
3number_of_siblings = 2
4
5# Floats (decimal numbers)
6height = 1.75
7temperature = 98.6
8
9# Basic math operations
10total_cost = 10.99 + 5.99
11print(total_cost) # Output: 16.98

3. Booleans (True/False)

Booleans are like simple yes/no or on/off switches. Think of your phone's settings - WiFi can be either on (True) or off (False).

Python
1# Boolean examples
2is_sunny = True
3is_raining = False
4
5# Common comparisons that result in booleans
6is_teenager = age >= 13 and age <= 19
7print(is_teenager) # Output: True
8
9has_passed = grade >= 7.0
10print(has_passed) # Output: True

Variable Naming Rules

When naming variables in Python, follow these simple rules:

  • Start with a letter or underscore
  • Can contain letters, numbers, and underscores
  • Cannot use Python keywords (like 'if', 'for', 'while')
  • Case-sensitive (age and Age are different variables)
Python
1# Good variable names
2user_name = "John"
3age_in_years = 25
4total_score = 95.5
5
6# Bad variable names (don't do this!)
71st_place = "Gold" # Can't start with a number
8my-name = "Alex" # Can't use hyphens
9class = "Math" # Can't use Python keywords

Practice Exercise

Let's create a simple program that stores information about a student:

Python
1# Create variables for a student profile
2student_name = "Alex"
3student_age = 16
4student_grade = 9.8
5is_enrolled = True
6
7# Print student information
8print("Student Profile:")
9print("Name:", student_name)
10print("Age:", student_age)
11print("Grade:", student_grade)
12print("Currently Enrolled:", is_enrolled)
13
14# Calculate if the student has honors (grade > 9.5)
15has_honors = student_grade > 9.5
16print("Honors Student:", has_honors)

🎯 Try it yourself!

Create your own variables to store information about:

  • Your favorite movie (title, year, rating)
  • A recipe (name, cooking time, difficulty level)
  • A game score (player name, points, is_high_score)

Related Tutorials

Learn about different types of operators in Python and how to use them.

Learn more

Master reading input from users and displaying output in Python.

Learn more

Learn about if statements, loops, and control flow in Python.

Learn more