Python Data Science Toolbox (Part 1)
Notes taken by Kyesswa Steven, obviously...
Notes By Kyesswa Steven
Started on 10/18/2023
Here i go with taking note...
Functions, Oh Yes, you heard right... FUNCTIONS-IN-PYTHON
Code snippets, notes and stuff... Below
# Code by Kyesswa Steven
#
# 10/18/2023
#
def wakanda(Flooded) :
""" Returns output of strings when argument is passed """
wakanda_status = "Wakanda Is Now Flooded By Namor"
namors_promise = Flooded
print( namors_promise )
return wakanda_status
namor = "Namor says, 'Am not Mr.nimbus but i shall make wakanda wet, wet i tell you!' "
wakanda(namor)
Now with turples
Instructions
Unpack nums to the variables num1, num2, and num3. Construct a new tuple, even_nums composed of the same elements in nums, but with the 1st element replaced with the value, 2.
# Code by Kyesswa Steven
#
# 10/23/2023
#
# Assuming nums is a tuple
nums = (3, 4, 6)
# Unpack nums into num1, num2, and num3
num1, num2, num3 = nums
# Construct even_nums
even_nums = (2,) + nums[1:]
print(even_nums)
Nested Functions with Closure(s)
Yes these ones really love closure.
Nested Functions II (From my DATACAMP execises) With nesting functions, we get the idea of a closure. This means that the nested or inner function remembers the state of its enclosing scope when called. Thus, anything defined locally in the enclosing scope is available to the inner function even when the outer function has finished execution.
Let's move forward then! In this exercise, you will complete the definition of the inner function inner_echo() and then call echo() a couple of times, each with a different argument. Lets Complete the exercise and see what the output shall be!
# Code by Kyesswa Steven
#
# 10/23/2023
#
# Define echo
def echo(n_word):
"""Return the inner_echo function."""
# Define inner_echo
def inner_echo(word1):
"""Concatenate n copies of word1."""
echo_word = word1 * n_word
return echo_word
# Return inner_echo
return inner_echo
# Call echo: twice
twice = echo(2)
# Call echo: thrice
thrice = echo(3)
# Call echo: fourth
fourth = echo(4)
# Call twice() and thrice() then print
print( twice('Napa '), thrice('Napa-Valley '), fourth('Nesto is the biggest N-word by far ') )
Introducing Default Arguments(*args) and **Kwargs
# Code by Kyesswa Steven
#
# 10/23/2023
#
def echo_word(word1, echo):
"""Concatenate echo copies of word1."""
echo_word = word1 * echo
return echo_word
# Call echo_word() with arguments 'hello' and 3
echo_word('Am Mr.Nimbus, I control the police ', 3)
Lambda Function
Map() and lambda functions
lambda functions: Lambda function in Python is a small, anonymous function that is defined using the lambda keyword. Lambda functions are also known as "anonymous functions" because they don't have a name like a regular function. Instead, they are typically used for simple operations and are often defined inline.
Syntax is:
lambda arguments: expression
Lambda functions are useful when you need a small, throwaway function for a specific purpose, often in combination with functions like map, filter, and reduce.
map() Function: The map() function in Python is used to apply a given function to all the items in an input list (or other iterable) and returns a map object (an iterator) that contains the results.
Syntax is:
map(function, iterable, ...)
function
: This is the function that you want to apply to each item in the iterable.
iterable
: This is the iterable (e.g., a list, tuple, or other collection) on which the function will be applied.
The map() function essentially allows you to transform data by applying a specific function to each item in a collection without the need for explicit loops. It is often used in combination with lambda functions when you need to perform a simple operation on each element of an iterable. The result can be converted into a list or another data structure if desired.
Thats All I Have On The map()
and lambda
functions for now.
# Code by Kyesswa Steven
#
# 10/25/2023
#
# Create a list of strings: spells
weird_spells = ["protego", "accio", "expecto patronum", "legilimens", "Mr.Nimbus",
"Abraca-Chill-Bro", "Braca-Lays", "Abraca-Doritos", "Abraca-Fried-Chicken"]
# Use map() to apply a lambda function over spells: shout_spells
shout_spells = map( lambda item: item + '!!!', weird_spells )
# Convert shout_spells to a list: shout_spells_list
shout_spells_list = shout_spells = list(map(lambda item: item + '!!!', weird_spells))
# Print the result
print(shout_spells_list)
The filter() Function:
The filter()
function is used to filter a sequence (e.g., a list, tuple, or other iterable) by applying a specified function to each element in the sequence and returning a new iterable that contains only the elements for which the function returns True.
Syntax is:
filter(function, iterable)
function
: This is the function that is applied to each element in the iterable. It should return True or False. The function can be a regular function or a lambda function.
iterable
: This is the iterable that you want to filter.
Here's how filter() works:
The function is applied to each element in the iterable
.
If the function returns True
for a particular element, that element is included in the result.
If the function returns False
, the element is excluded from the result.
The filter()
function returns an iterator. To see the filtered elements, you can convert the result to a list, for example:
filtered_result = filter(function, iterable)
filtered_list = list(filtered_result)
Sample Example Code Below: