The map() is the higher-order function in Python, meaning it takes a function as an argument and applies it to an iterable (e.g., a list, tuple, etc.).
The map() function in Python is used to apply a function to all elements in an iterable. It transforms the elements based on the logic provided in the function and produces a map object, which is an iterator.
Note: This map object can be transformed into a list or another kind of iterable.
Following is the syntax of the Python map() function:
map(function, iterable)
Following are the parameters:
This function returns an iterator containing the transformed elements.
Below is the basic example of the map() function:
nums = [1, 2, 3, 4]
# Square each number
squared_nums = list(map(lambda x: x ** 2, nums))
print(squared_nums)
Output
In this example, you have a list of dictionaries containing information about employees. You want to create a new list where each employee's salary is increased by 10%, and their name is capitalized.
employees = [
{"name": "akash", "salary": 50000},
{"name": "rahul", "salary": 60000},
{"name": "aman", "salary": 70000},
]
# Use map to transform the employee data
updated_employees = list(map(lambda emp: {
"name": emp["name"].capitalize(),
# Increase salary by 10%
"salary": round(emp["salary"] * 1.1, 2)
}, employees))
print(updated_employees)
Output
The map() is the higher-order function in Python, meaning it takes a function as an argument and applies it to an iterable (e.g., a list, tuple, etc.).
The map() function in Python is used to apply a function to all elements in an iterable. It transforms the elements based on the logic provided in the function and produces a map object, which is an iterator.
Note: This map object can be transformed into a list or another kind of iterable.
Following is the syntax of the Python map() function:
pythonmap(function, iterable)
Following are the parameters:
This function returns an iterator containing the transformed elements.
Below is the basic example of the map() function:
pythonnums = [1, 2, 3, 4] # Square each number squared_nums = list(map(lambda x: x ** 2, nums)) print(squared_nums)
Output
In this example, you have a list of dictionaries containing information about employees. You want to create a new list where each employee's salary is increased by 10%, and their name is capitalized.
pythonemployees = [ {"name": "akash", "salary": 50000}, {"name": "rahul", "salary": 60000}, {"name": "aman", "salary": 70000}, ] # Use map to transform the employee data updated_employees = list(map(lambda emp: { "name": emp["name"].capitalize(), # Increase salary by 10% "salary": round(emp["salary"] * 1.1, 2) }, employees)) print(updated_employees)
Output
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is ₹5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from ₹3,000 - ₹1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved