Being-Creative-with-AI/C3/Python-Functions-in-Google-Colab/English
| Visual Cue | Narration |
| Title Slide | Welcome to this Spoken tutorial on Python Functions in Google Colab. |
| Learning Objectives Slide | In this tutorial, you will learn to:
• Define a new function using def. • Call the function by its name. • Use parameters and return values. • Handle minor coding errors. • Improve function using a default parameter. |
| Disclaimer slide | As Al tools constantly evolve, if you are unable to locate any icon or encounter difficulty at any step, you may use any conversational Al Chatbot for guidance. |
| System Requirements Slide | I am using the following setup:
• A computer with Internet connection. • A Web browser such as Chrome, Firefox, or Edge. • An active Google account to access Google Colab. |
| Prerequisite Slide
For the Pre-requisites of this tutorial, visit the website shown on your screen |
Learners should be familiar with Basic knowledge of Python syntax.
For the Pre-requisites of this tutorial, visit the website shown on your screen. |
| Show the code file
The following code file is required to practice this tutorial. 1. Creating-a-Greeting.txt This file is provided in the code files link of this tutorial page.
|
The following code file is required to practice this tutorial.
This file is provided in the Code Files link of this tutorial page. Please download and extract the file. |
| Show slide: What is a function? | Let’s now understand what a function is.
A function is reusable code for a specific action. We define it once and use it many times. |
| Show Google Colab homepage | We will use Google Colab, a cloud-based editor in this tutorial. |
| Show browser address bar with colab.research.google.com | Open any web browser and Log into your Google account.
Now in the address bar, type colab dot research dot google dot com press enter. |
| Show Google Colab welcome page | Welcome to colab webpage opens with an Open notebook window. |
| Highlight “New Notebook” button | To begin, we will create a new notebook.
So go to the bottom left of this window and click on the tab plus New notebook. |
| Show new Colab notebook opening | A new webpage opens.
The file name appears on the top left corner of the webpage. By default it is named as this. Let us rename this as shown on the screen. This file is automatically saved to your Google drive. |
| Blank code cell shown | Now, we are ready to write a Python function. |
| Show code cell (empty) | Let us write our first function now.
We will create a simple function to generate a personalized greeting. |
| def create_greeting(name):
|
In the coding bar, type: def create underscore greeting open parenthesis name close parenthesis colon
Press Enter. Press Tab key for indentation and type the return statement as shown here. |
| Mouse hover on the play button | Press control and Enter to run the cell.
You can also click on the play button on the left of the coding bar to run the cell. |
| New cell below | The function is ready to use. |
| Highlight the bold words as narrated
def create_greeting(name):
|
The keyword def tells Python to create a function.
create under score greeting is the function name. Every time you start a new function, you’ll use def followed by the function’s name. Name is the input, also called a parameter. Return sends a value back to the caller. The f-string inserts the name into the sentence. |
| Show on screen + Code
print(create_greeting('Alex'))
|
Now, press plus Code
A new cell will open. You can call the function by typing the command shown on the screen. Press Shift and Enter. |
| Show output | Here is the output: Welcome, Alex! |
| print(create_greeting('Asha'))
print(create_greeting('Rahul')) |
Pause the tutorial here. Try other names like 'Asha' or 'Rahul'.
Observe the output and resume the tutorial. |
| Show the outputs | The function works for any name we provide. |
| Show slide: Store the result | Now, let us see how we can store the result in a variable. |
| Show code block
greet = create_greeting('Sam') |
Open a new cell by clicking on plus Code.
If you already have a new cell, you may ignore this step. Now pause the video and type the command as shown on the screen. Press Shift and Enter. |
| Show code block
print(greet) |
Next in a new cell type this.
press Shift and Enter. The output is: Welcome Sam! |
| Show slide: Common mistake | Let us look at a common mistake we might make while using functions. |
| print(create_greeting(Alex)) | Let me type the following code and press the play button to run it.
Do you notice the error? |
| print(create_greeting(Alex))
print(create_greeting('Alex')) |
Can you tell what could be the possible reason?
Let me add our previous command next to it. Now can you tell what was the mistake. |
| Show fix
print(create_greeting('Alex')). |
Yes, always remember to use quotes around the value you want to print.
|
| print(create_greeting(‘Alex’)) | Another common mistake is when we forget to run the cell with def first.
If we do not run the cell, python will not know that the function exists. |
| print(create_greeting(‘Alex’)) | Now, when we run print open parenthesis create underscore greeting open parenthesis single quote Alex close quote close parenthesis and again close parenthesis
a NameError occurs. |
| Show slide: Optional improvement | To make our function better, let us add a default value to the parameter.
This allows the function to work without a name. |
| Show code block
def create_greeting(name='Friend'):
|
Let us understand this with the code that we used earlier in this tutorial
We start with def, followed by the function name. Inside the parenthesis, we see; name equals 'Friend'. Name is our parameter or input variable. 'Friend' is the default value. |
| Show code block
def create_greeting(name='Friend'):
|
The next line starts with the return keyword.
This sends the formatted string back to the user. |
| Show code block
def create_greeting(name='Friend'):
|
Why do we use a default value?
It acts as a safety net for our code. |
| print(create_greeting(‘Alex’)) | If we provide a name like 'Alex', Python ignores the default and uses 'Alex'. |
| print(create_greeting())
Output: Welcome, Friend! |
Now, let us call the function with empty brackets.
Type the command as shown and run the cell. Python now uses the default value 'Friend'. Default values prevent errors when input is missing. |
| Summary slides
Summary of what we have learnt: • Define a new function using def • Call the function by its name • Use parameters and return values • Handle small coding errors • Improve your function using a default parameter |
This brings us to the end of this tutorial.
Here is a summary of what we learnt. • Define a new function using def • Call the function by its name • Use parameters and return values • Handle small coding errors • Improve your function using a default parameter |
| Assignment slide
1) Create a function make_tagline(product) 2) It should return 'The best choice: {product_name}'. 3) Call it with three products of your choice. 4) Print each tagline on a new line. |
Here is an assignment |
| Acknowledgement | Thank you |
| Closing slide
This Spoken tutorial is brought to you by EduPyramids Educational Services Private Limited, SINE, IIT Bombay. |