The first day of Python Learning & mdash;

Source: Internet
Author: User

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()

 


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.