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.
1# Creating variables is like labeling boxes2student_name = "Maria"3age = 154grade = 9.55is_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!
1# Examples of strings2message = "Hello, World!"3favorite_food = 'Pizza'4long_text = """This is a5multi-line6text."""78# You can combine (concatenate) strings9greeting = "Hi, " + student_name10print(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.
1# Integers (whole numbers)2age = 153number_of_siblings = 245# Floats (decimal numbers)6height = 1.757temperature = 98.689# Basic math operations10total_cost = 10.99 + 5.9911print(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).
1# Boolean examples2is_sunny = True3is_raining = False45# Common comparisons that result in booleans6is_teenager = age >= 13 and age <= 197print(is_teenager) # Output: True89has_passed = grade >= 7.010print(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)
1# Good variable names2user_name = "John"3age_in_years = 254total_score = 95.556# Bad variable names (don't do this!)71st_place = "Gold" # Can't start with a number8my-name = "Alex" # Can't use hyphens9class = "Math" # Can't use Python keywords
Practice Exercise
Let's create a simple program that stores information about a student:
1# Create variables for a student profile2student_name = "Alex"3student_age = 164student_grade = 9.85is_enrolled = True67# Print student information8print("Student Profile:")9print("Name:", student_name)10print("Age:", student_age)11print("Grade:", student_grade)12print("Currently Enrolled:", is_enrolled)1314# Calculate if the student has honors (grade > 9.5)15has_honors = student_grade > 9.516print("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 moreMaster reading input from users and displaying output in Python.
Learn moreLearn about if statements, loops, and control flow in Python.
Learn more