Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using Variables | Variables and Types
Introduction to Python (generated task tests)

Swipe to show menu

book
Using Variables

Mastering variables isn't just about storing data β€” it's also about using this data to perform meaningful operations. Variables allow you to calculate and manipulate data, enabling you to handle tasks such as pricing adjustments or inventory management in your programs.

Let's see how Alex uses variables that store different data types:

First, let's see how we can use variables to calculate the total cost of items. This example multiplies the price per unit by the quantity to determine how much it would cost to buy a certain number of items:

123456
# Calculate the total cost of 100 cupcakes item_price = 0.50 item_quantity = 100 total_cost = item_price * item_quantity print("Total cost for", item_quantity, "cupcakes is $", total_cost)
copy
Task

Swipe to start coding

Use basic arithmetic by calculating the total cost of a grocery item using variables.

Code Instructions

  • Create a variable item_name and assign it the string "Soda".
  • Create a variable item_price and assign it the float 6.99 (price per case).
  • Create a variable purchase_quantity and assign it the integer 5 (number of cases).
  • Calculate the total cost by multiplying item_price by purchase_quantity, and store the result in total_cost.

Requirements checklist

  1. Check that the variable item_name is equal to "Soda".
  2. Check that the variable item_price is equal to 6.99 (with a tolerance for floating point comparison, e.g., abs(item_price - 6.99) < 1e-6).
  3. Check that the variable purchase_quantity is equal to 5.
  4. Check that the variable total_cost is equal to item_price multiplied by purchase_quantity (with a tolerance for floating point comparison, e.g., abs(total_cost - (item_price * purchase_quantity)) < 1e-6).

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!

SectionΒ 2. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

Awesome!

Completion rate improved to 2.17

book
Using Variables

Mastering variables isn't just about storing data β€” it's also about using this data to perform meaningful operations. Variables allow you to calculate and manipulate data, enabling you to handle tasks such as pricing adjustments or inventory management in your programs.

Let's see how Alex uses variables that store different data types:

First, let's see how we can use variables to calculate the total cost of items. This example multiplies the price per unit by the quantity to determine how much it would cost to buy a certain number of items:

123456
# Calculate the total cost of 100 cupcakes item_price = 0.50 item_quantity = 100 total_cost = item_price * item_quantity print("Total cost for", item_quantity, "cupcakes is $", total_cost)
copy
Task

Swipe to start coding

Use basic arithmetic by calculating the total cost of a grocery item using variables.

Code Instructions

  • Create a variable item_name and assign it the string "Soda".
  • Create a variable item_price and assign it the float 6.99 (price per case).
  • Create a variable purchase_quantity and assign it the integer 5 (number of cases).
  • Calculate the total cost by multiplying item_price by purchase_quantity, and store the result in total_cost.

Requirements checklist

  1. Check that the variable item_name is equal to "Soda".
  2. Check that the variable item_price is equal to 6.99 (with a tolerance for floating point comparison, e.g., abs(item_price - 6.99) < 1e-6).
  3. Check that the variable purchase_quantity is equal to 5.
  4. Check that the variable total_cost is equal to item_price multiplied by purchase_quantity (with a tolerance for floating point comparison, e.g., abs(total_cost - (item_price * purchase_quantity)) < 1e-6).

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

Swipe to show menu

some-alt