Here is only for personal notes. The content and code are mostly from the 100 day course of GitHub Luo Hao God. The link is attached below, which is infringed and deleted~
GitHub - jackfried / python-100-days: Python - 100 days from novice to master
1, Use of functions and modules
(1) Define function: use the def keyword to define a function. The parameters passed to the function can be placed in parentheses after the function name. After the function is executed, you can i use the return keyword to return a value.
(2) Function parameters: in Python, function parameters can have default values and support the use of variable parameters. Therefore, python does not need to support function overloading like other languages, because we can use a function in many different ways when defining it. The following are two small examples.
from random import randint def roll_dice(n=2): #Set the default value to 2 dice """ Chromophore """ total=0 for _ in range(n): total +=randint(1,6) return total def add(a=0, b=0, c=0): """Add three numbers""" return a + b + c # If no parameters are specified, shake two dice with the default value print(roll_dice()) # Shake three dice print(roll_dice(3)) print(add()) print(add(1)) print(add(1, 2)) print(add(1, 2, 3)) # Parameters can be passed in different order print(add(c=50, a=100, b=200))
This point is not particularly understood. Let's find some lessons to listen to!
In fact, the above add function has a better implementation scheme. Variable parameters are used when the number of parameters is uncertain.
# A * in front of the parameter name indicates that args is a variable parameter def add(*args): total = 0 for val in args: total += val return total # 0 or more parameters can be passed in when calling the add function print(add()) print(add(1)) print(add(1, 2)) print(add(1, 2, 3)) print(add(1, 3, 5, 7, 9))
(3) Manage functions with modules: two functions with the same name are defined in the same. py file. Since Python does not have the concept of function overloading, the following definitions will overwrite the previous definitions, which means that only one function with the same name actually exists.
Each file in Python represents a module. We can have functions with the same name in different modules. When using the function, we can import the specified module through the import keyword to distinguish which module foo function to use. The code is as follows.
This place can't run out!!!!!
How to solve this???!!!
practice:
1. Realize the function of calculating the maximum common divisor and the minimum common multiple
def gcd(x,y) """greatest common factor """ (x, y) = (y, x) if x > y else (x, y) for factor in range(x,0,-1): if x%factor==0 and y%factor==0: return factor def lcm(x,y) """Find the least common multiple""" return x*y//gcd(x,y)
2. Implement a function to judge whether a number is a palindrome number
def is_palindrome(num): """Judge whether a number is a palindrome number""" temp=num total=0 while temp>0: total=total*10+temp%10 temp//=10 return total==num
3. Realize the function of judging whether a number is a prime number
def is_prime(num): """Judge whether a number is a prime number""" for factor in range(2, int(num ** 0.5) + 1): if num % factor == 0: return False return True if num != 1 else False
4. Write a program to determine whether the input positive integer is a palindrome prime
if __name__ == '__main__': num = int(input('Please enter a positive integer: ')) if is_palindrome(num) and is_prime(num): print('%d Is a palindrome prime' % num)
2, Scope of variable
def foo(): b = 'hello' # Functions can be redefined inside functions in Python def bar(): c = True print(a) print(b) print(c) bar() # print(c) # NameError: name 'c' is not defined if __name__ == '__main__': a = 100 # print(b) # NameError: name 'b' is not defined foo()
The above code can execute smoothly and print out 100, hello and True, but we noticed that there are no variables A and B defined inside the bar function, so where do a and B come from? We defined a variable a in the if branch of the above code, which is a global variable , belongs to the global scope, because it is not defined in any function. In the foo function above, we define the variable B, which is a local variable defined in the function , belongs to the local scope and cannot be accessed outside the foo function; however, for the bar function inside the foo function, the variable b belongs to the nested scope and we can access it in the bar function. The variable c in the bar function belongs to the local scope and cannot be accessed outside the bar function. In fact, python looks up a variable according to the "local scope" , nested scope, global scope and built-in scope. We have seen the first three in the above code. The so-called "built-in scope" is those identifiers built in Python. The input, print and int we used before belong to the built-in scope.