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

What is the basic syntax of Python?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What is the basic syntax of Python? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

As a computer language, Python naturally has its own grammatical rules, just like English and Chinese have their own grammatical rules. The syntax of Python is famous for its simplicity. It defines its syntax in terms of lines, indents, comments, identifiers, reserved keywords, and so on.

Let's look at a piece of Python code first:

The line of Python

In Python, rows are divided into logical lines and physical lines. A physical line is counted as a line at the end of the line Terminator (\ n), and usually a physical line is a logical line. But sometimes, a logical line can contain multiple physical lines, such as the following code. If an expression is too long, we write it with multiple lines and connect with a slash (\):

Whole = 'This is an example of too long string' +\' which should be splited into multi-lines' +\ to show it'print (whole)

If the statement contains parentheses (), [], {}, you don't need to use a slash as a concatenator, such as:

The # list element can be written on multiple lines months = ['January',' Februray', 'March',' April', 'June',' July', 'August',' September', 'October',' November', 'December'] # the string is too long or it can be written on multiple lines through (). # this feature is useful for writing very long strings (such as SQL statements) sql = ('select id, name, age, height from' 'students where id > 100')

Blank line

A blank line is also a line (empty content) and is part of the code. It is often used to split code blocks to facilitate code maintenance, which is defined in detail in this code specification.

Multiple statements in a single line

The Python syntax allows multiple statements to exist in a line, separated by semicolons (;), but this is strongly discouraged in the coding specification, and one statement per line is clearer.

# legal but not recommended single-line multi-statement writing print ('hi'); a = 10; b = 20; print (aqb) indentation

One of the obvious differences between Python syntax and other languages is that code blocks are represented by indentation, rather than braces {}, such as C * C +, Java, and so on.

However, indentation does not strictly restrict characters, which can be the Tab key or multiple spaces. But indentation must be the same in the same source file (.py file), that is, either use the Tab key, or use two spaces, or all use four spaces. The official coding specification (PEP 8) specifies that four spaces are used as an indent.

If the indentation is inconsistent, an error will be reported: IndentationError. For example, the following code:

#! / usr/bin/env python3# coding:utf-8# File: demo.py# indentation is confused, execution Times error a = 10if a > 10: print ('a > 10') else: print ('a

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