Variables and Data
Types in Python
How computers remember and respond to us! ✨
Have you ever filled out an online form or played a game that
remembered your name or score? How do you think it does that?
What are Variables?
A variable is a named storage location in computer memory that can hold different types of data and can be changed during program execution.
Think of a variable as a special jar with a label on it. You can put different things inside—like your name, your score in a game, or your favorite color—
and the computer remembers what's in each jar! 🏺
What would you name your own "jars" for different types of information?
Variables in Action! 🎮
Variables help computers remember important stuff! They store dynamic information in games (like changing scores or player levels) and in apps (like your username or
preferences). They're everywhere you interact digitally!
Imagine creating your own game. What information would you need to store and
track? 🤔
• Player's name?
• Current level?
• High score?
• Number of lives?
Getting Input with input()
The input() function allows a program to receive data from the user during runtime, returning the entered data as a string.
input() is like the computer asking you a question and waiting for your answer! 💬
name = input("What's your name? ") # The computer asksprint("Hello, " + name +
"!") # The computer replies
Understanding Data Types 📊
A data type defines the kind of data that can be stored in a variable and determines what operations can be performed on that
data.
Data types are like different categories that help the computer understand what kind of information it's working with!
Type What it is Examples
Integers (int) Whole numbers 10, -5, 0 (your age, number of apples) 🍎
Floats (float) Decimal numbers 3.14, 0.5 (prices, measurements) 📏
Strings (str) Text (words, letters, "Hello", "Python" (names, messages) 📝
symbols)
Booleans (bool) True or False values True, False (game over? light on?) 💡
Can you categorize these: your shoe size, your favorite animal, and whether it's raining outside?
Type Conversion: Input always returns
Strings! 🔄
When you get input from a user, Python treats it as text (string) by default. If you want to do math with it, you
need to convert it!
The Problem:
age_str = input("How old are you? ") # input is "10" (a string)# print(age_str + 5) #
Build Your Own Form! 📝
Let's create a simple program that asks for your name and your favorite number, then says something fun!
# Ask for the user's nameuser_name = input("What's your name, future coder? ")# Ask for their
favorite number and convert it to an integerfav_num_str = input("What's your favorite whole
number? ")fav_num = int(fav_num_str) # Convert the text input to a number!# Print a fun
message using the gathered informationprint("Hello, " + user_name + "!")print("Did you know "
+ str(fav_num) + " is a super cool number?
Recap: Python's Memory Magic! ✨
Variables are Jars: Input is Conversation:
Labeled containers for your data. You can change The input() function lets the computer ask you
what's inside! questions.
Data Types are Categories: Conversion is Transformation:
Like int, float, str, bool – they tell Python what kind Turn input (text) into numbers using int() or float()
of data it is. when needed!
Quick Quiz: What data type is "True"? What function do you use to get user input?
Your First Python
Calculator! ➕
Let's write a program that asks for two numbers and adds them together!
# Ask for the first numbernum1_str = input("Enter the first
number: ")num1 = int(num1_str) # Convert to integer# Ask for the
second numbernum2_str = input("Enter the second number: ")num2 =
int(num2_str) # Convert to integer# Add the numberssum_result =
num1 + num2# Print the resultprint("The sum of your numbers is:
" + str(sum_result))
Try running this code! What happens if you type words instead of numbers?