Length of a List: To find the length of a list, use the LEN
function.
Example: LEN([1, 2, 3, 4, 5])
returns 5
.
Concatenation of Lists: Use the +
sign to concatenate two lists.
Results in a larger combined list.
Minimum and Maximum Values:
Use the min()
function to find the smallest element in a list. All elements must be of the same type for this function to work.
Use the max()
function to find the largest element.
Homogeneous Lists: Python lists can contain different data types (e.g., integers, strings), but min()
and max()
require homogeneous elements.
Sum of List Items: sum(list)
returns the sum of numeric items in the list.
Index Method: list.index(value)
returns the index of the first occurrence of value
in the list.
Count Method: list.count(value)
gives the number of times value
appears in the list.
Initial List of House Prices: Start with a list of house prices.
Appending New Prices: Use list concatenation to add new prices.
Print Number of Prices: Use LEN
function to display how many prices are in the list.
Example Output: "There are 4 prices in the list."
Finding the Lowest Price: Use min()
to find the lowest price, e.g., min(house_prices)
returns the lowest price.
Example Output: "The most affordable house is for sale at $225,000."
Closing Costs: Define additional costs in a list and calculate total costs by summing house price and closing costs.
Example: total_cost = lowest_cost + sum(closing_costs)
.
Print Total Cost: Ensure to convert final result to an integer for clarity.
Lists of Scores: Contains midterm and final exam scores.
Concatenation: Combine midterm and final scores into a single list.
Counting Scores with LEN
: Use LEN
to count and print the number of scores for both midterm and final exams.
Calculation of Dropped Students: Subtract the number of final exam scores from midterm scores to find the number of students who dropped.
Example output: "Number of students who dropped: X."
Finding Minimum and Maximum Final Scores:
Use min(final_scores)
to find the lowest score.
Use max(final_scores)
to find the highest score.
Range Printing: Use formatted strings to print the range of final scores.
Definition: A tuple is a sequence type resembling a list, but defined with parentheses.
Immutability: Once a tuple is defined, it cannot be modified (immutable).
Accessing Elements: Use indexing, e.g., tuple[index]
.
Examples: Define a tuple for coordinates, e.g., coordinates = (lat, long)
.
Definition: A named tuple is a subclass of tuples with named fields.
Creation: Import from collections, define with attributes (e.g., Car: make, model, price).
Accessing Fields: Access attributes by specifying the tuple name followed by the attribute name.
Definition: An unordered collection of unique elements.
Creation: Sets can be created using curly braces or the set()
function.
Adding Elements: Use the add()
method, which ignores duplicates.
Removing Elements: The remove()
method deletes specified elements, raising an error if not found.
Empty Set: Could only be created using set()
.
Union: Combines sets, eliminating duplicates.
Example: set1.union(set2)
.
Intersection: Finds common elements between sets.
Example: set1.intersection(set3)
.
Difference: Removes elements from one set that exist in another.
Example: set1.difference(set4)
.
Definition: A collection of key-value pairs defined within curly braces.
Accessing Values: Use dict[key]
to access the value associated with a key.
Modifying Values: Update values by reassigning using dict[key] = new_value
.
Deleting Items: Use del dict[key]
to remove a key-value pair from the dictionary.
Numeric Types: Include integers and floats (floating-point numbers).
Sequence Types: Include strings, lists, tuples (ordered collections).
Mapping Type: Represented by dictionaries which associate keys with values.
Set Types: Include unordered and unique elements (sets).
Definition: Binary uses two digits (0 and 1) for representation.
Decimal to Binary Interpretation: Understand positional weight in binary, converting between the two numerical systems.
Example: A binary number has a weight based on its position, similar to decimal.
Example of Conversion: Convert 1101
binary to decimal by evaluating positional weights.