My github repository for the functions I learned as part of the course. Here's what this notebook covers:
Introduction to functions
So, let's talk about functions. They're like these cool little building blocks in programming that do specific things. You know how in math, functions take in stuff and give out other stuff? Well, programming functions are kind of like that. You give them some inputs, they do their thing, and then they give you some outputs.
Functions are awesome because they make your code neater and easier to use. Think about it: if you've got something you need to do over and over again in your program, instead of copying and pasting the same code all over the place, you can just write a function once and call it whenever you need to do that thing. It's like having a shortcut! This way, you don't have to keep writing the same stuff, and your code stays nice and tidy.
Benefits of using functions
Modularity: Functions break down complex tasks into manageable components.
Reusability: Functions can be used multiple times without rewriting code.
Readability: Functions with meaningful names enhance code understanding.
Debugging: Isolating functions eases troubleshooting and issue fixing.
Abstraction: Functions simplify complex processes behind a user-friendly interface.
Collaboration: Team members can work on different functions concurrently
Maintenance: Changes made in a function automatically apply wherever it's used
Inputs(Parameters)
Inputs, also called parameters or arguments, are like bits of information that you give to functions so they know what to do. It's like giving them instructions on how to perform their tasks.
Tasks
The purpose of a function determines the tasks it performs.
Producing outputs
The output is basically what the function gives back after doing its job. It's like the final product of all the work the function does. Imagine it as the result or answer you get from the function.
Python's built-in functions
Python has a rich set of built-in functions that provide a wide range of functionalities. These functions are readily available for you to use.
Built-in functions
Built-in functions are kind of like ready-made tools in Python that you can use right away. To use one, you just say its name and then add parentheses. If the function needs any extra information to do its job, you put that info inside the parentheses.
Once you've called the function, it does whatever it's supposed to do, and sometimes it gives you back a result that you can use in your code. It's like having shortcuts to common tasks.
Defining your functions
pass
A "pass" statement serves as a placeholder or a way to indicate no action. You use it when you want to define a function or a block of code but don't have the functionality ready yet. The "pass" statement keeps your code syntactically correct without actually doing anything.
Parameters
Parameters are like inputs for functions. They go inside parentheses when defining the function. Functions can have multiple parameters
Return statement
The "return" statement in a function is like the way a function gives you something back after it's done its job. It stops the function's work and sends out the result.
Scopes
Scope is all about where you can access stuff like variables, objects, and functions in your program.
Global Scope
Anything you define outside of a function or class is in the global scope. That means you can use it anywhere in your program, even inside functions and classes. You can make a global variable inside a function using the "global" keyword.
Local Scope
Anything you define inside a function or class stays local to that function or class. You can only use it there. If there's a variable with the same name both globally and locally, the local one wins out within its scope.
Using functions with loops
Functions and loops go hand in hand, especially for doing things over and over or working through collections of data. Functions help keep actions organized, while loops help repeat actions neatly.
for loop with function
With a for loop and a function together, you can apply the same actions or logic to lots of things in a sequence.
while loop with function
When you put a while loop and a function together, you can keep doing the same operations from the function over and over again until a certain condition isn't true anymore.
Conditional statements and functions with loop
Conditional statements, functions, and loops help beginners control program behavior, organize code, handle errors, and write efficient programs.
Modify data structures using functions
Modifying data structures involves creating functions to add, remove, or change items in lists, dictionaries, sets, etc.
Modifying Lists
For modifying lists, let's take an example function called "add_element
". It's designed to add an element to a list. This function needs two parameters:
data_structure
This is the list you want to add the element to. You can pass different types of data structures like lists, dictionaries, or sets to functions.
element
This is the item you want to add to the list.
Inside the function, you use the "append
" method to add the provided element to the data_structure, assuming it's a list. This way, you can modify lists easily using functions.
Function to remove an element
Using the else
statement, a string will be printed if the element is not found in the list.
Add elements to list Use the add_element
function to add three elements (10, 20, and 30)
to the my_list
.
Remove elements from the list Use the remove_element
function to remove elements from the my_list
.
Modifying Dictionaries
The add_to_dict
function needs three things: the dictionary (my_dict
) where you want to add a new key-value pair, the new key you want to add (key
), and the corresponding value for that key (value
).
The remove_from_dict
function requires two things: the dictionary (my_dict
) from which you want to remove a key, and the specific key (key
) you want to remove. If the condition is met, it uses the del
statement to remove the key-value pair from my_dict
. If the key isn't found, it prints a message saying so.
Modifying Sets
The add_to_set
function puts a new element into a set using the add
method. It needs two things: the set (my_set
) where you want to put the element, and the element itself (element
) that you want to add.
The remove_from_set
function takes an element out of the set if it's already in there. First, it checks if the element exists in the set using the in
operator. If it does, it removes the element using the remove
method. If the element isn't found, it prints a message saying that the element wasn't found in the set.
Source Disclosure:
In this blog post, I've been learning the IBM Data Science Professional Certificate on Coursera. The course has been super helpful in getting me up to speed with Python and basic functions. I shared some key concepts and ideas I've picked up along the way.
If you're interested in diving deeper, you can check out the course Python for Data Science, AI & Development | Coursera.