- Introduction to Map, Filter, and Reduce in Python
- List Comprehension
- Real-World Applications: Country Data Processing
- Comprehensive Guide to Python Error Types
- 1. SyntaxError
- 2. IndentationError
- 3. TypeError
- 4. NameError
- 5. IndexError
- 6. KeyError
- 7. ValueError
- 8. AttributeError
- 9. ZeroDivisionError
- 10. ImportError / ModuleNotFoundError
- 11. FileNotFoundError
- 12. RuntimeError
- 13. StopIteration
- 14. OverflowError
- 15. MemoryError
- 16. AssertionError
- Handling Errors with
try-exceptBlocks - Conclusion
- Exercises
- Exercises:
In Python, functional programming techniques such as map(), filter(), and reduce() allow us to process and transform collections of data efficiently. This reading material will guide you through the practical use of these functions with real-world examples.
The map() function applies a given function to all items in an input list (or any iterable). It returns a map object, which can be converted into a list.
Example 1: Squaring Numbers
nums = [1, 2, 3, 4, 5]
squares = list(map(lambda n: n ** 2, nums))
print(squares) # Output: [1, 4, 9, 16, 25]Example 2: Extracting Country Names
Here, we use map() to extract country names from a dataset.
country_names = list(map(lambda country: country['name'], countries_data))Challenge:
Using the same dataset, modify the map() function to extract country languages:
languages = list(map(lambda country: country['languages'], countries_data))To handle the list of lists, flatten the languages into one big list and convert it into a set for unique values:
all_languages = []
for language in languages:
all_languages.extend(language)
unique_languages = set(all_languages)
print(len(unique_languages)) # Count of unique languagesThe filter() function is used to filter out elements from a list (or any iterable) based on a condition. It returns an iterator, which can be converted into a list.
Example 1: Filtering Even Numbers
nums = [0, -5, 2, 4, 10, 3]
evens = list(filter(lambda n: n % 2 == 0, nums))
print(evens) # Output: [0, 2, 4, 10]Example 2: Filtering Countries Containing 'land'
countries_with_land = list(filter(lambda country: 'land' in country['name'], country_names))Example 3: Filtering European Countries
european_countries = list(filter(lambda country: country['region'] == 'Europe', countries_data_big))The reduce() function, from the functools module, reduces a list to a single value by applying a function cumulatively to the items in the list. Unlike map() and filter(), reduce() does not return an iterable but a single result.
Example: Product of All Numbers
from functools import reduce
nums = [1, 2, 3, 4, 5]
product = reduce(lambda acc, cur: acc * cur, nums)
print(product) # Output: 120Python's list comprehensions offer a concise way to create lists. It's often more readable and faster than using map() or filter().
Example 1: List Comprehension for Squaring Numbers
squares = [num ** 2 for num in nums]
print('squares:', squares) # Output: [1, 4, 9, 16, 25]Example 2: Flattening a List of Lists
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat_list = [n for sublist in lst for n in sublist]
print(flat_list) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]Let's say we have a dataset of countries with properties like name, capital, and population. Using the techniques discussed, we can process this data efficiently.
Example: Extracting Country Details
data = [{
'name': country['name'],
'capital': country['capital'],
'population': country['population']
} for country in countries_data]You can filter countries with 'land' in their name:
land_countries = [country['name'] for country in countries_data if 'land' in country['name']]The functions map(), filter(), and reduce(), along with list comprehensions, are powerful tools for transforming and analyzing data in Python. These techniques allow for clean, readable, and efficient code, especially when dealing with large datasets.
Errors and exceptions are integral parts of programming. They occur when Python encounters a problem during the execution of a program. Understanding these errors and knowing how to handle them can significantly improve your coding skills. This guide will cover all the common Python error types and how to deal with them effectively.
A SyntaxError arises when the code you write does not follow the proper syntax rules of Python. It usually happens when there are missing symbols (like parentheses or colons) or incorrect indentation.
Example 1:
# Missing closing parenthesis
print("Hello, world"Output:
SyntaxError: unexpected EOF while parsingHow to fix: Review the code to ensure that it follows proper Python syntax. Check for missing punctuation, mismatched parentheses, or incorrect indentation.
Example 2:
# Missing colon at the end of the function definition
def greet()
print("Hello, world!")Output:
SyntaxError: invalid syntaxHow to fix: Check your code for missing punctuation, such as colons, parentheses, or incorrect indentation.
Python relies on indentation to define the structure of code. An IndentationError occurs when the code is not properly indented.
Example:
def greet():
print("Hello, world!")Output:
IndentationError: expected an indented blockHow to fix: Ensure that blocks of code within functions, loops, and conditionals are indented properly. Use consistent spaces (usually four) for indentation.
A TypeError occurs when an operation or function is applied to an object of inappropriate type. For example, trying to perform arithmetic on a string and an integer will raise this error.
Example:
num = 5
print("The number is: " + num)Output:
TypeError: can only concatenate str (not "int") to strHow to fix: Ensure that operations are applied to compatible data types. In this case, convert the integer to a string before concatenating:
print("The number is: " + str(num))A NameError occurs when a variable or function name is used without being defined.
Example:
print(message)Output:
NameError: name 'message' is not definedHow to fix: Ensure that the variable or function is defined before using it. Check for typos or scope issues.
An IndexError occurs when you try to access an element in a list or sequence using an invalid index.
Example:
my_list = [1, 2, 3]
print(my_list[5])Output:
IndexError: list index out of rangeHow to fix: Always check the length of the list before accessing an index. Use len() to ensure the index is within range:
if len(my_list) > 5:
print(my_list[5])A KeyError occurs when trying to access a key that does not exist in a dictionary.
Example:
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict['gender'])Output:
KeyError: 'gender'How to fix: You can use the .get() method to avoid KeyError or check if the key exists before accessing it:
print(my_dict.get('gender', 'Key not found'))A ValueError occurs when a function receives an argument of the correct type but an inappropriate value.
Example:
number = int("ten")Output:
ValueError: invalid literal for int() with base 10: 'ten'How to fix: Ensure the input is of the correct format and valid value. In this case, the string should contain digits to be converted to an integer:
number = int("10")An AttributeError occurs when you try to access or call an attribute or method that does not exist on an object.
Example:
my_list = [1, 2, 3]
my_list.push(4)Output:
AttributeError: 'list' object has no attribute 'push'How to fix: Check the correct method or attribute for the object type. For lists, use append() instead of push():
my_list.append(4)A ZeroDivisionError occurs when you attempt to divide a number by zero, which is mathematically undefined.
Example:
result = 10 / 0Output:
ZeroDivisionError: division by zeroHow to fix: Ensure the denominator is not zero before performing division:
if denominator != 0:
result = numerator / denominator
else:
print("Cannot divide by zero!")An ImportError occurs when a module or specific function from a module cannot be imported. A ModuleNotFoundError is a subclass of ImportError and occurs when the Python interpreter cannot find the specified module.
Example:
import non_existent_moduleOutput:
ModuleNotFoundError: No module named 'non_existent_module'How to fix: Ensure the module is installed and the import statement is correct. You can install missing modules using pip:
pip install module_nameA FileNotFoundError occurs when trying to access or open a file that does not exist.
Example:
file = open('non_existent_file.txt', 'r')Output:
FileNotFoundError: [Errno 2] No such file or directory: 'non_existent_file.txt'How to fix: Ensure the file path is correct, and the file exists before trying to open it. You can handle this error with a try-except block:
try:
file = open('file.txt', 'r')
except FileNotFoundError:
print("File not found!")A RuntimeError is a generic error that occurs when something goes wrong during program execution but doesn't fit into any other specific error categories.
Example:
raise RuntimeError("An unexpected error occurred!")Output:
RuntimeError: An unexpected error occurred!How to fix: Investigate the code that triggers the error. Runtime errors often require debugging and analyzing the program flow.
A StopIteration error occurs when the next item of an iterator is requested, but there are no more items to return. This typically happens in loops or when manually handling iterators.
Example:
my_iterator = iter([1, 2, 3])
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator)) # No more items leftOutput:
StopIterationHow to fix: Handle iterators properly using a loop that checks when the iteration is finished, such as a for loop, which handles StopIteration automatically.
An OverflowError occurs when a numerical calculation exceeds the limits for a numeric type.
Example:
import math
result = math.exp(1000)Output:
OverflowError: math range errorHow to fix: Check your calculations to ensure they don't exceed the limits of the data type. For large numbers, use appropriate data types like decimal or handle the situation with approximate calculations.
A MemoryError occurs when the system runs out of memory to execute an operation, such as trying to create a massive data structure.
Example:
huge_list = [1] * (10 ** 10)Output:
MemoryErrorHow to fix: Optimize your code to use memory efficiently. Break down large tasks or use data structures with lower memory overhead.
An AssertionError occurs when an assertion statement, assert, fails. Assertions are used for debugging and testing.
Example:
x = 5
assert x > 10, "x should be greater than 10"Output:
AssertionError: x should be greater than 10How to fix: Ensure that the condition in the assert statement is true. Assertions are mainly used for internal consistency checks.
To avoid abrupt program termination due to errors, you can use try-except blocks to handle exceptions gracefully.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")By handling exceptions, you can ensure your program behaves predictably even when errors occur.
Python provides a wide range of error types to help programmers understand and resolve issues that arise during execution. Familiarity with these errors enables better debugging and the ability to write more reliable and resilient programs. Make use of try-except blocks
-
My students' ages range from 18 to 75. Each batch typically has 25 students, although the number can sometimes be less than or greater than 25. Create a function that generates student ages.
-
Descriptive statistics primarily focus on summarizing the central tendency, variability, and distribution of sample data. Central tendency refers to the measure that represents the typical value or characteristic of a sample or population. Common measures of central tendency include the mean, median, and mode.
Variability, on the other hand, indicates the extent of differences among the elements within a sample or population based on the measured characteristics. Key metrics of variability include range, variance, and standard deviation.
Create a
stats.pyfile and define functions for calculating mean, median, mode, range, variance, and standard deviation. Then, import thestats.pymodule into themain.pyfile. Find the mean, median, mode, range, variance, and standard deviation of the ages list.#Hint=> import random ages = [23, 45, 30, 32, 50, 19, 45, 33, 40, 30, 28, 26, 19, 22, 49, 35, 55, 21, 25, 31, 29, 58, 62, 41, 55]
-
Create a Python list of special characters using the standard
stringmodule.# Hint => import string special_characters = ['!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']
-
Generate a random string of 24 characters, consisting of a mix of lowercase letters, uppercase letters, and numbers. This string can be used for unique identification. Use the Python standard
string moduleto get digits and alphabets.Sample output:
UFGKoExs8bQVh0aLcTGYoRek
-
Create a function that generates a six-digit lottery number as a Python list, where each number is unique.
[15, 67, 32, 90, 48, 22]
-
Find the 10 most populated countries and 10 most spoken languages from the countries data.
-
Find the number of languages in the countries data.
-
Load the [cats] (https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/cats.json) by reading it. Use list comprehension and functional programming alternatively, and you can check your result from this website.
- Count the number of cat breeds
- Which country has the largest number of cat breeds
- Filter cat breeds highers than 5 Killograms
- What is the average weight of cat across all breeds based on this data?
- What is the average life span of cat across all breeds on this data?
- The data includes descriptions for each cat breed. Find the 10 most common words used in these descriptions.
- Which countries have one or more breeds in this dataset?
-
Data Visualization
- Create a word frequency table or line graph based on the cat breed descriptions.
- Create a word cloud of the cat breed descriptions.
- Create a bar graph showing the number of cat breeds by country of origin.
- Create a pie chart showing the percentage of cat breeds by country.
-
Fetch the cat breeds data from this API and answer Q1. Install
requestspackage usingpip install requests. Then, use this function to fetch the data:def fetch_data(url): import requests response = requests.get(url) if url.endswith('.txt'): return response.content else: data = response.json() return data