-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial 5
Python can be used automate analysis. However, the easiest way to get to grips with coding in python is to write python files and then execute them.
A Python file is any filename with the extension .py
. To run a file from the command line, make sure you are in the same directory as the file and type python <filename>.py
.
Iterating a task in python can be done with a for loop. Python loops use something called a variable (in this case, "x"). Here, the variable x loops through the list of strings. The command print
simply prints the output to the screen so you can see the result.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Branch structure design, a structure that is very important to us in programming. This structure can determine the next step in the execution process by assuming that certain specific conditions are satisfied. Common branching structures include single-branch, double-branch and multi-branch structures.
if
The if function is used to determine if the function is satisfied, and to execute it if it is satisfied, otherwise it is not executed. It is important to note that the executed statement needs to be indented relative to the conditional statement
n=2
if n > 0:
print("n is a positive number")
...
n is a positive number
if-else
This function divides the situation into two categories, executes the statement below if
if it matches, otherwise executes the statement below else
, and just like single-branch, you need to notice indentation.
n=-5
if n > 0:
print("n is a positive number")
else:
print("n is a negative number")
...
n is a positive number
if-elif-else
The multi-branch structure is an extension of the two-branch structure; interestingly, you can have many elif
branches, which will always look for a branch that satisfies the condition and jump out of the whole structure once found.
score=70
if score > 80:
print("very good")
elif 60 < score < 80:
print("good")
else:
print("bad")
...
good
With the break statement we can stop the loop before it has looped through all the items:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Congratulations you have finished all of the BMD Python tutorials!