Python basic syntax

Source: Internet
Author: User

Codingby default, Python 3 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-*-
Identifier

Identifiers are composed of letters, numbers, underscores

Identifiers can include english, numerals, and underscores (_), but cannot start with a number

Python can display multiple statements on the same line, by using semicolons ;

The first character must be a letter in the alphabet or an underscore ' _ '.

The other parts of the identifier are composed of letters, numbers, and Underscores.

Identifiers are case Sensitive.

In Python 3, non--ascii identifiers are allowed as Well.

Python reserved word

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.

Comments

Single-line comments in Python start with #

Multiline comments with three single quotes can also be three double quotes, but must match

Line and indent

Python's Most distinctive feature is the use of indentation to represent a block of code, without the use of Braces.

The number of spaces indented is mutable, but the statement of the same block must contain the same number of indent spaces

A blank line is also part of the program code

Data type

There are four types of python: integers, long integers, floating-point numbers, and complex numbers

integers, such as 1

Long integers are relatively large integers.

Floating-point numbers such as 1.23, 3E-2

Complex numbers such as 1 + 2j, 1.1 + 2.2j

Show multiple statements on the same line

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

Print output

Print default output is newline, if you want to implement no line break need to add end= "" at The end of the variable

Print( x,End="") must be separated by commas before opening
Wait for user input

Executing the following program will wait for user input after pressing enter:

Input("\ n \ nthe exit after pressing the Enter Key.") ")

In the above code, "\ n" will output two new blank lines before the result Output. Once the user presses the key, the program Exits.

Multiple statements form a code group

Indenting the same set of statements constitutes a block of code, which we call the code GROUP.

Compound statements such as if, while, def, and class, The first line begins with a keyword, ends with a colon (:), and one or more lines of code after that line form the code GROUP.

We refer to the first line and the following code group as a clause (clause).

Cases:

If:    Suiteelif:Else: Suite
Import and From...import

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

Import the entire module (somemodule) in the format: import somemodule

Import a function from a module in the format: from somemodule import somefunction

Import multiple functions from a module in the format: from somemodule import firstfunc, secondfunc, thirdfunc

Import all functions in a module in the format: from somemodule import *

Importing the SYS moduleimport sys print('================python import mode========================== '); print ( " command line parameters are: ' for i in sys argv: < Span class= "hl-brackets" > print (i) print (\n python path is ' ,< Span class= "hl-identifier" >sys. Path Importing the Argv,path member of the SYS module from sys import argv,path # import specific members print ( "================python from import======== =========================== ' ) Print('path:',path) # Because the path member has already been imported, there is no need to add sys.path when referencing it here Command-Line arguments

Many programs can perform some operations to see some basic letters, and Python can use the-h parameter to view the Help information for each parameter:

$ python-Husage:Python[Option] ... [-C-cmd| -M MoD|File| -] [Arg] ...Options andArguments(andcorresponding environment variables):-C-cmd:Program passedInch As String (Terminates option list)-d : debug output from parser  (also pythondebug =x) -e : Ignore environment variables  (such as Pythonpath) - h : printthis help message and  Exit                  

Key Points:

? You can not write the first line under Windows note:#!/usr/bin/python3

Python basic syntax

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.