Python Basic Syntax

Source: Internet
Author: User

PythonBasic Syntax

The Python language has many similarities with languages such as Perl,c and Java. However, there are some differences.

The first Python program
E:\python>pythonpython 3.3.5 (v3.3.5:62cf4e77f785, Mar  9, 10:37:12) [MSC v.1600 + bit (Intel)] on Win32type " Help "," copyright "," credits "or" license "for more information.>>> print (" Hello python ") Hello python


E:\python>python test.py hello,python!

Two ways: Interactive programming and scripting are programming

Python identifiers

Identifiers: Identifiers are composed of letters, numbers, and underscores. But you can't start with numbers.

Identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance.

1: A single underscore (_foo) represents a class attribute that cannot be accessed directly , and must be accessed through the interface provided by the class, and cannot be imported with "from XXX import *";

2: A double underscore (__foo) represents a private member of a class;

3: The __foo__ that begins and ends with a double underscore represents a special method -specific identifier for Python, such as __init__ (), which represents the constructor of a class.

Python reserved characters

The following list shows the reserved words in Python. These reserved words cannot be used as constants or variables, or as any other identifier name.

All Python keywords contain only lowercase letters.

and Exec Not
Assert Finally Or
Break For Pass
Class From Print
Continue Global Raise
Def If Return
Del Import Try
Elif Inch While
Else Is With
Except Lambda Yield

All the keywords are lowercase

Python indent

The biggest difference between learning Python and other languages is that Python's code block does not use curly braces ({}) to control classes, functions, and other logical judgments. Python's most distinctive feature is the use of indentation to write modules .

The amount of whitespace indented is variable, but all code block statements must contain the same amount of indentation whitespace , which must be strictly enforced. As shown below:

If true:    print "True" Else:  print "False"

  

The following code will execute the error:

If true: print "    Answer"    print "True" Else:    print "Answer"  print "False"

  

Therefore, you must use the same number of indent spaces in a Python code block.

Multi-line statements for Python

A new line is generally used as the Terminator for a statement in a Python statement.

But we can use a slash ( \) to divide a line of statements into multiple lines of display, as follows:

Total = Item_one + \         item_two +         Item_three

  

The statement contains [], {}, or () parentheses do not need to use a multiline connector. The following example:

days = [' Monday ', ' Tuesday ', ' Wednesday ',        ' Thursday ', ' Friday ']

  

Python Quotes

Python receives single quotation marks ('), double quotation marks ("), and three quotation marks (" ' "" ") to denote the string, and the beginning of the quotation mark must be the same type as the end.

Where three quotation marks can be composed of multiple lines, writing the shortcut syntax of multiple lines of text, common language file strings, at a specific location of the file, as a comment.

Word = ' word ' sentence = "This is a sentence." Paragraph = "" "This is a paragraph. It ismade up of multiple lines and sentences. "" "

  

Python comments

A single-line comment in Python starts with #.

Python does not have a block comment, so now the recommended multiline comment is the #比如:

# First Commentprint "Hello, python!";  # second Comment

Output Result:

Hello, python!.

Annotations can be at the end of a statement or an expression line:

Name = "Madisetti" # this is again comment

  

Multiple reviews:

# This was a comment.# this is a comment, too.# this is a comment, and too.# I said that already.

  

Python Empty Line

A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The class and function entries are 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. When you write without inserting a blank line, the Python interpreter runs without errors. However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.

PS: A blank line is also part of the program code.

Show multiple statements on the same line

Python can use multiple statements in the same row, with semicolons (;) split between statements, and here's a simple example:

Import SYS; x = ' Foo '; Sys.stdout.write (x + ' \ n ')

  

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

The following example:

If expression:    suite elif expression:     Suite  Else:     
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-h usage:python [option] ... [-C cmd |-M mod | file |-] [Arg] ... Options and arguments (and corresponding environment variables):-C Cmd:program passed in As String (terminates option L IST)     -D: Debug output from parser (also pythondebug=x)-e     : Ignore environment variables (such as PYTHONPATH)-h< c2/>: Print this help message and exit  

  

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.