(3) BASIC program structure and basic program structure

Source: Internet
Author: User
Tags natural string

(3) BASIC program structure and basic program structure

1. Encoding

By default, Python 3 source code files are UTF-8 encoded, and all strings are unicode strings. Of course, you can also specify different codes for the source code file:

 # -*- coding: cp-1252 -*-

 

2. identifier

  • The first character must be a letter or underscore '_' in the alphabet '_'.
  • Other parts of an identifier are composed of letters, numbers, and underscores.
  • The identifier is case sensitive.

In Python 3, non-ASCII identifiers are also allowed. There is an unwritten rule:

  • Constants are capitalized.
  • The global variable starts with two underscores.
  • Variables and class names are named by the camper method.



3. Reserved characters in python

Reserved Words are keywords. We cannot use them as any identifier names. The standard Python Library provides a keyword module that can output all the keywords of the current version:

>>> import keyword>>> keyword.kwlist['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

 

4. Notes

Annotations are a means of adding texts to the Code for reading. Annotations are not executed by computers. There are two ways to note

  • Single line comment
  • Multi-line comment

In Python, a single line comment starts with #. The example is as follows:

Print ('Hello World') # This is a note

Run the above Code and the output result is:

Hello world

 

Multiple # numbers can be used for multi-line comments, as well as ''' and """:

# First comment # second comment ''' third comment fourth comment ''' "fifth comment Sixth Comment" print ("Hello World! ")

 

Run the above Code and the output result is:

Hello world

 

 

5. Lines and indentation

The most distinctive feature of python is that it uses indentation to represent code blocks without braces ({}). The number of indented spaces is variable, but the statement of the same code block must contain the same number of indented spaces. Example:

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

The following code does not match the number of spaces in the last line of the statement, causing a running error:

If True: print ("Answer") print ("True") else: print ("Answer") print ("False") # inconsistent indentation leads to running errors

Because the indentation of the above program is inconsistent, an error similar to the following will occur after execution:

A group of statements with the same indentation constitute a code block, which we call a code group. Usually we press the Tab key or four spaces to indent. If we use an IDE such as PyCharm, you just press enter and the IDE will automatically indent you.

 

6. Multi-line statements

Python usually writes a statement in one line, but if the statement is long, we can use a backslash (\) to implement multiple statements. For example:

print('Hello + \World')

Run the above Code and output:

Hello World

Multi-line statements in [], {}, or () do not need to use a backslash (\). For example:

Day = ['monday', 'tues', 'wedned', 'thurs', 'Friday', 'satur', 'sunday']

 

7. Data Types

Python3 has six standard data types:

  • Number)
  • String (String)
  • List)
  • Tuple (tuples)
  • Sets)
  • Dictionary)

We will discuss it one by one later.

 

8. Variable type

In Python, a variable is a variable and has no type. The "type" we call is the type of the object in memory referred to by the variable.

Age = 100 # integer variable height = 1.75 # Floating Point variable name = "roy" # string

Multiple values:

a = b = c = 100

In the preceding example, three variables of an integer object are allocated to the same memory space. Multiple values can also be as follows:

a,b,c = 1,2,'roy'

In the above example, 1 is assigned to a, 2 to B, and roy to c. The built-in type () function can be used to query the object type referred to by the variable (as described later ).

 

9. String

  • In python, single quotes and double quotes are identical.
  • You can use three quotation marks (''' or ") to specify a multi-line string.
  • Escape Character '\'
  • Natural string, by adding r or R before the string. For example, if r "this is a line with \ n", \ n is displayed, not a line break.
  • Python allows you to process unicode strings with the prefix u or U, such as u "this is an unicode string ".
  • The string is unchangeable.
  • Cascade strings literally. For example, "this" "is" "string" is automatically converted to this is string.

 

10. Empty rows

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 runs without errors. However, empty lines are used to separate two sections of code with different functions or meanings to facilitate code maintenance or reconstruction in the future.

Remember: Empty lines are part of the program code.

 

11. Multiple statements are displayed in the same row.

Python can use multiple statements in the same line and use semicolons (;) to separate them. The following is a simple example:

Print ("hello world"); print ('second sentence ')

Run the above Code and output:

Hello world

 

Finally, the python syntax diagram is attached.

 

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.