
Learn Functions in Python Programming
Explore the concept of functions in Python programming, including defining functions, scope of variables, and reusability of code. Understand how to call functions and pass parameters efficiently within your code.
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Objectives O This chapter introduces: 1. Introduction to a function Writing a function Scope of variables in a function 2. 3. 2
Function O Function is an independent program block that can perform specific code O The program can call the defined function to execute the specified program O The code of the function can be reused and is easy to maintenance 3
Function O A function is defined using the def keyword O The code in the function needs to be separated from other programs by indentation O To call a function, use the function name followed by parenthesis The keyword to define a function function_name() def MyFunction(): ''' Function content ............ ' #The indented blocks are function content of MyFunction() #Use the function name() to call the function 4
Function O When the main program calls a function, the code in the function will be executed O After completing the execution of the code in the function, the function result will be sent back to the caller def SayHello():#the function needs to be defined before calling print('Hello') print('Good morning!') The indented block includes function content Program output: print('Hi.') SayHello() print('Nice to meet you.') 5
Function O Function can also declare variables, such as integer, floating-point number, or string The keyword to define a function function_name() def show_num(): a = 1 while a <= 16: print(a, end=' ') a *= 2 print() #The main program show_num()# Call the function show_num() # Call it again The indented block includes function content Program output: 6
Function import time #the main program while 1: period = time.strftime('%p') if period == 'AM': print(time.strftime('Am %I hour %M minute %S second')) else: print(time.strftime('PM %I hour %M minute %S second')) time.sleep(1) import time #the function def get_time(): period = time.strftime('%p') if period == 'AM': print(time.strftime('Am %I hour %M minute %S second')) else: print(time.strftime('PM %I hour %M minute %S second')) #the main program while 1: get_time() time.sleep(1) The left and the right program have the same functionality 7
Local variable O Any variable present outside any function block is called a global variable O Its value is accessible from inside of any function O The variable declared in the function is a local variable O This variable can only be used in the function O After the function is completed, the local variable will be disappeared def foo(): a = 10 print(a) foo() print(a) 8
Local variable a = 1 b = 2 def foo(): #a += 2 #UnboundLocalError: the local variable 'a' referenced before assignment a = 2 #the variables in Function will not replace the #variables in main program print('1. a=' + str(a) + ' b=' + str(b)) a += b print('2. a=' + str(a) + ' b=' + str(b)) Because the function does not declare any variable accesses the global variable b b, it Output result foo() print('3. a=' + str(a) + ' b=' + str(b)) If we assign another value to a globally declared variable inside the function, a new local variable is created in the function's namespace. This assignment will not alter the value of the global variable. 9
Global variable O If you need to access and change the value of the global variable from within a function, this permission is granted by the global keyword a = 1 b = 2 def foo(): global a #Specify a as the global variable a = 2 print('1. a=' + str(a) + ' b=' + str(b)) a += b print('2. a=' + str(a) + ' b=' + str(b)) Output result: foo() print('3. a=' + str(a) + ' b=' + str(b)) 10
Source O References: O https://docs.python.org/3/library/functions. html O Python 11
Exercise 1 O Design a program that will determine if the number inputted by the user is a prime or not. In addition, it will also ask the user to see if all factors of the inputted number is listed or not. The details can be seen in the example. Output example: 12
Exercise 2 O Write a program that accepts a sentence as input, and prints the numbers of letters and digits of the sentence on the screen. O Input: hello world! 123 Output: The number of letters is: 10 The number of digits is: 3 13
Exercise 3 O Write a program that will read the data from input.txt and ask the user to input a number N. If the number N is not listed in the input.txt, it will print -1; otherwise, it will print how many numbers listed in the input.txt less than N. O Input: Output: 11 22 -1 3 14