madamasterclass.com

📔 Introduction to Functions

Introduction to the concept of functions in python


Functions in Python allow you to organize your code into reusable blocks. This not only improves code readability but also facilitates maintenance and testing. In this section, we will cover how to define, call, and use functions in Python.

1. Defining a Function

To define a function in Python, use the def keyword followed by the function name and parentheses. Here's a basic example:

⌨️  
def greet():
    print("Hello from a function!")
    
2. Calling a Function

Once defined, you can call a function by using its name followed by parentheses:

⌨️  
greet()
    
3. Functions with Parameters

You can pass information into functions using parameters:

⌨️  
def greet(name):
    print("Hello, " + name + "!")

greet("Alice")
    
4. Activity: Word Search

Let's play a word search! Find the following keywords related to Python functions: REUSABILITY, ORGANIZATION, ABSTRACTION, TESTING, DEBUGGING, FUNCTIONS, MODULARITY.

Word Search
SCORE: 0
TIME: 0s
5. Conclusion

Now that you’ve learned how to define and use functions in Python, try writing your own to perform simple tasks like greeting users, calculating values, or organizing your code into clear sections. Keep practicing, and use the power of functions to make your code efficient and readable!


In this final activity, you will review everything you’ve learned in Python by building a mind map! You will use ChatGPT to generate a structured summary in Markdown format, and then transform it into a visual map using an online tool.

🎯 Goal

Create a mind map that summarizes the core concepts of Python you’ve learned so far: variables, data types, conditionals, loops, functions, modules, and more.

📝 Step 1: Ask ChatGPT to Generate Markdown

Go to chat.openai.com and paste this prompt:

Can you create a mind map structure in Markdown that summarizes the main topics of beginner Python programming? 
Please include key areas like: Variables, Data Types, Conditions, Loops, Functions, Modules, Lists, Dictionaries, and File Handling.

ChatGPT will generate something like:

🧠
            # Python Programming

            ## Variables
                    ● Assignment
                    ● Naming rules

            ## Data Types
                    ● int, float, str
                    ● type(), casting

            ## Conditions
                    ● if, elif, else
                    ● comparison operators

            ## Loops
                    ● for, while
                    ● break, continue

            ## Functions
                    ● def, parameters
                    ● return, scope

            ## Lists & Dictionaries
                    ● Append, indexing
                    ● Keys, values

            ## Modules
                    ● import
                    ● built-in vs custom

            ## File Handling
                    open(), read(), write()
    
🧭 Step 2: Visualize It as a Mind Map

Go to markmap.js.org/repl, paste the Markdown content you got from ChatGPT, and see your mind map come to life!

📤 Step 3: Export & Submit

Once your map is ready:

  •                 1️⃣ Click the Export button
  •                 2️⃣ Save it as an image or PDF
  •                 3️⃣ Submit it to your instructor or upload it to the course platform

💡 Bonus Tip

Use your own words in the mind map! You can edit the Markdown before pasting it into Markmap or even build it manually. This helps you better remember each concept.

Exercise 1: ★ ☆ ☆ ☆ ☆

Define a simple function in Python named greet that prints "Hello, world!" when called.

1. Write the function definition.
2. Call the function once.

def greet(): print("Hello, world!")

greet()


Exercise 2: ★ ★ ☆ ☆ ☆

Write a Python function named square that takes a number as input and returns its square.

1. Define the function.
2. Use the function to find the square of 5 and 10.

def square(x): return x * x

print(square(5)) # 25 print(square(10)) # 100


Exercise 3: ★ ★ ★ ☆ ☆

Create a function named is_even that returns True if a number is even, and False otherwise.

1. Define the function.
2. Use it to test the numbers 4 and 7.

def is_even(n): return n % 2 == 0

print(is_even(4)) # True print(is_even(7)) # False


Exercise 4: ★ ★ ★ ★ ☆

Write a function named fahrenheit_to_celsius that converts a temperature from Fahrenheit to Celsius using the formula:
C = (F - 32) * 5/9

1. Define the function.
2. Convert 98.6°F to Celsius.

def fahrenheit_to_celsius(f): return (f - 32) * 5 / 9

print(fahrenheit_to_celsius(98.6)) # 37.0


Exercise 5: ★ ★ ★ ★ ★

Create a function calculate_average that takes a list of numbers and returns the average.

1. Define the function.
2. Test it with the list [10, 20, 30, 40].

def calculate_average(numbers): return sum(numbers) / len(numbers)

print(calculate_average([10, 20, 30, 40])) # 25.0


Forum(s) associé(s)

L'Art de la Philosophie : Interprétations et Débats

08 Apr, 2016

Explorez comment l'art et la philosophie s'entrelacent pour questionner notre perception de la réalité et de l'esthétique.

Read more.

Voyage à Travers la Liberté : Concepts et Dilemmes

27 Jan, 2014

Plongez dans les débats philosophiques sur la liberté, ses implications éthiques et les défis contemporains qui l'entourent.

Read more.

La Quête de la Vérité : Philosophies et Perspectives

30 Feb, 2015

Découvrez les différentes approches philosophiques de la vérité et comment elles influencent notre compréhension du monde.

Read more.
Page: