Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Storing Data in Variables | Variables and Types
Introduction to Python

bookStoring Data in Variables

Brilliant work mastering data types! Next, we'll explore how to store data inside variables, much like organizing your groceries into the right bins. This makes your data easy to access and manipulate β€” key to any efficient coding project.

Watch Alex as he demonstrates how to store data in variables:

Let's put this into practice. Here, we're assigning various types of data to variables, each representing different aspects of a grocery item.

12345678910
# Storing different types of data in variables item_name = "Banana" item_price = 0.50 item_quantity = 100 in_stock = True print("Item:", item_name) print("Price: $", item_price) print("Quantity:", item_quantity) print("In stock:", in_stock)
copy

This example shows how easily you can access and display the data once it's stored in variables. Notice how each variable type (string, float, integer, boolean) serves a specific purpose in our grocery analogy.

Reassigning Variables

Once a variable is created, you can change its value at any time. This is called reassigning a variable β€” the old value gets replaced with a new one.

123456
item_name = "Banana" print("Item:", item_name) # Reassigning the variable item_name = "Apple" print("Item:", item_name)
copy

Here, you first store "Banana" in item_name, and later replace it with "Apple". The variable itself stays the same, but its content is updated.

Task

Swipe to start coding

Define and use variables to represent details of a grocery item β€” "Eggs". You will assign values for its name, price, quantity, and stock status.

  • Create a variable item_name and assign it the string "Eggs".
  • Create a variable item_price and assign it the float 3.99 (price per egg).
  • Create a variable item_stock_quantity and assign it the integer 120 (number of eggs in stock).
  • Create a variable in_stock and assign it the boolean True (eggs are in stock).

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about the different data types used in the example?

How do I choose good variable names in Python?

What happens if I try to use a variable before assigning a value to it?

close

Awesome!

Completion rate improved to 2.17

bookStoring Data in Variables

Swipe to show menu

Brilliant work mastering data types! Next, we'll explore how to store data inside variables, much like organizing your groceries into the right bins. This makes your data easy to access and manipulate β€” key to any efficient coding project.

Watch Alex as he demonstrates how to store data in variables:

Let's put this into practice. Here, we're assigning various types of data to variables, each representing different aspects of a grocery item.

12345678910
# Storing different types of data in variables item_name = "Banana" item_price = 0.50 item_quantity = 100 in_stock = True print("Item:", item_name) print("Price: $", item_price) print("Quantity:", item_quantity) print("In stock:", in_stock)
copy

This example shows how easily you can access and display the data once it's stored in variables. Notice how each variable type (string, float, integer, boolean) serves a specific purpose in our grocery analogy.

Reassigning Variables

Once a variable is created, you can change its value at any time. This is called reassigning a variable β€” the old value gets replaced with a new one.

123456
item_name = "Banana" print("Item:", item_name) # Reassigning the variable item_name = "Apple" print("Item:", item_name)
copy

Here, you first store "Banana" in item_name, and later replace it with "Apple". The variable itself stays the same, but its content is updated.

Task

Swipe to start coding

Define and use variables to represent details of a grocery item β€” "Eggs". You will assign values for its name, price, quantity, and stock status.

  • Create a variable item_name and assign it the string "Eggs".
  • Create a variable item_price and assign it the float 3.99 (price per egg).
  • Create a variable item_stock_quantity and assign it the integer 120 (number of eggs in stock).
  • Create a variable in_stock and assign it the boolean True (eggs are in stock).

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

close

Awesome!

Completion rate improved to 2.17
SectionΒ 2. ChapterΒ 2
single

single

some-alt