Python functions Flashcards
What is the use of a range function()?
The range() function returns a sequence of numbers.
By default, it starts from 0, increments by 1 and stops before the specified number.
For example :
numbers = list(range(10))
print(numbers)
output -
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
If range is called with one argument, it produces an object with values from 0 to that argument.
If it is called with two arguments, it produces values from the first to the second. Do you know how ?
numbers = list(range(3, 8))
print(numbers)
if len (numbers) ==5:
print(“Great”)
output -
[3,4,5,6,7]
Great
Do you know how the range() function can have 3 arguments (step)?
For Example :
numbers = list(range(5, 20, 2))
print(numbers)
x= list(range(1,30,3))
print(x)
u = list(range(40,5,-2))
print(u)
Output -
[5, 7, 9, 11, 13, 15, 17, 19]
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28]
[40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6]
Do you know how the range() function can have 3 arguments (step)?
For Example :
numbers = list(range(5, 20, 2))
print(numbers)
x= list(range(1,30,3))
print(x)
u = list(range(40,5,-2))
print(u)
Output -
[5, 7, 9, 11, 13, 15, 17, 19]
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28]
[40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6]
Can a FOR loop be used with range , without a list call?
YES .
for example -
for x in range (6):
print(x)
output -
0
1
2
3
4
5
How can a function be called ?
By using call function
for example -
def c():
print (“hey”)
c() # The function c() has been called , which will print the output “hey”
Can a defined function have arguments inside ?
YES
for example -
def p(x): #argument in the function
print(x + “ “ + “!!!”)
p(“Rishabh”) #When calling the function , x now becomes the string “Rishabh”
p(“is”)
p(“coding”)
output -
Rishabh !!!
is !!!
coding !!!
Can a defined function have arguments inside ?
YES
for example -
def p(x): #argument in the function
print(x + “ “ + “!!!”)
p(“Rishabh”) #When calling the function , x now becomes the string “Rishabh”
p(“is”)
p(“coding”)
output -
Rishabh !!!
is !!!
coding !!!
Can defined functions have multiple arguments inside ??
YES
For example -
def room(a,b):
print(a + b)
print(a*b)
room(2,5)
Output -
7
10
Does it matter which arguments you put when calling a function ?
NO
for example all the 3 below examples will give the same output -
[EXAMPLE 1]
password = input()
repeat = input()
def validate(password, repeat):
#your code goes here
if password == repeat:
print(“Correct”)
else:
print(“Wrong”)
validate(password,repeat)
[EXAMPLE 2]
password = input()
repeat = input()
def validate(x, y):
#your code goes here
if x==y:
print(“Correct”)
else:
print(“Wrong”)
validate(password,repeat)
[Example 3]
password = input()
repeat = input()
def validate(x, y):
#your code goes here
if password == repeat:
print(“Correct”)
else:
print(“Wrong”)
validate(password,repeat)
OUTPUT -
All are the same
Can a function argument be used outside a function ?
NO
Function arguments can be used as variables inside the function definition. However, they cannot be referenced outside of the function’s definition. This also applies to other variables created inside a function.
Example -
def function(variable):
variable += 1
print(variable)
function(7)
print(variable) # variable cannot be called outside the function since it was created inside fuction()
OUTPUT
This will throw an error because “variable” has been created inside the function but print(variable) is trying to call it
What is the difference between a PARAMETER and an ARGUMENT ?
Technically, parameters are the variables in a function definition, and arguments are the values put into parameters when functions are called.
example -
def function(variable): #parameter
variable += 1
print(variable)
function(7) #argument
What python functions do i know uptil now ?
print()
count()
max()
min()
list.append()
list.reverse()
list.index()
replace()
What is the use of a RETURN statement?
A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed.
for example 1] -
def pot (x,y):
if x > y:
return x , xy
else:
return y , yx
print(pot(5,9)) # pot() function is called and condition is not satisfied so it will return the value y , y*x
output -
(9, 45) # return y , y*x
Example 2]
def pot (x,y):
if x > y:
return x + y
else:
return x*y
print(pot(5,9)) # pot() function is called and condition is not satisfied so it will return the value y , y*x
output -
45 # return y , y*x
When a function is defined , can anything beyond the RETURN statement be executed ?
NO
for example -
def add_numbers(x, y):
return x + y print("This won't be printed") # Since this is after the return statement , it wont be executed
print(add_numbers(4, 5))
Output -
9
How to replace anything ?
boyofman
use the below example -
s = input()
def hashtagGen(s):
k=s.replace(“ “,””)
return "#"+k
print(hashtagGen(s))
Output - # suppose user enters “boy of man”
Difference between Comment and Docstrings ?
Docstrings (documentation strings) serve a similar purpose to comments, as they are designed to explain code. However, they are more specific and have a different syntax. They are created by putting a multiline string containing an explanation of the function below the function’s first line
EXAMPLE
def shout(word):
“””
Print a word with an
exclamation mark following it.
“””
print(word + “!”)
shout(“spam”)
Can a function be assigned to variable ?
YES
for example -def multiply(x, y):
return x * y
a = 4
b = 7
operation = multiply
print(operation(a, b))
Can a function be used as a parameter ?
YES
for example -
def pot(t,s):
if s>t:
return s*t
else:
return s +t
def built_in(func,t,s) : #here func is the parameter which can have ‘pot’
return func(func(t,s),func(t,s))
t=5
s=6
print(built_in(pot,t,s)) #the built_in() function has been called which callls the func ‘pot()’ has been called here
OUTPUT -
60