Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python basic syntax

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use the basic grammar of Python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Introduction to Python

Python is an interpretive, object-oriented, dynamic data type high-level programming language

Since it is an interpretive language, and CCompact + is a compiled language, it will certainly run much slower. Interpretive language exists relative to compiled language, its source code is not directly translated into machine language, but first translated into intermediate code, and then the interpreter interprets and runs the intermediate code.

Python is a high-level scripting language that combines interpretation, compilation, interactivity and object-oriented.

The design of Python is highly readable. Compared with other languages, it often uses English keywords and some punctuation marks. It has more distinctive grammatical structures than other languages.

Python is an interpreted language: this means that there is no compilation in the development process. Similar to PHP and Perl languages.

Python is an interactive language: this means that you can execute code directly after a Python prompt >.

Python is an object-oriented language: this means that Python supports object-oriented styles or code-encapsulated programming techniques.

Python is a beginner's language: Python is a great language for beginner programmers, supporting a wide range of application development, from simple word processing to WWW browsers to games

Basic syntax of Python

There are no & & and | | operators in python, and they are replaced by English and and or. Other operators have not changed

1 line and indent

The biggest difference between learning Python and other languages is that Python's code blocks do not use curly braces {} to control classes, functions, and other logical judgments.

The amount of indented white space is variable, but all code block statements must contain the same amount of indented white space, which must be strictly enforced.

The following example is indented to four spaces:

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

The following code will execute the error:

Example

If True: print ("Answer") print ("True") else: print ("Answer") # is not strictly indented and will error print ("False") during execution

If you execute the above code, the following error reminder will appear:

File "test.py", line 11 print ("False") ^ IndentationError: unindent does not match any outer indentation levelIndentationError: unindent does not match any outer indentation level

The error shows that the indentation you use is inconsistent, some tab indentation, some space indentation, just change to consistent.

If it is an IndentationError: unexpected indent error, then the python compiler is telling you, "Hi, man, there is something wrong with the format in your file. It may be that tab and whitespace are not aligned." all python is very strict with the format. This is a big difference. It is easy to make mistakes in the beginning of school.

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

It is recommended that you use a single tab or two or four spaces in each indentation level. Remember not to mix them.

Multiline statement

A new line is usually used as the Terminator in a Python statement. It is a semicolon in CAccord +; as the Terminator of a statement, this is a big difference, and it is easy to make mistakes in the beginning of the class.

However, we can use a slash () to divide the statement of one line into multiple lines, as follows:

Total = item_one + item_two + item_three

If the statement contains [], {} or () parentheses, you do not need to use multiline connectors. The following is an example:

Python quotation marks

Python can use quotation marks ('), double quotation marks ("), or triple quotation marks (''or") to represent a string, and the beginning and end of the quotation marks must be of the same type.

Three quotation marks can be made up of multiple lines and write a shortcut syntax for multiple lines of text, which is often used as a document string and is used as a comment at a specific location in the file.

Word = 'word'sentence = "this is a sentence." paragraph = "" this is a paragraph. Contains multiple statements "2 Python comments

Single-line comments in python begin with #.

Example

The first comment

Output result:

Hello, Python!

Comments can be made at the end of a statement or expression line:

Name = "Madisetti" # this is a comment

Multiline comments in python use three single quotation marks ('') or three double quotation marks ("").

Instance''this is a multiline comment, using single quotation marks. This is a multiline comment, using single quotes. This is a multiline comment, using single quotation marks. "this is a multiline comment, using double quotes." 3 Python blank line

Functions or methods of a class are separated by blank lines to indicate the beginning of a new piece of code. The class and the function entry are also separated by a blank line to highlight the beginning of the function entry.

Blank lines, unlike code indentation, are not part of the Python syntax. No blank lines are inserted when writing, and the Python interpreter runs without error. However, the function of blank lines is to separate two pieces of code with different functions or meanings, which is convenient for future code maintenance or refactoring.

Remember: blank lines are also part of the program code.

4 multiple statements are displayed on the same line

Python can use multiple statements on the same line, separated by semicolons (;). Here is a simple example:

Import sys; x = 'w3ccommodity; sys.stdout.write (x +')

Execute the above code, and the input result is:

W3cschool

5 print output

The default output of print is newline, and you need to put a comma at the end of the variable if you want to achieve non-line wrapping.

X = "a"

Y = "b"

# Line break output print xprint yprint'-'# No line break output print x print yMagi # No line break output print x Magi y

The execution result of the above example is:

A

B

-

A b a b

More than 6 statements form a code group

Indent the same set of statements to form a code block, which we call a code group.

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

We call the first line and the following code group a clause (clause).

The following is an example:

If expression: suite elif expression: suite else: suite, this is the end of the introduction to how to use Python basic Grammar. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report