-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.py
More file actions
80 lines (66 loc) · 2 KB
/
Copy pathFunctions.py
File metadata and controls
80 lines (66 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#Function definition
def my_function():
print("Hello from a function")
#Function invocation
my_function()
#Empty Functions
def empty_func():
pass
empty_func()
#Positional Arguments
def calc(qty, item, price):
print("The "+ item + " cost/s" , (qty*price), "rupees")
calc(2,'Apples', 20,50)
#Keyword Arguments
def calc(qty, item, price):
print("The "+ item + " cost/s", (qty*price), "rupees")
calc(qty=5, item="Oranges",price=10)
# Default Parameters
def greet(name, msg="How are you?"):
print(name + ", " + msg)
greet("John")
greet("John", "Good Evening")
greet("Good Evening")
#Arbitrary Arguments
def add(*args):
#sum() is an inbuilt function to sum up a list
total=(sum(args))
print("The sum : " ,total)
def greet(msg, *names):
for name in names:
print(msg + ", " + name)
#Calling add() function
add(1,4,5)
#Calling greet() function
greet("Hello","John","Maggie","Lucy")
#The return Statement
def totalPrice(quantity, price):
"""
This function return a single value; The total price
"""
return quantity*price
def getItemDetails():
"""
getItemDetails function return three values: a Tuple of (stock, name, price).
but you can receive them as three separate results as well.
"""
return 15,"Bread",80.00
def showItemDetails(quantity,name,price,total):
"""
showItemDetails function returns nothing; It displays the details in screen.
So the return statement is optional
"""
print("Qty: {} Item: {} Unit Price: {} Total Price: {}".format(quantity,name,price,total))
#Receiving returning result as a Tuple
#details = getItemDetails()
#print (details)
#Receiving returning result as separaet results
stock, itemName , price = getItemDetails()
quantity = 10
total = totalPrice(quantity,price)
showItemDetails(quantity,itemName,price,total)
#Docstrings
def calc(quantity, price):
"""Returns the product of quantity and price"""
return quantity*price
print (calc.__doc__)