Python basic syntax

Source: Internet
Author: User

Python internal execution process

Coding

By default, Python3 source files are encoded in UTF-8, and all strings are Unicode strings. Of course, you can also specify a different encoding for the source file:

# -*-coding:cp-1252–*-
1. ASCII

The PYTHON2 interpreter encodes the content when it loads the code in the. py file (default ASCII)

ASCII is a computer coding system based on the Latin alphabet, which is mainly used to display modern English and other Western European languages, with a maximum of 8 bits (1 bytes), namely: 2**8=256. All ASCII codes can represent up to 256 symbols

2. Unicode

Unicode (Uniform Code, universal Code, single Code) is an industry standard in the field of computer science, including character set, encoding scheme, etc. Unicode is created to address the limitations of traditional character encoding schemes, which set a uniform and unique binary encoding for each character in each language to meet the requirements of cross-language, cross-platform text conversion and processing. Unicode specifies that all characters and symbols are represented by a minimum of 16 bits (2 bytes), i.e.: 2**16=65536

3.utf-8

UTF-8 (8-bit Unicode Transformation Format) is a variable-length character encoding for Unicode. Created by Ken Thompson in 1992. It is now standardized to RFC 3629. UTF-8 encodes Unicode characters in 1 to 4 bytes. The same page can be used to display Chinese simplified traditional and other languages (such as English, Japanese, Korean). It does not use a minimum of two bytes, but instead classifies all characters and symbols: the contents of the ASCII code use 1 bytes, the European characters use 2 bytes, the East Asian characters use 3 bytes ...

Identifier

In programming languages, identifiers are words that the programmer himself prescribes, such as: Class name, function name, property name, variable name, and so on.

  • The first character must be either a character in the alphabet or an underscore "_"
  • Other parts of the identifier can consist of letters, numbers, and glide lines
  • Identifiers are case-sensitive
  • Non-ASCII identifiers are also allowed in Python3

Identifiers that begin with an underscore have a special meaning. A class attribute that begins with a single underscore (_foo) cannot be accessed directly, is accessed through the interface provided by the class, cannot be imported with a "from * * * * * * * *", and a double underscore (__foo) represents a private member of a class; A double underscore that starts and ends (__foo__ ) Represents a special method-specific identifier in Python, such as __int__ (), which represents the constructor of a class.

Python reserved word

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

>>>Importkeyword>>>keyword.kwlist['False','None','True',' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','finally',' for',' from','Global','if','Import','inch',' is','Lambda','nonlocal',' not','or','Pass','Raise','return','Try',' while',' with','yield']
Comments

Single-line comments in Python begin with #

# !/usr/bin/env python # Coding=utf-8 # First line Comment Print ("Hello world! ")  # First line Comment """ Multi-line comments """ " " Multi-line comments " "
Line and indent

In Python, you do not need braces "{}" to represent blocks of code by using indentation, and the number of spaces you indent is variable, but the number of indentation spaces for the same code block statement must be the same.

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

If indentation is inconsistent, it can result in a run error

#!/usr/bin/python3#-*-coding:utf-8-*-#file name: test.pyifTrue:Print("Answer")    Print("True")Else:    Print("Answer")    #no strict indentation, maintained at execution time       Print("False")
" /****/**** ", line    print ("False")    ^  indentationerror:unexpected Indent
Single Statement multiple lines

Python usually writes a single statement on one line, but if the statement is long, you can use the backslash "\" to implement a multiline statement, for example:

" a "  "b" "c" = A +      +     C

In "[]", "{}", "()" Do not need to use the backslash "\", for example:

A = {"a""b",     "C"  "" "keeplive" "" "" "homework" 
Data type
    • Digital
    • Boolean value
    • String
    • List
    • Dictionary
    • Meta-group
Blank Line

A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The entry of the class and function is also separated by a line of blank lines to highlight the beginning of the function entry. Blank lines are different from code indentation, and empty lines are not part of the Python syntax. The Python interpreter does not error when writing without inserting blank lines. But a blank line can separate two different functions or meanings of code to facilitate later maintenance and refactoring.

Note: Blank lines are also part of program code

Multi-statement line

In Python you can use multiple statements in the same row, separated by semicolons ";" between statements.

The following example:

# !/usr/bin/python3 Import ' Runoob ' ' \ n ')

Multiple statements form a code group

Indenting the same set of statements constitutes a block of code that we call code groups. Compound statements such as if, while, Def, class, the first line begins with a keyword, ends with a colon ":", and a line or long code after the line forms a code group. We make the first line and the following code group A clause (clause).

The following example:

if expression:    suiteelif  expression:    Else  :    Suite
. pyc File

When you execute Python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode generated after the Python interpreter was compiled

Note: Code is compiled to generate bytecode, and bytecode can be obtained by deserializing the code

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.