python defining a function in simple English

Опубликовано: 27 September 2026
на канале: Dr Codie
1,829
30

Python defining a function is a tutorial that simply explains functions in python Define functions, call functions, pass and return are all explained in simple English.
functions examples in Python 3) what you need to know
define a function
music:
Happy Life by FREDJI   / fredjimusic     / fredjimusic   Music promoted by Audio Library    • Happy Life – FREDJI (No Copyright Music)  

examples
examples
1. call a function
printname()
printnumbersquared()
#can call many times
printname()
printname()
lesson 1 call a function
printname()
Output: my name is John, hi there
printnumbersquared()
Output: the square of 1 is 1
the square of 2 is 4
the square of 3 is 9
the square of 4 is 16
lesson 2 pass to a function
def printnumbersquared():
mynum = 4
sqnum = mynum*mynum
print('the square of', mynum, 'is', sqnum)
printnumbersquared()
Output: the square of 4 is 16
lesson 2 pass to a function
def printnumbersquared(mynum):

sqnum = mynum*mynum
print('the square of', mynum, 'is', sqnum)
printnumbersquared(4)
Output: the square of 4 is 16
range
for i in range(5):
print(i)
Output: 0
1
2
3
4
using a range
def printnumbersquared(mynum):
sqnum = mynum*mynum
print('the square of', mynum, 'is', sqnum)
for n in range(5):
printnumbersquared(n)
output
Output: the square of 0 is 0
the square of 1 is 1
the square of 2 is 4
the square of 3 is 9
the square of 4 is 16
lesson 3 return from a function
def getanumber():
x = 1
return x
y = getanumber()

lesson 3 return from a function
def printnumbersquared(mynum):
sqnum = mynum*mynum
print('the square of', mynum, 'is', sqnum)

for n in range(5):
printnumbersquared(n)

output
Output: the square of 0 is 0
the square of 1 is 1
the square of 2 is 4
the square of 3 is 9
the square of 4 is 16
lesson 3 return from a function
def printnumbersquared(mynum):
sqnum = mynum*mynum
return sqnum

for n in range(5):
sq = printnumbersquared(n)
print('the square of', n, 'is', sq)
output
Output: the square of 0 is 0
the square of 1 is 1
the square of 2 is 4
the square of 3 is 9
the square of 4 is 16
finally – multiple pass & return
def doublenumbers(x,y):
a = x*2
b = y*2
return a,b
num1 = 5
num2 = 8
num1,num2 = doublenumbers(num1,num2)
print (num1,num2)
output
Output: 10,16
Quiz
question 1
write a function that prints a list
e.g. alist = [2,4,8,1,3,9]
#you could pass it a list or just call the function that prints the list
question 2
write a function that takes a number as a string and returns the number as an integer
e.g. num = ‘7’
#after the function mynum = 7
question 3
write a function that can add two numbers or add two strings.
Call the function twice (pass two numbers first, pass two strings second)
Return and print the answers
question 4
write a function that doubles an integer
return and print the new value
call the function using a range of numbers from 0 to 10.
question 5
write a function that takes a name and splits the name into words (use text.split())
Return the resulting list and print the first word
e.g. pass “John Smith” prints John
e.g. pass “John Mark Smith” prints John
e.g. pass “Johnny2go” prints Johnny2go
Pause the video

come back to
check your answers
Answers
question 1
write a function that prints a list
def printlist(alist):
print (alist)
alist = [2,4,8,1,3,9]
printlist(alist)
Output: [2,4,8,1,3,9]
question 2
write a function that takes a number as a string and returns the number as an integer
def converttonum(astring):
num = int(astring)
return num
astring = "7"
anum = converttonum(astring)
print(anum) Output: 7
question 3
function that adds two numbers or adds two strings.
def addtwo(a,b):
c = a + b
return c
x = 1
y = 2
z = addtwo(x,y)
print(z) Output: 3
question 3
function that adds two numbers or adds two strings.
def addtwo(a,b):
c = a + b
return c
i = "hello "
j = "world"
k = addtwo(i,j)
print(k) Output: hello world
question 4
write a function that doubles an integer using a range of numbers from 0 to 10
def printdouble(mynum):
dblnum = mynum + mynum
return dblnum

for n in range(10):
dbl = printdouble(n)
print('the double of', n, 'is', dbl)
output
the double of 0 is 0
the double of 1 is 2
the double of 2 is 4
the double of 3 is 6
the double of 4 is 8

question 5
write a function that takes a name and splits the name into words (use text.split()) & print the first word
def firstname(text):
words = text.split()
return words[0]
name = "John Smith"
fname = firstname(name)
print('the first name is', fname)
output
Output: the first name is John