Вы находитесь на странице: 1из 2

Name

BELMONTE, Bianca Lou F.


APPLIED DATA SCIENCE
2nd Qtr SY 2019-2020

WORKSHEET #2: PYTHON DATA SCIENCE TOOLBOX

2 Date: 12/12/19
A variable num has been predefined as 5, alongside the following function definitions:
def func1():
num = 3
print(num)

def func2():
global num
double_num = num * 2
num = 6
print(double_num)
What value is printed out when func1() is called? 3

What value is printed out when func2() is called? 10

What is the value of num in the global scope after calling func1() and func2()? 6

3 Date: 12/12/19
Create a function named three_shouts(), which contains three parameters. This function returns a tuple of strings
concatenated with ‘!!!’. Call three_shouts(). The output should look like this:

(‘a!!!’, ‘b!!!’, ‘c!!!’)


Code

def three_shouts(w1, w2, w3):


shout1 = w1 + '!!!'
shout2 = w2 + '!!!'
shout3 = w3 + '!!!'
return (shout1, shout2, shout3)

three_shouts('a', 'b', 'c')

4 Date: 12/12/19
Create function shout_echo that has two parameters: word and echo, which has a default value of 1. The function should
concatenate word with echo copies and 3 exclamation marks. Raise an error with raise if echo is less than 0 [ValueError (‘echo
must be greater than zero.’].

Call out shout_echo() with the following arguments: “Hey”, -3


Code

def shout_echo(word,echo=1):
if echo <0:
raise ValueError("echo must be greater than zero")
else:
echo_word = (word * echo) +'!!!'
return echo_word

shout_echo('Hey',-3)

Page 1 of 2
Name
BELMONTE, Bianca Lou F.
APPLIED DATA SCIENCE
2nd Qtr SY 2019-2020

WORKSHEET #2: PYTHON DATA SCIENCE TOOLBOX

Output

ValueError: echo must be greater than zero

5 Date: 12/12/19
Create function shout_echo that has two parameters: word and echo, which has a default value of 1. The function should
concatenate word with echo copies and 3 exclamation marks. Raise an error with raise if echo is less than 0 [ValueError (‘echo
must be greater than zero.’].

Call out shout_echo() with the following arguments: “Hey”, 5


Code

def shout_echo(word,echo=1):
if echo <0:
raise ValueError("echo must be greater than zero")
else:
echo_word = (word * echo) +'!!!'
return echo_word

shout_echo(“Hey”,5)

Output

'HeyHeyHeyHeyHey!!!'

Page 2 of 2

Вам также может понравиться