SoFunction
Updated on 2024-12-20

Learning Python with Laoqi's journey from if to start statement

The average programming textbook is going to cover all the variable types and then the statements. This way of speaking, in fact, does not meet the characteristics of learning. Learning is all about incremental progress. At this point, I can be very blowing a pass, because I have been a teacher, studying education and teaching, sort of have a little bit of knowledge. So, here I go with the teaching statements.

What is a statement

Earlier, we have written some .py files that can be run in python. Those files, are programs that consist of statements.

In order to be able to rigorously articulate this concept, I'll still copy a passage from the Wikipedia entry: imperative programming

Command Programming(English (language):Imperative programming),is a programming paradigm that describes the behaviors that computers are required to make。Almost all computer hardware works on command.;Almost all computer hardware is designed to run machine code,written in imperative style。Higher-level imperative programming languages use variables and more complex statements,But still following the same paradigm。
Operational statements in general exhibit the behavior of performing operations on data in memory,The results are then stored in memory for later use。高级Command Programming语言更能处理复杂的表达式,May produce a combination of quadratic and functional calculations。


All high-level languages, in general, contain the following statement, and Python is no exception:

Loop statements: Allow some statements to run several times. Loops can be based on a default number of times to determine how many times to run these statements; or they can be run repeatedly until some condition changes.
Conditional Statements:Allows a block to be run only if certain conditions hold. Otherwise, the statements in the block are omitted, and the block continues to run with the statements that follow it.
Unconditional branching statements allow the running order to be shifted to other parts of the program. This includes jumps (called Goto in many languages), subroutines, and Procedures.
Loops, conditional branches, and unconditional branches are all control flows.

if statement

When it comes to statements, don't be intimidated. Look at the example below first:

if a==4:
  print "it is four"
else:
  print "it is no four"

Sentence by sentence explanation, pay attention to read the comments. Here to give listed as a reminder, in writing the program is by, be sure to write the necessary comments, and at the same time in reading the program, but also pay attention to read the comments.

if a==4:        # If the variable a==4 is true and a==4 is True, the
  print "it is four" # Print "it is four".
else:          # Otherwise, i.e., a == 4 is False, a == 4 is False, on the
  print "it is not four" #printable“it is not four”。

The above statements complete a conditional judgment, doing different things under different conditions. Therefore, if statements are often translated as "conditional statements".

Basic style structure of conditional statements:

if prerequisite1:
  Content of implementation1
elif prerequisite2:
  Content of implementation2
elif prerequisite3:
  Content of implementation3
else:
  Content of implementation4

The execution of content 1, content 2, etc., is called a statement block. elif is used when multiple conditions are used, and can be used without. It is also possible to have only if and no else.

Reminder: each implementation is indented with four spaces.

Example 1: input a number, and output the results of the input, if the number is greater than 10, then at the same time output is greater than 10, if small is, at the same time output tips less than 10, if equal to 10, the output of a sentence of praise.

From here on, our code is going to get closer and closer to a complex judgment process. In order to make our thinking clearer about the process of solving the above problem, we often draw flowcharts during program development. What is a flowchart, I say from another point of view, is to make the thinking process visualization, short for "thinking visualization". By the way, I have been promoting mind mapping since 2004, which is a thinking visualization tool. That's the end of the bragging. Look at the flow chart of this problem:

Once you understand the meaning in the flowchart, start writing the code, an example of the code is given below:

#! /usr/bin/env python
#coding:utf-8

print "Please enter any integer number:"

number = int(raw_input())  # The number entered via raw_input() is a string
              # Convert the string to an integer using int()

if number == 10:
  print "The number you have entered is: %d"%number
  print "You are SMART."
elif number > 10:
  print "The number you have entered is: %d"%number
  print "This number is more than 10."
elif number < 10:
  print "The number you have entered is: %d"%number
  print "This number is less than 10."
else:
  print "Are you a human?"  

Special reminder to see the attention of the audience, we have already used the raw_input () function, this is to get the information entered by the user in the interface, and through it to get a string type of data. This can be tested in IDLE in this way:

>>> a=raw_input()
10
>>> a  
'10'
>>> type(a)
<type 'str'>
>>> a=int(a)
>>> a
10
>>> type(a)
<type 'int'>

That a you just got is of type str, and if you convert it with int(), it becomes int.

It seems that int() can convert numbers of type word str to type int, similarly, is not the conclusion that str() can convert numbers of type int to type str. Suggested to look at the experiment.

The back of the above program, is based on the conditions for judgment, different conditions to do different things. Need to remind is in the conditions: number == 10, in order to read easily, in the number and == between a space is the best, similarly, there is also a back. Here 10, is an int type, number is also an int type.

I don't know if you understand the above program? If not, you can contact me through QQ, my QQ announcement: 26066913, or log on to my microblogging, microblogging to contact me, of course, you can also send an e-mail. I'll reply when I see your question. Interacting with me on github is most welcome.

Finally, I'll leave you with a practice question for your watchers:

Post-class exercise: develop a program for a number guessing game. That is, the program specifies a number in a range, for example, a number in the range 0 to 9, and the user guesses the size of the number specified by the program.

Please look at writing your own. We will discuss this later.

trivia

I don't know if you noticed, but that code above, there is a line at the beginning:

#! /usr/bin/env python
What does that mean?

This sentence starts with a # to indicate that it was not originally run in the program. The purpose of this sentence is to tell the machine to look for the python interpreter on that device, and the operating system uses the interpreter it finds to run the program code in the file. Some programs write /usr/bin python, indicating that the python interpreter is inside /usr/bin. However, if you write /usr/bin/env, it means that you have to search for the python interpreter through the system search path. The location of the interpreter may vary from system to system, so this approach makes the code more portable. By the way, the above is for the Unix family of operating systems. For windows, this statement is not true.