Functions in Python include built-in functions, standard library functions, third party library functions
3
00:00:20,950 --> 00:00:22,350
and user self-defined functions. Build-in functions are those included in the builtins module. After installing Python, they can be directly used like input() and print(). Standard library functions are those requiring users to import the module before use . Each library has some relevant functions, such as the pow() function in the math library. There are a ton of third party libraries, which is also an essential characteristics advantage of Python. The famous scientific computing package SciPy, for instance, includes a large number of functions for scientific computing. Well, next, let's talk about common functions in several common standard libraries in Python in this class, including the math library, the os library, the random library and the datetime library. Alright, let me show you. To use functions in the math library, a.k.a. the math module, we first need to import this module. Then, we can use the dir() function to view the functions included in the module. As we see, there are two constants, one is pi, another is e. For learning about the effect of a certain function, we can resort to the help() function. Like the ceil() function, its effect is to round up. There is a similar function, floor(), to round down. Some other common functions also exist, such as to calculate the result of x to the power of y, and radication. Also, let's look at this pair of functions, degrees() and radians(). According to their return results, we see, one is to convert radian into angle, and the other is to convert angle into radian, such two functions. Many more functions are provided in the math library, you can practice them more. Next, look at the os module. The os module provides many functions to interact with the operating system . Take several common functions for file and directory processing for example. First, it's also necessary to import the module. Let's first look at the very common function getcwd() According to the return result, we can probably guess that this function is for acquiring the current working directory. Of course, the directory can be changed. Here, for instance, we change it to under the test directory in Disk C. We only need the chdir() function to achieve it. Bingo! Changed. Besides there are functions like rename(). Also quite common. If we want to modify a file under C:\test, rename it as test1. To delete a file, use the remove() function. Next, look at the random module. This module provides many functions to generate random numbers. For example, the game of Guess the Number we mentioned before uses the randint() function here. Several simple examples below are used to demonstrate the effect and use of common functions in this module. For more detailed explanations, you may view them with the help() function. First, import the module. Let's try the choice() function first. As we see, it may acquire a random value from the sequence. Then, let's look at the aforesaid function randint(). It may generate a random integer between 1 and 100. Surely, 1 and 100 here can be replaced with other numbers. The effect of this randrange() function is to, from such an array generated with the following range(), acquire a random integer. Another function random(). Its effect is to generate a random floating-point number between 0 and 1.0, including 0 but excluding 1.0. uniform() is another common one, it generates a random floating-point number between 5 and 10. Let's look at the function, sample(), According to the result, you may guess that it is to randomly acquire a group of values of a designated quantity from the sequence we provide, i.e. range(100), of course, a set is also OK. The result is returned in a list. The quantity here is 10. Let's look at another very helpful function, shuffle(), in the module random. Define a list first. As we see, the shuffle() function may disarrange elements in the nums list. Is it very suitable for drawing lots. We may add such attributes as names and student ID numbers of all persons into a list, and then change the original sequence with the shuffle() function. Mum needn't worry about my drawing a bad lot any more. Next, let's look at the datetime module. This module provides various methods for data and time processing. While supporting date and time algorithms, this module focuses on valid attribute extraction for output formatting and processing. Let's import this module first. What's included in this module? The "datetime" module contains the "data" class representing dates, the "datetime" class representing dates and time, and the "time" class representing time. Each class contains some relevant functions. For example, look at the "date" module first, its most frequently used one is the "today()" function, which is the date today. Next, look at the "time" module. Let's create a time. OK, we've created a time, 23:20:35. Next, look at "datetime". It can indicate both time and date. With the function "now()", we can view the current date and time. However, as we notice, it finally includes a strange value, which is indeed a millisecond different from our usual habit. We may use the "strftime()" function to convert, the "datetime" object into a string in our fixed format, say, such a format here. The format symbols of time and date in Python have fixed meanings. For example, in this case, what does a% mean? It's simplified name of local time. And A% is the local complete form of day-of-week. For the specific meaning of the format, you may view it with the help() function. In datetime, there are a set of common functions, timestamp() and fromtimestamp(). The effect of the former is to convert a "datetime" into a globally unified time stamp, while the effect of the latter is to convert the time stamp into the local date and time. Then, what is a time stamp? Roughly it means, in the computer, the time of 1971-01-01 00:00 GMT is defined as the Epoch Time, as 0, and the currently readable time is calculated by the seconds starting from the Epoch Time. It is called the timestamp. As we see the time stamp has nothing to do with the time zone. The time stamps of all the computers in the world are the same. But, "datetime" is related to the timezone. Give it a go. The date and time are created. We use the function "timestamp()" to convert it into the time stamp. Have a try. This is the time stamp. We then use the function fromtimestamp() to convert the time stamp into the local time and date. Well, this is the time we created just now. So much for the common functions in the various standard libraries in Python. Please try more if you have free time. So easy, just view with the dir() function, and then use the help() function to view its help information. Very simple. Practice makes perfect.