“Life is short (you need Python)” comes from Bruce Eckel’ s blog signature.
If you’ve heard of the TIOBE leaderboard, you’ll know the popularity of programming languages.
This is a variation of the most 10 commonly used programming languages over the past 10 years:
Today, we will introduce some basic knowledge of this forth popular programming language——Python.
1. What is Python
Python, like Java and C#, is also a virtual machine-based language. When we type “hello.py” on the command line, actually we activate the Python “interpreter” and tell the “interpreter” that “you” will start working. However, before the “interpretation”, the first work performed is the same as the compiled Java. So we should describe Python in this way: Python is a language that is first compiled and then interpreted.
2. Running process of Python
Before we discuss that, let’s learn about two concepts, PyCodeObject and pyc files. We may already know about the pyc on the hard disk, PyCodeObject is actually the result of the Python compiler. When the Python program runs, the compilation results are stored in PyCodeObject in memory. After the running finished, the Python interpreter writes PyCodeObject back to the pyc file. As it runs for the second time, the program will first look for the pyc file on the hard disk. If found, it will be loaded directly, otherwise the above process will be repeated.
Therefore, we say that the pyc file is actually a persistent save of PyCodeObject.
3. The functions of Python
Now let’s know more about Python through its functions.
3.1 Function format
The function format of Python is as follow:
Note: The statement following the indentation of the function is called a statement block. Indentation is for the logic and affiliation of the table name statement. It can not be ignored, otherwise the code can not run successfully.
3.2 Parameters
3.2.1 Positional arguments
For example,
Then we call this function:
Obviously,the filled parameters 1, 2, and 3 correspond to the parameters base_up, base_down, and height, respectively. This way of passing parameters is called positional arguments.
3.2.2 Keyword arguments
When calling a function, we assign a value to each parameter name. For example, when we call function fun1:
Output:
3.2.3 Variable-length arguments
Sometimes when designing a function interface, we are not able to determine the number of parameters passed in beforehand. Python provides a way to accept parameters that are not directly defined through adding an asterisk * to the front of the parameter. If no argument is specified when the function is called, it is an empty tuple. We can also pass unnamed variables to functions. For example:
From the output, we can see that * hobby is a variable parameter, and hobby is actually a tuple.
3.2.4 Keyword-only arguments
This can be achieved by placing the keyword-only arguments inside a * parameter or a single *, for example :
3. 3 Anonymous function
Python uses lambda to create anonymous functions, that is, no longer define a function in the standard form of a def statement.
Basic syntax:
For example:
4. The shortcomings of Python
4.1 Low operating efficiency
The main drawback of Python is : slow. It runs slower than C, C++, and Java. This is mainly because Pyhton is an interpreted language that must be interpreted before when the program is run.
4.2 Unable encryption
Python relies on indentation to write codes, which can’t be compressed and confused. So it also determines that Python codes can’t be encrypted.
And with this ends another interesting entry in our blog, if you have any questions or suggestions for us to write about a specific topic do not hesitate to contact us at: blog@tribalyte.com