Python3 basic syntax and python3 syntax

Source: Internet
Author: User

Python3 basic syntax and python3 syntax

I. Encoding

By default, the python3 source code file is encoded in UTF-8, and all strings are unicode strings. Of course, you can also specify different codes for the source code file:

1 # -*- coding: gbk -*-

 

Ii. identifier

1. The first character must be a letter or underscore '_'.

2. Other parts of the identifier are composed of letters, numbers, and underscores.

3. The identifier is case sensitive.

In python3, non-ASCII identifiers are also allowed.

 

Iii. Reserved python words

Reserved Words are keywords. We cannot use them as any identification names. The standard python Library provides a keyword module that can output all the keywords of the current version:

>>> import keyword>>> keyword.kwlist['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']>>> 

 

Iv. Notes

# Single line comment

''' Or "multi-line comment. Three single (double) quotation marks appear in pairs. You can also use this symbol to represent a piece of content.

 

V. Line and indent

Python uses indentation to represent the structure of code blocks. The number of indented spaces is variable, but the same number of indented spaces must be used in the same code block.

 

Vi. Data Types

Python data types include

Numbers (number)

String (String)

List)

Tuple (tuples)

Sets)

 

Number Type:

Number Type: integer, long integer, floating point, and plural

Integer: 1

Long Integer: a relatively large integer.

Floating Point: 1.23 3E-2

Plural: 1 + 2j, 1.1 + 2.2j

 

String:

* In python, single quotes and double quotes are identical.

* You can use three quotation marks (''' or ") to specify a multi-line string.

Escape Character '\'

Natural character, by adding r or R before the string. For example

Print (r "this is a line with \ n") show the result this is a line with \ n

The string is unchangeable.

Cascade strings literally, such

>>> a = "this " "is " "string">>> a'this is string'

 

 

Dictionarys (dictionary)

 

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.