Little white Beginners Learn python basic grammar

Source: Internet
Author: User

Python has been used more recently in development, so the basic syntax for Python is summed up for beginners to learn.

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 (2 bytes), which is:2 **16=65536

UTF-8

UTF-8(8-bit Unicode Transformation Format) is a variable-length character encoding for Unicode. Created by Ken Thompson in 1992 year. The RFC 3629 has now been standardized . UTF-8 encoding UNICODE with 1 to 4 bytes Character. 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 theASCII Code use 1 bytes, the European characters use 2 Bytes, East Asian characters using 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, and is accessed through the interface provided by the class and cannot be imported with a "from * * * * * * * *" , which begins with a double underscore ( __foo) represents a private member of a class; a double underscore (__foo__) that begins and ends with a python A special method for a specific identifier, such as __int__(), represents the constructor of the class.

Python reserved word

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

>>> Import keyword>>> Keyword.kwlist

[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' fi Nally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' 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!") # The first line comments "" "

MultiRow

Note " " "

MultiRow

Note " "

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.py

If True:

Print ("Answer")

Print ("True") Else:

Print ("Answer")

# No strict indentation, maintained at execution time

Print ("False")

Le "/****/****", line 10

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 = "a"

b = "B"

c = "C"

ABC = A +

B +

C

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

A = {"A", "B",

"C"}

B = "" "Keep

Live "" "

C = "Home"

Work ""

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 SYS; x = ' Runoob '; Sys.stdout.write (x + ' \ 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, with a colon ":" ends with a line or long code after the line that constitutes the code group. We make the first line and the following code group A clause (clause).

The following example:

If expression:

Suiteelif expression:

Suite ELSE:

Suite

. pyc File

When you execute a Python Code, if you import a different . py file, a . PYc is automatically generated during execution. File, 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

From: Blog Park / Kang

Little white Beginners Learn python basic grammar

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.