-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_basic_functions.py
More file actions
107 lines (90 loc) · 3.69 KB
/
Copy path24_basic_functions.py
File metadata and controls
107 lines (90 loc) · 3.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#======================================================================================
# Functions
#--------------------------------------------------------------------------------------
# A function is a reusable block of code that performs a specific task
# Why use functions ?
# Code reusability
# Better organization
# Easier debugging
# Reduces repetition
#======================================================================================
import math
#--------------------------------------------------------------------------------------
# Built-in Function (Just Calling)
#--------------------------------------------------------------------------------------
print(len('python'))
#--------------------------------------------------------------------------------------
# Function From Libraries (Import Then Call)
#--------------------------------------------------------------------------------------
number = 4.2
print(math.ceil(number))
#--------------------------------------------------------------------------------------
# User Defined Function
#--------------------------------------------------------------------------------------
# Defining a Function
def greet():
print(f'Hello World')
# Calling a Function
greet()
#--------------------------------------------------------------------------------------
# Function components
#--------------------------------------------------------------------------------------
# def --> keyword used to create a function
# greet --> function name
# () --> parameter area
# : --> starts function body
# Indented code -->function body
#--------------------------------------------------------------------------------------
# Here we are using parameters and arguments in functions
# Check 25_parameters_&_arguments.py file for more about parameters and arguments
#--------------------------------------------------------------------------------------
# return --> sends a value back from a function
#--------------------------------------------------------------------------------------
# syntax
# def function_name():
# return value
#--------------------------------------------------------------------------------------
# Returning a sum
def add(a,b):
return a + b
result = add(10,20)
print(result)
#--------------------------------------------------------------------------------------
# Key Point:- If a function has no return statement, python returns None by default
#--------------------------------------------------------------------------------------
def add1(x,y):
print(x+y)
result = add1(5,5)
print(result)
#--------------------------------------------------------------------------------------
# Important Points
#--------------------------------------------------------------------------------------
# return ends the function immediately.
# A function can have multiple return statements:
# Code after return inside the same block will not execute.
#--------------------------------------------------------------------------------------
# return ends the function immediately.
def demo():
return 'Hello'
print('World') # Never runs
result = demo()
print(result)
# A function can have multiple return statements:
# Code after return inside the same block will not execute.
def clean_name(name):
if not name:
return None
else:
return name.strip().lower()
result1 = clean_name('')
print(result1)
result2 = clean_name(' MaRIA ')
print(result2)
# Returning multiple values
def clean_name(name):
lo_cleaned = name.strip().lower()
up_cleaned = name.strip().upper()
return lo_cleaned, up_cleaned
lo_name, up_name = clean_name(' aLeX ')
print(lo_name)
print(up_name)