Def function Flashcards
Print: ‘cat, dog’ by def func
def animals (a, b):
return( a + ‘ , ‘ + b)
print(animals(‘cat’, ‘dog’))
print: ‘tac, god’ by def func
def animals (a, b):
return (a[::-1] + ‘ , ‘ + b[::-1])
print(animals(‘cat’, ‘dog’))
print: 8,9 by def by 2,3
def raise_both(value1, value2):
new_value1 = value1 ** value2
new_value2 = value2 ** value1
return(new_value1, new_value2)
print(raise_both(2,3))
The value within the function is: 10
the value outside of the function is: 20
using only X
Def my_func():
X = 10
print(‘The value within the function’, X)
X = 20
my_func()
print(‘The value outside of the function: ‘, x)
X outside of def (before)
The value of the variable is: 44
X = 44
def my_func():
print(‘the value of the variable: ‘ , x)
my_func()
The youngest child is Linus
using *
If you do not know how many arguments that will be passed into your function
def my_function(*kids):
print(“The youngest child is “ + kids[2])
my_function(“Emil”, “Tobias”, “Linus”)
írj egy függvényt ami igényel két argumentumot(stringet) és vissza nyújtja (és ki is nyomtatja)
a két stringet egymás mellett szóközzel elválasztva visszafelé sorrendben.
def string(a,b):
return(a[::-1] + ‘ ‘ + b[::-1])
print(string(‘Kati’, ‘Mari’))
írj egy függvényt ami igényel 2 argumentumot(egy számot és egy stringet) és vissza nyújtja (és ki is nyomtatja) a stringet annyiszor amennyi a szám argumentum. (a stringet soremelés(új sor) karakter válassza el)
def compl(text,length):
for i in range(length):
print(text, end=’ ‘)
print(‘ ‘)
text = ‘longword’
length = 6
compl(text, length)
Absolute value of a number with def function
Def absolute_value(num):
If num >= 0:
Return num
Else:
Return -num
Print(absolut_value(-9))
Create a function with variable length of arguments - with for loop inside calculate the average
def percentage(*args):
sum = 0
for i in args:
sum = sum + i
avg = sum / len(args)
print(‘Average =’, avg)
Write a program to create function calculation() such that it can accept two variables and calculate addition and subtraction. Also, it must return both addition and subtraction in a single return call.
def calculation(a, b):
addition = a + b
subtraction = a - b
return addition, subtraction
res = calculation(40, 10)
print(res)
Create a function in such a way that we can pass any number of arguments to this function, and the function should process them and display each argument’s value.
def func1(*args):
for i in args:
print(i)
func1(20, 40, 60)
func1(80, 100)
Write a program to create a function show_employee() using the following conditions.
It should accept the employee’s name and salary and display both.
If the salary is missing in the function call then assign default value 9000 to salary
def show_employee(name, salary=9000):
print(‘Name: ‘, name, ‘salary: ‘, salary)
show_employee(‘Ben’, 12000)
show_employee(‘Jessa’)
with def recursion 8,6,4,2 even numbers
def EvenNums(num):
print(num)
if num == 2:
return num
else:
return EvenNums(num-2)
EvenNums(8)
with def recursion 8,6,4,2 , first entering even odd number
def EvenNums(num):
print(num)
if num % 2 != 0:
print(‘Please enter and even number’)
elif num == 2:
return num
else:
return EvenNums(num-2)
EvenNums(11)