Def function Flashcards

1
Q

Print: ‘cat, dog’ by def func

A

def animals (a, b):
return( a + ‘ , ‘ + b)

print(animals(‘cat’, ‘dog’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

print: ‘tac, god’ by def func

A

def animals (a, b):
return (a[::-1] + ‘ , ‘ + b[::-1])

print(animals(‘cat’, ‘dog’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print: 8,9 by def by 2,3

A

def raise_both(value1, value2):
new_value1 = value1 ** value2
new_value2 = value2 ** value1

return(new_value1, new_value2)

print(raise_both(2,3))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The value within the function is: 10
the value outside of the function is: 20
using only X

A

Def my_func():
X = 10
print(‘The value within the function’, X)

X = 20
my_func()
print(‘The value outside of the function: ‘, x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

X outside of def (before)
The value of the variable is: 44

A

X = 44

def my_func():
print(‘the value of the variable: ‘ , x)

my_func()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The youngest child is Linus
using *
If you do not know how many arguments that will be passed into your function

A

def my_function(*kids):
print(“The youngest child is “ + kids[2])

my_function(“Emil”, “Tobias”, “Linus”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

í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.

A

def string(a,b):
return(a[::-1] + ‘ ‘ + b[::-1])

print(string(‘Kati’, ‘Mari’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

í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)

A

def compl(text,length):
for i in range(length):
print(text, end=’ ‘)
print(‘ ‘)

text = ‘longword’
length = 6

compl(text, length)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Absolute value of a number with def function

A

Def absolute_value(num):
If num >= 0:
Return num
Else:
Return -num
Print(absolut_value(-9))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a function with variable length of arguments - with for loop inside calculate the average

A

def percentage(*args):
sum = 0
for i in args:
sum = sum + i
avg = sum / len(args)
print(‘Average =’, avg)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

def calculation(a, b):
addition = a + b
subtraction = a - b
return addition, subtraction

res = calculation(40, 10)
print(res)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

def func1(*args):
for i in args:
print(i)

func1(20, 40, 60)
func1(80, 100)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

def show_employee(name, salary=9000):
print(‘Name: ‘, name, ‘salary: ‘, salary)

show_employee(‘Ben’, 12000)
show_employee(‘Jessa’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

with def recursion 8,6,4,2 even numbers

A

def EvenNums(num):
print(num)
if num == 2:
return num
else:
return EvenNums(num-2)

EvenNums(8)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

with def recursion 8,6,4,2 , first entering even odd number

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

factorial with recursion

A

def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)

print(factorial(5))
17
Q

1,3,6,10,15,21 with recursion

A

def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result