Language

Functions · Lesson 13 of 56

Examplesfunctions

Source: 4-Functions/4.2-examplesfunctions.ipynb

Start here — no coding background needed

What you will learn

Practice small useful functions — area, discount, grade.

In simple words

Functions keep logic tidy. Change inputs, same recipe runs again.

Write a recipe once, use it many times — functions save repetition.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Functions Examples

Example 1: Temperature Conversion

Reference example
Python
Output
Expected (from notebook):
77.0
25.0

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Example 2: Password Strength Checker
Reference example
Python
Output
Expected (from notebook):
False
True

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Example 3: Calculate the Total Cost Of Items In a Shopping Cart
Reference example
Python
Output
Expected (from notebook):
5.8999999999999995

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Example 4: Check IF a String Is Palindrome
Reference example
Python
Output
Expected (from notebook):
True
False

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Example 5: Calculate the factorials of a number using recursion
Reference example
Python
Output
Expected (from notebook):
720

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Example 6: A Function To Read A File and count the frequency of each word
Example HCL
HCL
def count_word_frequency(file_path):
    word_count={}
    with open(file_path,'r') as file:
        for line in file:
            words=line.split()
            for word in words:
                word=word.lower().strip('.,!?;:"\'')
                word_count[word]=word_count.get(word,0)+1
    
    return word_count

filepath='sample.txt'
word_frequency=count_word_frequency(filepath)
print(word_frequency)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example 7: Validate Email Address
Reference example
Python
Output
Expected (from notebook):
True
False

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

You learned "Examplesfunctions". Use print() to show: Done: Examplesfunctions

Hint: Use one print() with the exact text.

Python