[go: up one dir, main page]

0% found this document useful (0 votes)
7 views3 pages

Python Functions Day1 Day2

The document provides an overview of Python functions, covering their definition, purpose, and syntax, along with examples and practice exercises. It also discusses the return statement and variable scope, distinguishing between local and global variables. Key concepts include the use of the 'global' keyword and the importance of functions for code organization and reusability.

Uploaded by

Sunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Python Functions Day1 Day2

The document provides an overview of Python functions, covering their definition, purpose, and syntax, along with examples and practice exercises. It also discusses the return statement and variable scope, distinguishing between local and global variables. Key concepts include the use of the 'global' keyword and the importance of functions for code organization and reusability.

Uploaded by

Sunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Functions - Day 1 & 2 Notes

Day 1: Introduction to Functions

What is a Function?

A function is a reusable block of code that performs a specific task.

Why Use Functions?

- Avoid repetition

- Organize and modularize code

- Easier debugging

- Better readability and reusability

Syntax:

def function_name():

# block of code

Example:

def greet():

print("Hello!")

greet()

Analogy:

Think of a function as a coffee machine. You press a button (call function), and it gives output

(coffee).

Page 1
Python Functions - Day 1 & 2 Notes

Practice Zone:

1. hello_world() - prints "Hello, World!"

2. add(a, b) - returns sum

3. greet(name) - prints greeting

4. square_numbers() - prints square of 1 to 5

Day 2: Return and Variable Scope

Return Statement

Used to send result back to where the function was called.

Example:

def add(a, b):

return a + b

result = add(2, 3)

Without return result is None

With return result = output of function

Variable Scope:

- Local: Inside function only

- Global: Outside and accessible everywhere

Use 'global' keyword to modify global variables inside function.

Page 2
Python Functions - Day 1 & 2 Notes

Example:

x = 10

def change():

global x

x = 20

Practice Tasks:

1. area_of_circle(radius)

2. convert_to_uppercase(text)

3. max_of_three(a, b, c)

4. is_even(n) + print_even_numbers(start, end)

Summary Table:

- return: sends output

- local vs global scope

- global keyword usage

Page 3

You might also like