Learning Functions in Python

Introduction

 

Functions

Functions are away to organize code. They are used to divide a problem into a smaller ones that are easier to manage.

Functions take inputs and process or carry out actions on them. They could also produce an out.

 

 

Types of Function:

1. User defined function

2. Imported or external function: like 

from tkinter import *

3. Built in function: like print( ) , input( )

Example One:

def function2(x):

return 2*x

a=function2(3)

print(a)

 

Example Two:

def add(a,b):

added = a+b

return added

def minus(a,b):

diff= a-b

return diff

num1=float(input("Enter first number:"))

num2=float(input("Enter second number"))

total = add(num1,num2)

print(total)

subtract = minus(num1,num2)

print(subtract)

 

 

Task

Challenges:

1.Create a function that Accept two int values from the user and return their product. If the total is greater than 1000, then return their sum.

2.Design a program with functions to calculate the area of a rectangle.

3.Design a program with functions to calculate the perimeter  of a rectangle.

4. Create a calculator using functions: Follow the video to create a calculator.


Debrief 

Did you enjoy creating the calculator? What are challenges you were facing while creating this form?What other forms you would like to create?

https://classroom.google.com/u/1/c/Mzc2ODQ2Njk1MDFa/m/NDM3MzcxNzM5NjZa/details