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

Kursinnhold

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
Built-in Functions

Welcome to the world of Python functions! In this chapter, we'll explore some of Python's most powerful built-in functions, which serve as essential tools for any Python developer.

First, let's watch as Alex demonstrates how to use some of these essential built-in functions:

What are Built-in Functions?

Built-in functions are predefined functions that come with Python, allowing you to use them in your code without writing additional functionality. These functions are designed to perform common tasks like calculations or data manipulation, making coding more efficient and streamlined.

For Python developers, mastering built-in functions is key to writing clean, efficient, and concise code.

Python offers a wide range of built-in functions. You've already encountered some, such as print(), len(), range(), and type(). Let's explore more commonly used built-in functions:

  • sum(): adds all items in an iterable (like a list) and returns the total, which is especially useful for working with numerical data;
123
checkout = [2.99, 5.49, 3.99] total = sum(checkout) print(total)
copy
  • max() and min(): return the largest and smallest elements in an iterable, respectively — ideal for comparisons or finding extremes;
123
freezer_temperatures = [38, 32, 41, 34, 40] print(max(freezer_temperatures)) print(min(freezer_temperatures))
copy
  • float(): converts a number or a string representing a number into a floating-point number (a number with decimals);
123456789
price1 = "3.99" price2 = 12 # Convert prices to float price1_converted = float(price1) price2_converted = float(price2) print(f"Price #1 is ${price1_converted} and is of type {type(price1_converted)}") print(f"Price #2 is ${price2_converted} and is of type {type(price2_converted)}")
copy
  • int(): converts a number or a string representing a number into an integer. This is helpful when working with whole numbers or converting input data to integers;
12345678
price = 3.99 quantity = "4" # Calculate the total cost total_cost = int(quantity) * price print(f"The total cost for {quantity} items is ${total_cost}") print(f"Converting the total cost to an integer results in ${int(total_cost)}")
copy

Note

When a floating-point value is converted to an integer, the decimal portion is simply removed (the value is truncated).

  • sorted(): returns a new, sorted list from an iterable (like lists, tuples, or dictionaries). Unlike the sort() method, sorted() does not modify the original data and works on a wider range of types;
123456
fruit_prices = {"cherries": 3.99, "apples": 2.99, "bananas": 1.49} # Sorting the dictionary keys alphabetically sorted_prices = sorted(fruit_prices) print(sorted_prices)
copy
  • zip(): combines two or more iterables (e.g., lists) into a single iterable of tuples, pairing elements from each iterable together.
123456789
products = ["apple", "banana", "cherry"] prices = [0.99, 0.59, 2.99] stock = [50, 100, 25] # `zip()` combines the 3 lists into a series of tuples # `list()` converts the zip object into a list product_info = list(zip(products, prices, stock)) print("Product information:", product_info)
copy
Oppgave

Swipe to start coding

Process product data from a dictionary where prices and quantities are stored as strings. Your goal is to calculate total sales for each product and generate summary statistics.

  • Loop through the products dictionary.
  • For each product:
    • Convert the price to a float;
    • Convert the quantity sold to an int;
    • Multiply them to get the total sales for that product;
    • Append the total sales to total_sales_list.
  • Use sum() to calculate the total sum of all sales.
  • Assign the total sum to the total_sum variable.
  • Use min() and max() to get the minimum and maximum sales values.
  • Assign the minimum value to the min_sales variable.
  • Assign the maximum value to the max_sales variable.

Output Requirements

  • For each product, print:
    Total sales for <product>: $<total_sales>
  • After processing all products, print:
    • Total sum of all sales: $<total_sum>
    • Minimum sales: $<min_sales>
    • Maximum sales: $<max_sales>

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 1
toggle bottom row

book
Built-in Functions

Welcome to the world of Python functions! In this chapter, we'll explore some of Python's most powerful built-in functions, which serve as essential tools for any Python developer.

First, let's watch as Alex demonstrates how to use some of these essential built-in functions:

What are Built-in Functions?

Built-in functions are predefined functions that come with Python, allowing you to use them in your code without writing additional functionality. These functions are designed to perform common tasks like calculations or data manipulation, making coding more efficient and streamlined.

For Python developers, mastering built-in functions is key to writing clean, efficient, and concise code.

Python offers a wide range of built-in functions. You've already encountered some, such as print(), len(), range(), and type(). Let's explore more commonly used built-in functions:

  • sum(): adds all items in an iterable (like a list) and returns the total, which is especially useful for working with numerical data;
123
checkout = [2.99, 5.49, 3.99] total = sum(checkout) print(total)
copy
  • max() and min(): return the largest and smallest elements in an iterable, respectively — ideal for comparisons or finding extremes;
123
freezer_temperatures = [38, 32, 41, 34, 40] print(max(freezer_temperatures)) print(min(freezer_temperatures))
copy
  • float(): converts a number or a string representing a number into a floating-point number (a number with decimals);
123456789
price1 = "3.99" price2 = 12 # Convert prices to float price1_converted = float(price1) price2_converted = float(price2) print(f"Price #1 is ${price1_converted} and is of type {type(price1_converted)}") print(f"Price #2 is ${price2_converted} and is of type {type(price2_converted)}")
copy
  • int(): converts a number or a string representing a number into an integer. This is helpful when working with whole numbers or converting input data to integers;
12345678
price = 3.99 quantity = "4" # Calculate the total cost total_cost = int(quantity) * price print(f"The total cost for {quantity} items is ${total_cost}") print(f"Converting the total cost to an integer results in ${int(total_cost)}")
copy

Note

When a floating-point value is converted to an integer, the decimal portion is simply removed (the value is truncated).

  • sorted(): returns a new, sorted list from an iterable (like lists, tuples, or dictionaries). Unlike the sort() method, sorted() does not modify the original data and works on a wider range of types;
123456
fruit_prices = {"cherries": 3.99, "apples": 2.99, "bananas": 1.49} # Sorting the dictionary keys alphabetically sorted_prices = sorted(fruit_prices) print(sorted_prices)
copy
  • zip(): combines two or more iterables (e.g., lists) into a single iterable of tuples, pairing elements from each iterable together.
123456789
products = ["apple", "banana", "cherry"] prices = [0.99, 0.59, 2.99] stock = [50, 100, 25] # `zip()` combines the 3 lists into a series of tuples # `list()` converts the zip object into a list product_info = list(zip(products, prices, stock)) print("Product information:", product_info)
copy
Oppgave

Swipe to start coding

Process product data from a dictionary where prices and quantities are stored as strings. Your goal is to calculate total sales for each product and generate summary statistics.

  • Loop through the products dictionary.
  • For each product:
    • Convert the price to a float;
    • Convert the quantity sold to an int;
    • Multiply them to get the total sales for that product;
    • Append the total sales to total_sales_list.
  • Use sum() to calculate the total sum of all sales.
  • Assign the total sum to the total_sum variable.
  • Use min() and max() to get the minimum and maximum sales values.
  • Assign the minimum value to the min_sales variable.
  • Assign the maximum value to the max_sales variable.

Output Requirements

  • For each product, print:
    Total sales for <product>: $<total_sales>
  • After processing all products, print:
    • Total sum of all sales: $<total_sum>
    • Minimum sales: $<min_sales>
    • Maximum sales: $<max_sales>

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 1
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt