Python basics-the first python program (basic syntax 1)

Novice programming:

1. Spyder interface: programs can be written on both sides

The left interface box: enter the program print("hello word") and press the F5 shortcut key to run, and the result will be displayed in the right interface.

Enter on the left:

Press F5 on the right to get the result:

Many small programs can be written on the left, and the results of these programs will be output when F5 is running.

Right interface frame: input the program and press Enter to display the result directly.

2. Use # for comments, multi-line comments are allowed, if you need multi-line comments, write them on the left interface.

 

 

Multi-line comments can use multiple # signs, as well as''' and """

 

3. Lines and indentation

Python uses indentation to represent code blocks. No need for braces {}

The number of indented spaces can be changed, but statements in the same code block must contain the same number of indented spaces.

if True:
    print("1")
else:
    print("2")
    
1

If the number of spaces is not the same, it will cause operation errors. Examples are as follows:

The indentation refers to the meaning of shrinking inward. If the indentation number is moved forward, an error will be reported, and the backward movement will not affect it.

if True:
    print("1")
else:
print("2")   #缩进数不同
  File "<ipython-input-19-ce72f1213308>", line 4
    print("2")

if True:
      print("1")   
else:
                 print("2")    #后移不叫缩进
                 
1
if True:
print("1")   #缩进数不同
else:
       print("2")
  File "<ipython-input-21-15c4b3ebf0db>", line 2
    print("1")
    ^
IndentationError: expected an indented block
if True:
    print("1")
    else:       #if/else的位置:对齐
        print("2")
  File "<ipython-input-17-8d87826d7c98>", line 3
    else:
    ^
SyntaxError: invalid syntax

4. Multi-line statements

(1) Python statements are usually written one line at a time. If the statement is very long, you can use the (\) backslash to achieve it.

total=1+\
      2+\
      3

(2) Multi-line statements in [], {}, or () do not need to use backslashes (\)

total=['1','2','3']

total={'w','o','r','d'}

total=('1','2','3')

 

Guess you like

Origin blog.csdn.net/zhangxue1232/article/details/108984364