Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using Variables | Variables and Types
Introduction to Python (copy)
course content

Kursusindhold

Introduction to Python (copy)

Introduction to Python (copy)

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

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
Opgave

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.

Output Requirements

Print the result in the following format:

  • Total cost for <purchase_quantity> cases of <item_name> is $ <total_cost>

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
toggle bottom row

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
Opgave

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.

Output Requirements

Print the result in the following format:

  • Total cost for <purchase_quantity> cases of <item_name> is $ <total_cost>

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt