Back

Activity 1.3: Variables and Data Types

divider

Activity 1.3

Variables and Data Types

Key Concepts

Storing Information

Variables and Data Types

What is a Variable?

A variable stores a value so your program can use it again later.

You Have Been Looking at Variables for Years

Street Fighter II gameplay. The HUD shows a score of 379601, a high score of 379601, two health bars, a countdown timer at 65, and both fighters labeled RYU

Score. High score. Health. Time. Every number on that bar is a value the game is storing and showing you.

Every Game Does This

A Terminator side-scrolling game. The HUD shows a score of 88884000, a score multiplier of x1, a health bar, a timer at 06:37, and a remaining-lives count of 3
  • Score changes when you hit something
  • Health changes when you get hit
  • Lives changes when you die

The game has to keep those three numbers somewhere. That somewhere is a variable.

What is a Variable?

Demo
name = "Anthony"
age = 35
print(name)
print(age)
Demo
Anthony
35

Data Types

Python automatically figures out what type of data a variable holds.

  • str — text, wrapped in quotes
  • int — a whole number
  • float — a decimal number

Data Types

Use the built-in type() function to check a variable's data type.

Demo
name = "Anthony"
age = 35
height = 5.9
print(type(name))
print(type(age))
print(type(height))
Demo
<class 'str'>
<class 'int'>
<class 'float'>

Naming Variables

  • Use all lowercase letters
  • Separate words with an underscore, like favorite_color
  • Choose descriptive names
  • Names cannot start with a number

Today's Objectives

  • Declaring and assigning variables
  • Displaying variable values
  • Identifying data types with type()

Key Terms

Variable
A named location that stores a value for later use.
Assignment
Storing a value in a variable using the = operator.
Data Type
The kind of value a variable holds, such as text or a number.
String (str)
A data type representing text.
Integer (int)
A data type representing a whole number.
Float (float)
A data type representing a decimal number.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-3-variables.


Task 1: Declare Your Variables

  • Create a name variable (a string) and an age variable (an integer).
  • Print each variable on its own line.
Variables and Data Types
name = "Anthony"
age = 35
print(name)
print(age)
#

Task 2: Add More Variables

  • Create a favorite_color variable (a string) and a favorite_number variable (a float).
  • Print each one using a label, e.g. print("Favorite Color:", favorite_color).
  • Note: separating two values with a comma inside print() prints them on the same line with a space in between.
Variables and Data Types
name = "Anthony"
age = 35
print(name)
print(age)
favorite_color = "green"
favorite_number = 7.5
print("Favorite Color:", favorite_color)
print("Favorite Number:", favorite_number)
#

Task 3: Check Your Data Types

  • Use type() to print the data type of each of your four variables.
Variables and Data Types
name = "Anthony"
age = 35
print(name)
print(age)
favorite_color = "green"
favorite_number = 7.5
print("Favorite Color:", favorite_color)
print("Favorite Number:", favorite_number)
print(type(name))
print(type(age))
print(type(favorite_color))
print(type(favorite_number))
#

Challenge (Optional): Reassign and Recheck

  • Reassign one of your variables to a value of a different data type. For example, change age = 35 to age = "thirty-five".
  • Print its data type again and compare the result.
divider

Checkpoint

Verify your program works correctly.

Example Output
Anthony
35
Favorite Color: green
Favorite Number: 7.5
<class 'str'>
<class 'int'>
<class 'str'>
<class 'float'>
divider

Reflection

Answer the following questions before submitting your work.

  1. What is a variable, and why are variables useful in a program?
  2. Python does not require you to announce a variable's data type ahead of time — it figures the type out automatically. What do you think would happen if you tried to use a variable before assigning it a value?
  3. If you completed the optional challenge, what did type() report after you reassigned a variable to a different data type?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete