Kurssisisältö
Introduction to Python (copy)
Introduction to Python (copy)
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;
checkout = [2.99, 5.49, 3.99] total = sum(checkout) print(total)
max()
andmin()
: return the largest and smallest elements in an iterable, respectively — ideal for comparisons or finding extremes;
freezer_temperatures = [38, 32, 41, 34, 40] print(max(freezer_temperatures)) print(min(freezer_temperatures))
float()
: converts a number or a string representing a number into a floating-point number (a number with decimals);
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)}")
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;
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)}")
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 thesort()
method,sorted()
does not modify the original data and works on a wider range of types;
fruit_prices = {"cherries": 3.99, "apples": 2.99, "bananas": 1.49} # Sorting the dictionary keys alphabetically sorted_prices = sorted(fruit_prices) print(sorted_prices)
zip()
: combines two or more iterables (e.g., lists) into a single iterable of tuples, pairing elements from each iterable together.
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)
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
.
- Convert the price to a
- Use
sum()
to calculate the total sum of all sales. - Assign the total sum to the
total_sum
variable. - Use
min()
andmax()
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>
Ratkaisu
Kiitos palautteestasi!
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;
checkout = [2.99, 5.49, 3.99] total = sum(checkout) print(total)
max()
andmin()
: return the largest and smallest elements in an iterable, respectively — ideal for comparisons or finding extremes;
freezer_temperatures = [38, 32, 41, 34, 40] print(max(freezer_temperatures)) print(min(freezer_temperatures))
float()
: converts a number or a string representing a number into a floating-point number (a number with decimals);
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)}")
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;
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)}")
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 thesort()
method,sorted()
does not modify the original data and works on a wider range of types;
fruit_prices = {"cherries": 3.99, "apples": 2.99, "bananas": 1.49} # Sorting the dictionary keys alphabetically sorted_prices = sorted(fruit_prices) print(sorted_prices)
zip()
: combines two or more iterables (e.g., lists) into a single iterable of tuples, pairing elements from each iterable together.
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)
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
.
- Convert the price to a
- Use
sum()
to calculate the total sum of all sales. - Assign the total sum to the
total_sum
variable. - Use
min()
andmax()
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>
Ratkaisu
Kiitos palautteestasi!