Basic Python syntax and python syntax

Source: Internet
Author: User

Basic Python syntax and python syntax
Python internal execution process

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:

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

When the python 2 interpreter loads the code in the. py file, it will encode the content (default ASCII)

ASCII is a computer coding system based on Latin letters. It is mainly used to display modern English and other Western European languages. It can only be expressed in 8 bits (1 byte), that is: 2x8 = 256. all ASCII codes can contain a maximum of 256 characters.

>>> Import keyword >>> keyword. kwlist ['false', 'none', 'true', 'and', 'as', 'assert ', 'Break', 'class', 'contine ', 'def', 'del ', 'elif', 'else', 'partition t', 'Finally', 'for ', 'from', 'global', 'if ', 'import', 'in', 'is ', 'lambda', 'nonlocal', 'not ', 'or', 'pass', 'raise', 'Return ', 'try', 'wait', 'with', 'yield ']

Note
  

A single line comment in Python starts #

#! /Usr/bin/env python # coding = UTF-8 # print ("hello world! ") # Comment on the first line" multi-line comment "''' multi-line comment '''
Line and indent
  

In python, indentation is used to represent the code block. Braces "{}" are not required. The number of indentations is variable, but the number of indentations of the same Code BLOCK statement must be consistent.

if True:    print("True")else:    print("False")

If the indentation is inconsistent, a running error occurs.

#! /Usr/bin/python3 #-*-coding: UTF-8-*-# file name: test. pyif True: print ("Answer") print ("True") else: print ("Answer") # no strict indentation, print ("False") during execution ")
le "/****/****", line 10    print("False")    ^IndentationError: unexpected indent
Multiple rows in a single statement
  

Python usually writes a statement in one line, but if the statement is long, you can use the Backslash "\" to implement multiple statements. For example:

A = "a"B = "b"C = "c"abc = A + \    B + \    C

You do not need to use the Backslash "\" in "[]", "{}", and "()". For example:

A = {"a", "b",     "c"}B = """keeplive"""C = '''homework'''
Data Type
  • Number
  • Boolean Value
  • String
  • List
  • Dictionary
  • Tuples
Empty row
  

Functions or methods of classes are separated by blank lines, indicating the beginning of a new code. The class and function entry are also separated by a blank line to highlight the beginning of the function entry. Unlike code indentation, empty lines are not part of python syntax. No blank lines are inserted during writing, and the python interpreter does not report errors. However, empty lines can separate two sections of code with different functions or meanings to facilitate future maintenance and reconstruction.

Note: Empty lines are part of the program code.

Multiple statement lines
  

In python, multiple statements can be used in the same line, separated by semicolons.

Example:

#!/usr/bin/python3import sys; x = 'runoob'; sys.stdout.write(x + '\n')
Multiple statements form a code group
  

A group of statements with the same indentation constitute a code block, which is called a code group. For a composite statement such as if, while, def, and class, the first line starts with a keyword and ends with a colon ":". A code group consists of one or more lines after the row. We create the first line and the code group following it as a clause ).

Example:

if expression :    suiteelif expression :    suite else :    suite
. Pyc File
  

When you run the Python code. py file, a file with the same name is automatically generated during execution. pyc file, which is the bytecode generated after the python interpreter is compiled.

Note: After the code is compiled, it can generate bytecode. After the bytecode is decompiled, the code can also be obtained.

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.