Profile picture

Python Under 10 Minutes

Last updated: July 1st, 20192019-07-01Project preview

rmotr


Python in under 10 minutes - Exercises

green-divider

Exercise 1

Convert the following str into int type.

revenue_as_string = "500000"

In [12]:
revenue_as_string = "500000"

# your code goes here
In [13]:
revenue_as_integer = int(revenue_as_string)

revenue_as_integer
Out[13]:
500000

green-divider

Exercise 2

Make a function using the following formula:

$$ f(x,y) = 2 x + y $$
In [ ]:
# your code goes here

The function will have two parameters $x$ and $y$.

In [23]:
def func(x, y):
    return 2*x + y
In [24]:
func(10, 5)
Out[24]:
25
In [34]:
func(-3, 10)
Out[34]:
4

green-divider

Exercise 3

Make a function that receives a string as parameter and return the last letter on it.

In [ ]:
# your code goes here
In [21]:
def last_letter(string):
    return string[-1]
In [22]:
last_letter('Python')
Out[22]:
'n'
In [32]:
last_letter('RMOTR')
Out[32]:
'R'

green-divider

Exercise 4

Make a function that receives a list as parameter and return how many elements it has; if it doesn't have any element return an error.

In [ ]:
# your code goes here
In [29]:
def list_size(my_list):
    size = len(my_list)
    
    if (size > 0):
        return 'List has {} elements'.format(size)
    else:
        return 'Error, no elements'
In [30]:
list_size([1, 2, 3])
Out[30]:
'List has 3 elements'
In [31]:
list_size([])
Out[31]:
'Error, no elements'

purple-divider

Notebooks AI
Notebooks AI Profile20060