The use of python in linux

Author : xuzhiping   2022-11-28 15:00:56 Browse: 1121
Category : Python

Abstract: 1.View the version of python (interpreter) Generally speaking, python is pre-installed on linux. As long as you enter python in ...

Linux

1.View the version of python (interpreter)

Generally speaking, python is pre-installed on linux. As long as you enter python in Bash Shell, you can see the following version information:

Version information

Press Ctrl+D or execute the command exit(), you can exit python.

If you want to check if python3 is installed, you need to type python3 in Bash Shell.

2.Use the vim editor to create .py file, .py is the default suffix for python source files

Print ("Hello World!") // print is a function in python3, 
so be enclosed in parentheses

and then save it as first.py.

Enter Python first.py on the command line to view the script running results.

3.Installing python in windows

Download the interpreter python2 or python3 on the http://python.org/downloads/ page first, and be sure to select Add Python to Path when installing, so that the environment variables are automatically added, which will make it more convenient for you to use python.

4.What happens when you run .py files

First of all .py file states that this is a Python source file. The Python interpreter then runs this file. The Python interpreter reads every word throughout the program to determine its meaning.

5.Variable

This is the biggest difference between Python and C++, C++ is a strongly typed language, each variable requires a certain type, that is, C++ variables must be declared before use, but the type of variable in Python depends on the type of the value it is bound to, that is, in Python, the variable must be initialized immediately when defined, otherwise, the type of the variable will not be known. The definition of a Python variable is similar to Atuo X = 1; the type of X is automatically derived from the right value of the expression 1, but the difference is that in Python code, you can modify the value of the same variable anywhere, but if the type of the value on the right side of the expression assigned to the variable is different from the previous time, then the type of the variable binding is the type of the new value, which means that Python always remembers the type and value of the latest value of the variable. For example:

message = 2 //The type of message is integer. print(message) message = "Hello World!" //The type of message is a string. print(message)

6.Naming python variable names

  • The variable name contains letters, numbers, underscores, and cannot start with a number
  • Variable names cannot contain spaces, and words can be separated by underscores
  • Do not use python keywords and function names as variable names
  • Variable names should be short and descriptive. It is better not to use l and O, because they are easy to be regarded as 1 and 0.

7.The interpreter indicates an error in the program

When the interpreter executes the program, if an error occurs, the interpreter will provide a traceback, which is a record indicating where the interpreter encountered an error when running the program.

The interpreter points out errors in the program

For example, the above result shows that when the interpreter is running the program, an error occurred when the print module was last called on line 2 of the test.py file, and the error was: the variable name 'mesage' is not defined. The print (mesage) indicates the code that has an error, which is intended to help you quickly find the error code. A NameError usually means two problems, one is that the variable is not defined, and the other is that the variable is not assigned a value before using it.

In addition, unlike C++, python does not use a semicolon as the end of a statement, but a newline character as the end mark of the statement.

8.Naming method of python file name

Use uppercase and lowercase letters and underscores with the suffix of .py.

9.Data type in Python

  • String:String is a series of characters, in python, a pair of quotation marks around is the type of string, this quotation mark can be single quotation marks, double quotation marks or triple quotation marks ('''), which is different from C++, in C++ strings are enclosed in double quotation marks, single quotation marks around the representation character. This allows you to include quotation marks in a string without having to use escape characters like C++, but the quotation marks representing the string and the quotation marks inside the string cannot be the same, that is, they cannot all be double or single quotation marks. In python, methods can perform operations on data, which is consistent with C++, and the operator needs to be called after the method name (in fact, it seems to be a function call at present). Strings are also not modifiable.
  • Numbers:Everything is inseparable, and the number types of Python are nothing more than integers and floating-point numbers.
  • List:The expression of a list, similar to an array in C++, but it is richer than an array in C++, because a list can contain different types of elements. A list is a series of elements arranged in a specific order. The list is modifiable.
  • Tuples:Tuples look like lists, but are identified using parentheses instead of square brackets. Once a tuple is defined, its elements can be accessed using indexes, just like access list elements.

10.Python's control structure

If:The core of the if statement in Python is an expression with the values of True and False, for example:

If expression:

Statements
Label :
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).