- Course Overview
- Tool Setup
- Online Research Tips
- Further Reading and Helpful Links
- See the great work by Matt Watkins collecting examples. Sorry example links only accessible to students of the DU AI Courses.
- Resources
- Cheatsheets & Tips
- Books
- Articles
- Subscriptions
- Career Engagement
- Data Sources
import os
# Clearing the Screen
os.system('cls')
if type(mydict) is dict:
if type(mylist) is list:
if type(myint) is int:
if type(myfloat) is float:
if type(mystring) is string:
Add this for code your intend to run directly from the command line.
if __name__ == "__main__"
See more here: What Does if name == "main" Do in Python?
The map() function executes a specified function for each item in an iterable. The item is sent to the function as a parameter.
Syntax
map(function, iterables)
Example
def myfunc(a, b):
return a + b
x = map(myfunc, ('apple', 'banana', 'cherry'), ('orange', 'lemon', 'pineapple'))
print(x)
#convert the map into a list, for readability:
print(list(x))
returns:
<map object at 0x034244F0>
['appleorange', 'bananalemon', 'cherrypineapple']
Parameter | Description |
---|---|
function | Required. The function to execute for each item |
iterable | Required. A sequence, collection or an iterator object. You can send as many iterables as you like, just make sure the function has one parameter for each iterable. |
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
Syntax
lambda arguments : expression
Example 1
x = lambda a, b : a * b
print(x(5, 6))
Example 2
x = lambda a, b : a * b
print(x(5, 6))
Example 3
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
The filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or not.
Syntax
filter(function, iterable)
Parameter Values
Parameter | Description |
---|---|
function | A Function to be run for each item in the iterable |
iterable | The iterable to be filtered |
Example
ages = [5, 12, 17, 18, 24, 32]
def myFunc(x):
if x < 18:
return False
else:
return True
adults = filter(myFunc, ages)
for x in adults:
print(x)
returns:
18
24
32
See the Docstring tutorial