0% found this document useful (0 votes)
37 views3 pages

WS#2 Naval

The document is a Python worksheet that provides examples of Python code snippets demonstrating various concepts like: - Calculating discounts and net payable amounts based on sales amount and discount rates - Using a while loop to print counting numbers incremented by 3 each time - Defining and calling a function that concatenates strings with '!!!' - Demonstrating global and local scope by printing values inside and outside functions - Defining an anonymous lambda function to raise a number to a power and testing it

Uploaded by

Carlo Naval
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

WS#2 Naval

The document is a Python worksheet that provides examples of Python code snippets demonstrating various concepts like: - Calculating discounts and net payable amounts based on sales amount and discount rates - Using a while loop to print counting numbers incremented by 3 each time - Defining and calling a function that concatenates strings with '!!!' - Demonstrating global and local scope by printing values inside and outside functions - Defining an anonymous lambda function to raise a number to a power and testing it

Uploaded by

Carlo Naval
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name APPLIED DATA SCIENCE

WORKSHEET #2: INTRODUCTION TO PYTHON

6 Date:
Create a code that asks the user to enter a sales amount. The code should calculate the effective discount based on the entered
sales amount and the net payable amount, which is the original amount less the discount. Let the discount rate be 10% for values
below 5,000 and 20% for values 5,000 and above. Below is a sample output:

Code

amt = int(input("Enter Sale Amount: "))

if(amt>0):
if amt<5000:
disc = amt*0.10
elif amt>=5000:
disc=amt*0.20
print("Discount : ",disc)
print("Net Pay : ",amt-disc)

Output

7 Date:
Create a variable named count. Initialize this variable with the value 1. Write a while loop that lists the value of count while
its value is less than 10. Increase the value of count by 3 every time.
Code
count = 1

while count < 10:


Page 1 of 3
Name APPLIED DATA SCIENCE

WORKSHEET #2: INTRODUCTION TO PYTHON

print(count)
count = count + 3

print ("End")

Output

8 Date:
Create a function named three_shouts(), which has three parameters. This function returns strings concatenated with
‘!!!’. Call three_shouts(). The output should look like this:

‘Hi!!!Hello!!!Bye!!!’
Code

# Define three_shouts
def three_shouts(word1, word2, word3):

# Define inner
def inner(word):
return word + '!!!'

# Return a tuple of strings


return (inner(word1),inner(word2), inner(word3))

# Call three_shouts() and print


print(three_shouts('Hi', 'Hello', 'Bye'))

9 Date:
A variable num has been predefined as 5, alongside the following function definitions:

def func1():
num = 3
print(num)

Page 2 of 3
Name APPLIED DATA SCIENCE

WORKSHEET #2: INTRODUCTION TO PYTHON

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

10 Date:
Create a lambda function that raises number to power pow. Print a test run of the function.
Code

def lambda (num,pow):


x = lambda num,pow : num ** pow
return x
print(lambda(2,2))

Output

Page 3 of 3

You might also like