Repository navigation
Expand file tree
/
Copy pathfile14.py
More file actions
100 lines (60 loc) · 1.97 KB
/
Copy pathfile14.py
File metadata and controls
100 lines (60 loc) · 1.97 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
81
82
83
84
85
86
87
88
89
90
91
# Functions
# Make a function that prints "Hello World"
def hello_world():
print("Hello World")
hello_world()
# Make function with parameters
def greeting(name):
print(f"Good morning {name}")
greeting("John")
greeting("Sarah")
def custom_greeting(first_name, last_name, message):
print(f"Hey, {first_name} {last_name}, {message}")
custom_greeting("Tareq", "Aziz", "Welconme aboard!")
# Keyword argument ( position does not matter if it is used)
custom_greeting(last_name="Aziz", message="Good evening", first_name="Tareq")
# function with default argument
def smith_family(first_name, last_name = "Smith"):
print(f"Hello! {first_name} {last_name}, Thanks for joining")
print(smith_family("Bob"))
# fucntion with return statement
# Every function returns a "None" by default
def square (number):
return number * number
result = square(4)
print(result)
#or
print(square(5)) # without return it will return "None"
# Function that returns cube of a number
def cube(number):
return number ** 3
# Function that calculates average
def average_cost(total_cost, number_of_items):
return total_cost / number_of_items
print(average_cost(100, 5))
print(cube(4)) #output - 20
# Emoji converter function
def convert_to_emoji(message):
words = message.split(" ")
emojis = {
"happy": ":)",
"sad": ":(",
"angry": "-_-",
"shy": "^_^"
}
output = ""
for word in words:
coverted = emojis.get(word, word)
output += coverted + " "
return output
message = input("> ")
print(convert_to_emoji(message))
# Callback function
'''A callback function is essentially a function that you pass as an argument
to another function, with the expectation that it will be executed at a specific time. '''
def upperCase(text):
return text.upper() # transforming the text to upper case letters.
def greet(name, upperCase):
greeting1 = upperCase(name)
print(f"Hello {greeting1}")
greet("tareq", upperCase)