samuel bajracharya
20 pages
samuel bajracharya
20 pages
228
/ 20
Functions in Python
Contents
1 Introduction 4
2 What is Function? 4
3 Why Functions are Important 5
4 Function Definition 5
4.1 Understanding Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5 Function Call 5
6 Calling Function Multiple Times 6
7 Function Parameters 6
8 What is Parameter? 6
9 Arguments 6
10 Example 7
11 Default Parameters 7
12 Overriding Default Value 7
13 Positional Arguments 7
14 Wrong Position Example 8
15 Keyword Arguments 8
16 Important Rule 8
17 Return Statement 9
18 Calling Function with Return Value 9
19 return vs print 9
19.1 print() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
19.2 return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
20 Flexible Function Example 9
21 isinstance() Function 10
22 Variable Number of Arguments 10
23 Star Arguments 10
24 Understanding *numbers 11
25 Example 11
26 Similar to sum() Function 11
27 Double Star Arguments 11
28 Understanding **info 11
29 Single * vs Double ** 12
29.1 Single * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
29.2 Double ** . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
30 Example Function Structure 12
31 Real Example: Grade Calculator 13
32 Calling Grade Function 13
33 Why Functions Matter Here 13
34 Using Functions in Menu Programs 14
35 Good Function Naming 14
36 Final Thoughts 14
37 Exercises 16
37.1 Exercise 1: Greeting Function . . . . . . . . . . . . . . . . . . . . . . . . 16
37.2 Exercise 2: Addition Function . . . . . . . . . . . . . . . . . . . . . . . . 16
37.3 Exercise 3: Even or Odd . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
37.4 Exercise 4: Largest Number . . . . . . . . . . . . . . . . . . . . . . . . . 16
37.5 Exercise 5: Multiplication Table . . . . . . . . . . . . . . . . . . . . . . . 16
37.6 Exercise 6: Student Grade . . . . . . . . . . . . . . . . . . . . . . . . . . 16
37.7 Exercise 7: Sum Using *args . . . . . . . . . . . . . . . . . . . . . . . . . 16
37.8 Exercise 8: User Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
37.9 Exercise 9: Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
37.10Exercise 10: Prime Number . . . . . . . . . . . . . . . . . . . . . . . . . 17
2
38 Challenge Questions 18
38.1 Challenge 1: Calculator App . . . . . . . . . . . . . . . . . . . . . . . . . 18
38.2 Challenge 2: Login System . . . . . . . . . . . . . . . . . . . . . . . . . . 18
38.3 Challenge 3: Student Result System . . . . . . . . . . . . . . . . . . . . . 18
38.4 Challenge 4: Shopping Bill . . . . . . . . . . . . . . . . . . . . . . . . . . 18
38.5 Challenge 5: Mini Bank System . . . . . . . . . . . . . . . . . . . . . . . 19
38.6 Challenge 6: Voting System . . . . . . . . . . . . . . . . . . . . . . . . . 19
38.7 Challenge 7: Password Strength Checker . . . . . . . . . . . . . . . . . . 19
38.8 Challenge 8: Menu-driven Student System . . . . . . . . . . . . . . . . . 19
38.9 Challenge 9: Custom max() Function . . . . . . . . . . . . . . . . . . . . 19
38.10Challenge 10: Function-based Quiz Game . . . . . . . . . . . . . . . . . . 20
39 Homework Advice 20
3
/ 20
End of Document
228