Hello Everyone,
I will continue ‘Introduction to Basic Python 2’ in this article.
For Loops
A for loop is used for iterating over a sequence, a list, a tuple, a dictionary, a set, or a string.
“i” is a variable name, and we can write “x” instead of “i”. “i” variable will get in “a” list step by step, and values of “a” list will be squared.
“i” variable will get in the “salary_list” step by step, and if the salary is bigger than 4500, the salary will rise 10 percent. If salary is not bigger than 4500 then if salary is bigger than 2500, salary will rise 12 percent. If salary is not bigger than 2500 then salary will rise 14 percent. The results will add “new_salary_list”.
“new_salary_list” is [4480.0, 7700.0, 1140.0, 2508.0, 5500.0]
While Loops
With the while loop we can execute a set of statements as long as a condition is true.
We can do the same example of salary while loops.
We have to keep a counter. While loop needs a counter unlike ‘for loops’ for this example.
Lambda Function
A lambda function is a small anonymous function. Lambda functions behave in the same way as regular functions that are declared using the “def’ keyword.
The result of the example is 6.
Filter Function
Filter function creates a list of elements for which a function returns true. The filter resembles a for loop but it is a builtin function and faster.
Values in “numb” list are checked with lambda function, and if the result is true, the value is added to the filter list. We checked values whether they are even numbers. The result is [12, 14, 54].
Map Function
Map function applies a given function to each item of an iterable and returns a list of the results.
In this example, the length of values show as the list. The result is [6, 4, 1].
Thanks to your time, Have a nice day.