You can learn Python articles without reading online classes (the third day)

Why_ does_ it_ work 2021-11-25 15:31:52

Next to the previous one You don't have to watch online classes to learn python The article ( the second day )_Why_does_it_work The blog of -CSDN Blog

When it comes to statements , Then we should know something python

python The most distinctive feature is the use of indentation to represent code blocks , You don't need braces  {} ;

Lines and indents

if True:

Yes c The basis of language, let's look at this string of code ,python There is no harmony. c Languages also have semicolons and braces. In fact, we can see from the last article , that python How to distinguish ------ Indent

  • The number of indented spaces is variable , But the statements of the same code block must contain the same number of indented spaces ;

  • Generally, four spaces are used to indent the beginning of the line ;

  • Tabs are not recommended tab key , Because different text editors are inconsistent , If you want to run across platforms , Not recommended ;

  • python End of sentence , There is no need to add at the end A semicolon ; 

So here comes the question , What if the two lines are the same sentence , Is in the python Standard formats are generally used in languages to distinguish content so that code can be understood , Then you have to write a string of code on the same line , If you can't write it down, use the symbol \

# "\" example
test = test_one + \
test_two + \

contrary , If you want to write multiple strings of code on the same line, how to distinguish ? use ;

a=1; b="hello world"; c="Why_does_it_work"

Understand the basic sentences clearly , that

Judgement sentence

1.if Conditional sentence

if The conditions to judge :
If it is correct, the operation to be performed

It should be noted that c Language has a basic fixed format ,python There are no complicated symbols, but use indentation ,if In the statement, when the conditions to be judged are written and the line breaks are written, pay attention to , To click tab Button or four spaces ​

It should be noted that the judgment sentence and the indented part are a whole , Step by step from top to next

2.if-else The format of

if The conditions to judge :
Correct operation
Wrong operation

 3.elif sentence

if Conditions 1:
Meet the conditions 1 The operation made
elif Condition 2 :
Meet the conditions 2 Make a statement
elif Condition 3 :
Meet the conditions 3 Make a statement
Not meeting the conditions 123 Executed statements 

This is not in order , At the same time

4.if nesting

if Nesting and elif Totally different ,elif There are indentations in the. They are done at the same time , however if Have priority

if Nesting is and c The language is the same , Judge one by one

Loop statement

stay python There are three kinds of statements in language , Sequential statements , Branch statement , Loop statement

In fact, the statements that operate step by step at the beginning of the article are called sequential statements , stay if-else,elif It's a branch statement

The loop statement is also relatively simple

while loop

abc = 1
while abc<9:
abc+= 1

for loop

for i in range(1,101):
print(i , end = " ")

for Variable in range(10):
The code that the loop needs to execute

range Usage of :
range(stop): 0~stop-1
range(start,stop): start~stop-1
range(start,stop,step): start~stop step( step )

[0, 1, 2, 3, 4]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(1,11,2)        ## Take out the odd number
[1, 3, 5, 7, 9]
>>> range(0,11,2)        ## Take out even numbers
[0, 2, 4, 6, 8, 10]

break sentence ,continue sentence ,pass sentence , We'll talk about it later

I've devoted an issue to circulation , Actually, I know the structure , know python Indentation , and c Language is basically similar

About c Language cycle , What I want to say is :_Why_does_it_work The blog of -CSDN Blog

In the next article, I will train on some topics about sentences and share them with you , I hope you will continue to pay attention !

Please bring the original link to reprint ,thank
Similar articles