SoFunction
Updated on 2024-10-28

Python Sequential Results, Selection Structures, and Loop Structures Explained

preamble

Today is going to be an introduction to the various simple structures of python (sequential, if--else and various loops) This is the current progress of Python Basics

Let's go!

I. Sequential structure

A sequential structure is one that is executed from front to back in the order in which it is written. This type of structure is the simplest and most basic.

The following code is executed from the beginning down. Each statement is executed

print("------ Cultivation Experiences in the Three Worlds Period --- ---")
print('\t',"The day after.")
print('\t',"The firstborn.")
print('\t',"Purple-bellied friar.")
print('\t',"The Million Elephant Man.")
print('\t',"The Primordial Taoist.")
print('\t',"Anti-Virtual Immortal")
print('\t',"Heavenly Immortals and Gods.")
print("———————————————————————————")

Run results:

II. Choice of structure

----elif----else statements

Ever since I wanted to get started in IT learning, a phrase I once heard kept popping up inside my head

If you study hard in college and code well, you'll get an offer from a big company and go to the top of your life.

If: if you don't study hard in college, then you can only go home and sell sweet potatoes after graduation (crying)

Haha, if you know who said it, don't tell!

Events like this are a choice structure, if you do well in college, you get a good offer, otherwise you graduate and have to sell sweet potatoes

Implemented in python code as follows

Choose=input("Are you going to study hard: Y/N")
if Choose=='Y':
    print("Get the offer.")
else:
    print("Go home and sell sweet potatoes.")

The syntactic composition of the if--else statement can be seen in this piece of code

if Judgment Condition.

Execution of statement 1 (to be preceded by indentation)

else :

Execution of statement 2 (to be preceded by indentation)

Execute statement 1 if the judgment condition is valid, and statement 2 if it is not.

One point to note here is that as long as the statement following the if/else statement is indented, the system defaults to the statement that needs to be executed when the if/else is set up, and executes it as an executable statement all the way up to the unindented statement line.

When there is more than one choice, as in the case of grading questions in the category of test scores

score=int(input("Please enter your score."))
if score>=90 and score<=100:
    print("You have a grade of A.")
elif score >= 80 and score <= 89:
      print("Your grade is B.")
elif score >= 70 and score <= 79:
      print("You have a grade of C.")
elif score >= 60 and score <= 69:
      print("Your grade is D.")
else:
    print("You're getting a failing grade.")

The elif here is the C equivalent of else if.

The meaning of elif is that if the if condition does not hold, then another judgment is made to see if another condition is met, and if so, the corresponding statement is executed.

It differs from else in that the scope of else is all cases in which the if judgment condition does not hold, whereas the elif statement gives other choices of cases

Haha, one more example about studying for exams

Choose=input("Are you going to study hard your freshman and sophomore year: y/n/ I'm going to graduate school")
if Choose=='Y':
    print("Get the offer.")
elif Choose=="I'm going to graduate school.":
    print("Study hard after grad school and get offers too")
else:
    print("Go home and sell sweet potatoes.")

Multiple choices are made when it comes to  elif  express

2. Conditional expressions

statement x if judgment condition else statement y

If the judgment condition holds, execute the statement x

Otherwise, execute the statement y

III. Circular structure

function (math.)

The role of the range function: in the specified range to generate a sequence with step size

range(parameter1,parameter2,parameter3)

Parameter 1 is the starting point of the range

Parameter 2 is the end of the range

Parameter 3 is the step size between generated sequences, defaults to 1 when omitted

circulate

The while loop is usually used when the number of loops is not fixed.

The syntax format is

while Conditional expressions.

Loop body (conditional expression execution)

When the conditional expression holds, the loop body is executed; otherwise, the loop is exited.

Example 1: Summing 0-100

i=0
ret=0
while i<=100:
    ret+=i
    i+=1
print(ret)

Example 2: Finding the sum of even numbers between 0 and 100

i=1
sum=0
while i<=100:
    if i%2==0:
        sum+=i
    i+=1
print(sum)

3. for----in loop

The for----in loop isn't really that well thought out, but the conversion from C isn't that hard to think of either

The for----in loop is often used when the number of loops has already been determined.

Grammatical structure:

for variable in sequence

loop body

where the number of loops is the same as the number of elements in the sequence, independent of the value of the variable

When there are variables, the sequence elements are assigned sequentially to the variables before each execution of the loop body

When a variable is not necessary, you can write the variable position as _ (underscore), and the loop will still execute the sequence elements a number of times

The object traversed by the for----in loop must be an iterable object

If you print the number of daffodils between 100----900

i=0
wa_fl=0
z=0
for i in range(153,901,1):
    z=i
    wa_fl=0
    while z>0:
        wa_fl+=((z%10)**3)
        z//=10
    if wa_fl==i:
        print(i)

Code Parsing:

Control the number of loops with a for loop

The while loop controls finding each bit of this number and tripling and then summing

Each for loop makes a judgment to see if the number of daffodils is

IV. continue, break in the loop in the use of

break isJump directly out of the current loop

continue isSkips the remaining unexecuted statements in the loop body and proceeds immediately to the next loop condition determination.

The effect when using continue

i=1
while i<=5:
    if i%2==0 :
        i+=1
        continue
    print(i)
    i += 1

Effect when using break

i=1
while i<=5:
    if i%2==0 :
        i+=1
        break
    print(i)
    i += 1

The logic is a bit simpler, so you can study it for yourself

summarize

To this point this article on Python sequential results, selection structure and loop structure explains the article is introduced to this, more related python simple structure content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!