The first day of Python learning-the first day of Python Learning
1. Print the output Hello World:
Python2 printing method:
>>> Print "hello world"
Hello world
Python3 printing method:
>>> Print ("hello world ")
Hello world
Note: The difference between Python3 and Pytho2 is that parentheses are added.
2. Execute the code as a file:
[Root @ Centos-01 s1] # vim hello. py
Open the hello. py file and write the following content:
Print ("hello world ")
Save and exit.
The following error occurs when we execute the code file:
[Root @ Centos-01 s1] #./hello. py
./Hello. py: line 1: syntax error near unexpected token '"hello world "'
./Hello. py: line 1: 'print ("hello world ")'
This error occurs because the system does not know the language used to execute the code in the file.
The solution is to run the following command:
[Root @ Centos-01 s1] # python hello. py
Hello world
This will be executed normally. However, this execution will be inconvenient. We can specify the language in the code file for code execution.
Modify the content of the hello. py file to the following:
Method 1:
#! /Usr/bin/python
Print ("hello world ")
Method 2:
#! /Usr/bin/env python
Print ("hello world ")
Note: The first method is to specify the absolute path of the Execution code language. The second method is to find the specified language. The second method is recommended because it can improve code portability.
Now let's execute the Code:
[Root @ Centos-01 s1] # python hello. py
Hello world
[Root @ Centos-01 s1] #./hello. py
Hello world
OK. Both methods can be executed.
3. user input code:
Python3.X code:
Input_name = input ("enter your name :")
Print (input_name)
Execution result:
Enter your name: Earl
Earl
Python2.X code:
Input_name = raw_input ("enter your name :")
Print input_name
Execution result:
Enter your name: Earl
Earl
4. In Python3.x and Python2.x, the differences and mappings between user interaction modules are as follows:
Python3.x Python2.x
input raw_input
eval(input()) input()