The first Python program
You can open Notepad or other text editor, enter: Print ("Hello python!"), save the file to any drive letter, the suffix is. py
Two ways to execute Python programs
1. Execute the code in the terminal:
Execute command: The path \ filename of the Python file saved. py (red in the picture)
This can be done directly because the environment variable is configured, otherwise it will be an error, tell you can't find the interpreter
Python: An interpreter that specifies the execution of the program
2. Enter interactive mode and get execution results in real time (blue in the picture)
Interpreter path
#!/usr/bin/env python
In the first line of the Py file there is often this sentence stating which Python interpreter is currently in use and executing the command under the Linux system:
./hellopython.py
The reason this can be done is that #!/usr/bin/env python has specified the interpreter as Python
If you do this by using the Python file name. PY, the above statement is useless.
Coding
#-*-Coding:utf8-*-
Python2 in the beginning to add this sentence, because the python2 development of the default is the ASCII code, ASCII code does not contain Chinese, so Python2 tells the Python interpreter to utf-8 encoding way to execute the file.
There's no use in Python3, Python3 already supports Chinese encoding
Write a login Validator
1Usernme = input ("Please enter user name:")2Password = input ("Please enter your password:")3 4 ifUsernme = ="Root" andPassword = ="123123":5 Print("Login Successful! ")6 Else:7 Print("Login failed! ")
Variable
Usernme and password are variable names, so-called variables, that is, its value can be changed, not fixed.
Naming rules for variables:
1. Variable names can only contain letters, numbers, underscores,
3. Cannot be keyword, is all python keyword
4. It's best not to repeat with Python's built-in stuff
Need to know, write the program when there is a variable name, but when the program executes, the program to the Python interpreter, the Python interpreter and the program to the bottom of the C language to execute, when C is interpreted, there is no variable name,
Instead, use values instead, such as:
1 " Root " 2 Print (username") 3 4 # when C is interpreted, there is no variable name, and the variable name is replaced directly with the value: 5 Print ("root")
5. Give the variable a name, the best thing to start is meaningful, see the variable name to know what he stands for. So, if you have more than one word to write a variable name, you can connect it with an underscore:
such as: user_id
The second Python initial recognition and variables