Python basic syntax

Source: Internet
Author: User

1. View the Python version

Python-v

The results of the above command execution are as follows:

2. Identifiers

    • The first character must be a letter in the alphabet or an underscore _.
    • The other parts of the identifier are made up of letters, numbers, and underscores.
    • Identifiers are case sensitive.

3.python reserved words

Reserved words are keywords, and we cannot use them as any identifier names. Python's standard library provides a keyword module that can output all the keywords for the current version:

Import keyword>>> keyword.kwlist

' From ', ' global ', ' if ', ' Import ', ' on ', ' is ', ' lambda ', ' nonlocal ', ' no ', ' or ', ' Pass ', ' raise ', ' return ', ' try ', ' while ' , ' with ', ' yield ']

4. Notes

The single-line comment in Python begins with # , with the following example:

# First comment Print ("Hello, python! ")  

Multiline comments can be in multiple # numbers, plus " and " ":

# First comment # a second comment " " Third Note IV " " """ Fifth note Sixth Comment """ Print ("Hello, python! "

5. Line and indent

Python's most distinctive feature is the use of indentation to represent blocks of code, without the need to use curly braces {}.

The number of spaces to indent is variable, but the statement of the same code block must contain the same number of indentation spaces.

if True:     Print ('True') Else :     Print ('False')

The following code does not match the number of spaces in the last line of the statement, resulting in a run error:

ifTrue:Print('C')    Print('C #')Else:    Print('python')   Print('Java')#Inconsistent indentation, resulting in a run error

Error: Indentationerror:unindent does not match no outer indentation
level

Add: The If else argument does not write "()", the single must write ":"

6. Multi-line statements

Python is usually a line to finish a statement, but if the statement is long, we can use a backslash (\) to implement a multiline statement

" item_one          item_two          item_three"

Multi-line statements in [], {}, or () do not need to use backslashes (\)

Total = ['one',         'both ',          'three'

7. Wait for user input

temp = input ('\ n Please enter content:')print(temp)

\ nthe output of a new blank line before the result output

8. Display multiple statements on the same line

Python can use multiple statements in the same row, using semicolons between statements (;) split

Import ' Runoob ' ' \ n ' )#  output: Runoob

9.Print output

The print default output is newline, and if you want to implement no newline, add end= ""to the end of the variable:

"  One "  "both"#  newline output print(string1)print  (string2)#  non-newline output print(string1,end="" )print(string2)

10.import and From...import

Import the appropriate modules in Python using Import or from...import.

Import the entire module (turtle) in the format: import Turtle

Import a function from a module in the format: from turtle import done

Import multiple functions from a module in the format: from turtle import done,deepcopy

Import all functions in a module in the format: from turtle Import *

Import Turtle  from Import  Done  from Import done,deepcopy  from Import *

11. Command-line arguments

Many programs can perform some operations to see some basic information, Python can use the-H parameter to view the parameters Help information

Python basic syntax

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.