Python FUNCTIONS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor.
For answers, please select the invisible text right after Answer:


Implement a function called my_func() that creates a variable add which stores the result of adding 20 and 30, and prints out the value of add. Don’t forget to also call the function!

50

Answer:

def my_func():
    add = 20 + 30
    print(add)

my_func()

 Implement a function called my_func() that takes a single parameter x and multiplies it with 5, also returning the result when the function is called

result = my_func(20)
print(result)
100

Answer:

def my_func(x):
    return x * 5

result = my_func(20)
print(result)

Implement a function called my_func() that takes two parameters x and y and divides x by y, also returning the result when the function is called.

result = my_func(32,8)
print(result)
4.0

Answer:

def my_func(x, y):
    return x / y

result = my_func(32,8)
print(result)

Implement a function called my_func() that takes 3 parameters x, y and z and raises x to the power of y then adds z, also returning the result when the function is called.

result = my_func(3,3,3)
print(result)
30

Answer:

def my_func(x, y, z):
    return x ** y + z

result = my_func(3,3,3)
print(result)

Implement a function called my_func() that takes a single parameter x and multiplies it with each element of range(5), also adding each multiplication result to a new (initially empty) list called my_new_list. Finally, the list should be printed out to the screen after the function is called.

result = my_func(2)
print(result)
[0, 2, 4, 6, 8]

Answer:

def my_func(x):
    my_list = []
    for i in range(5):
        my_list.append(i * x)
    return my_list

result = my_func(2)
print(result)

Implement a function called my_func() that takes a single parameter x (a tuple) and for each element of the tuple that is greater than 4 it raises that element to the power of 2, also adding it to a new (initially empty) list called my_list. Finally, the code returns the result when the function is called.

result = my_func((2, 3, 5, 6, 4, 8, 9))
print(result)
[25, 36, 64, 81]

Answer:

def my_func(x):
    my_list = []
    for i in x:
        if i > 4:
            my_list.append(i ** 2)
    return my_list

result = my_func((2, 3, 5, 6, 4, 8, 9))
print(result)

Implement a function called my_func() that takes a single parameter x (a dictionary) and multiplies the number of elements in the dictionary with the largest key in the dictionary, also returning the result when the function is called.

result = my_func({1: 3, 2: 3, 4: 5, 5: 9, 6: 8, 3: 7, 7: 0})
print(result)
49

Answer:

def my_func(x):
    return len(x) * sorted(x.keys())[-1]

result = my_func({1: 3, 2: 3, 4: 5, 5: 9, 6: 8, 3: 7, 7: 0})
print(result)

Implement a function called my_func() that takes a single positional parameter x and a default parameter y which is equal to 10 and multiplies the two, also returning the result when the function is called.

result = my_func(5)
print(result)
50

Answer:

def my_func(x, y = 10):
    return x * y

result = my_func(5)
print(result)

Implement a function called my_func() that takes a single positional parameter x and two default parameters y and z which are equal to 100 and 200 respectively, and adds them together, also returning the result when the function is called.

result = my_func(50)
print(result)
350

Answer:

def my_func(x, y = 100, z = 200):
    return x + y + z

result = my_func(50)
print(result)

Add the necessary line of code inside the function in order to get 80 as a result of calling myfunc() and have the result printed out to the screen.

var = 8
def my_func(x):
    print(x * var)
    var = 12

my_func(10)
80

Answer:

var = 8
def my_func(x):
    global var
    print(x * var)
    var = 12

my_func(10)

Write code that will import only the pi variable from the math module and then it will format it in order to have only 4 digits after the floating point. Of course, print out the result to the screen using the print() function.

3.1416

Answer:

from math import pi

print("%.4f" % pi)

Add the necessary code in between print’s parentheses in order to read the content of text_file.txt as a string and have the result printed out to the screen.

f = open("text_file.txt", "r")
print()

Answer:

f = open("text_file.txt", "r")
print(f.read())

Add the necessary code in between print’s parentheses in order to read the content of text_file.txt as a list where each element of the list is a row in the file, and have the result printed out to the screen.

f = open("text_file.txt", "r")
print()

Answer:

f = open("text_file.txt", "r")
print(f.readlines())

Add the necessary code on line 5 in order to bring back the cursor at the very beginning of text_file.txt before reading from the file once again.

f = open("text_file.txt", "r")
f.read()
print()

Answer:

f = open("text_file.txt", "r")
f.read()
f.seek(0)
print(f.readlines())

Leave a Reply

Prev
Python EXCEPTIONS Exercise for Beginners and Intermediate

Python EXCEPTIONS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor

Next
Manage Users and Groups Permissions in Linux
Manage Users and Groups Permissions in Linux

Manage Users and Groups Permissions in Linux

A lot of new hackers even after having obtained the access will not be able to

You May Also Like