Intermediate Python for Developers
George Boorman
Curriculum Manager, DataCamp
# Printing
print("Display this as an output")
'Display this as an output'
# Checking data types
type(print)
builtin_function_or_method
# Looping through a range of numbers
for num in range(1, 5):
print(num)
1
2
3
4
sales = [125.97, 84.32, 99.78, 154.21, 78.50, 83.67, 111.13]
# Find the largest sale max(sales)
154.21
# Find the smallest sale
min(sales)
78.5
sum(sales)
737.5799999999999
# Store total sales total_sales = sum(sales)
# Round to two decimal places round(total_sales, 2)
737.58
# Store total sales
total_sales = sum(sales)
# Round to two decimal places
round(total_sales, 2)
737.58
# Store total sales total_sales = round(sum(sales), 2)
# Round to two decimal places print(total_sales)
737.58
# Count the number of sales
len(sales)
7
# Calculate average sales
sum(sales) / len(sales)
105.36857142857141
# Length of a string
len("Introduction to Programming for Developers")
42
# Length of dictionary
len({"a": 1, "b": 2, "c": 3})
3
# Sort the sales list in ascending order
sorted(sales)
[78.5, 83.67, 84.32, 99.78, 111.13,
125.97, 154.21]
# Sort a string alphabetically
sorted("George")
['G', 'e', 'e', 'g', 'o', 'r']
# Get information about the sorted() function
help(sorted)
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
int
, str
, {}
, []
, list
, etc.# Find total sales
sum(sales)
737.5799999999999
# Find total sales # Create a variable to increment sales_count = 0
# Loop through sales for sale in sales:
# Increment sales_count by each sale sales_count += sale
print(sales_count)
sum()
is reusable, shorter, cleaner,
and less prone to errors!125.97
210.29
310.07
464.28
542.78
626.4499999999999
737.5799999999999
Function | Returns |
---|---|
print() |
Display an output, e.g., variable's values |
max() |
Find the largest value in a data structure |
min() |
Find the smallest value in a data structure |
sum() |
Add up all elements in a data structure |
round() |
Trim a float to a specified number of decimal places |
len() |
Count the number of elements in a data structure |
sorted() |
Sort elements in a data structure in ascending order |
help() |
Get information about a function, variable, or value |
Intermediate Python for Developers