Lesson 5 Conditional execution in Python

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.33
List Price:  €91.90
You save:  €27.57
£55.09
List Price:  £78.70
You save:  £23.61
CA$95.29
List Price:  CA$136.14
You save:  CA$40.84
A$104.44
List Price:  A$149.21
You save:  A$44.76
S$94.20
List Price:  S$134.58
You save:  S$40.38
HK$546.10
List Price:  HK$780.18
You save:  HK$234.07
CHF 63.62
List Price:  CHF 90.89
You save:  CHF 27.27
NOK kr751.73
List Price:  NOK kr1,073.95
You save:  NOK kr322.21
DKK kr480.45
List Price:  DKK kr686.39
You save:  DKK kr205.93
NZ$114.09
List Price:  NZ$162.99
You save:  NZ$48.90
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,195.22
List Price:  ৳11,707.96
You save:  ৳3,512.74
₹5,830.20
List Price:  ₹8,329.22
You save:  ₹2,499.01
RM328.07
List Price:  RM468.70
You save:  RM140.62
₦102,885.30
List Price:  ₦146,985.30
You save:  ₦44,100
₨19,393.48
List Price:  ₨27,706.17
You save:  ₨8,312.68
฿2,536.99
List Price:  ฿3,624.43
You save:  ฿1,087.43
₺2,256.71
List Price:  ₺3,224.01
You save:  ₺967.30
B$357.27
List Price:  B$510.40
You save:  B$153.13
R1,265.02
List Price:  R1,807.25
You save:  R542.23
Лв125.89
List Price:  Лв179.86
You save:  Лв53.96
₩94,793.75
List Price:  ₩135,425.45
You save:  ₩40,631.70
₪259.28
List Price:  ₪370.41
You save:  ₪111.13
₱4,039.50
List Price:  ₱5,770.97
You save:  ₱1,731.46
¥10,895.69
List Price:  ¥15,565.94
You save:  ¥4,670.25
MX$1,162.43
List Price:  MX$1,660.69
You save:  MX$498.25
QR253.91
List Price:  QR362.75
You save:  QR108.83
P948.27
List Price:  P1,354.73
You save:  P406.46
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,283.23
List Price:  E£4,690.53
You save:  E£1,407.30
ብር4,009.02
List Price:  ብር5,727.42
You save:  ብር1,718.40
Kz59,310.71
List Price:  Kz84,733.22
You save:  Kz25,422.51
CLP$62,605.89
List Price:  CLP$89,440.82
You save:  CLP$26,834.93
CN¥505.55
List Price:  CN¥722.24
You save:  CN¥216.69
RD$4,078.44
List Price:  RD$5,826.59
You save:  RD$1,748.15
DA9,404.82
List Price:  DA13,436.03
You save:  DA4,031.21
FJ$155.95
List Price:  FJ$222.79
You save:  FJ$66.84
Q543.74
List Price:  Q776.81
You save:  Q233.06
GY$14,641.89
List Price:  GY$20,917.88
You save:  GY$6,275.99
ISK kr9,678.91
List Price:  ISK kr13,827.61
You save:  ISK kr4,148.70
DH693.20
List Price:  DH990.33
You save:  DH297.13
L1,231.95
List Price:  L1,760
You save:  L528.05
ден3,957.99
List Price:  ден5,654.52
You save:  ден1,696.52
MOP$562.43
List Price:  MOP$803.51
You save:  MOP$241.07
N$1,273.79
List Price:  N$1,819.78
You save:  N$545.99
C$2,572.13
List Price:  C$3,674.63
You save:  C$1,102.50
रु9,330.17
List Price:  रु13,329.38
You save:  रु3,999.21
S/260
List Price:  S/371.45
You save:  S/111.44
K271.59
List Price:  K388.01
You save:  K116.41
SAR262.46
List Price:  SAR374.96
You save:  SAR112.50
ZK1,777.13
List Price:  ZK2,538.86
You save:  ZK761.73
L320.28
List Price:  L457.57
You save:  L137.28
Kč1,590.03
List Price:  Kč2,271.57
You save:  Kč681.54
Ft24,925.85
List Price:  Ft35,609.88
You save:  Ft10,684.03
SEK kr750.08
List Price:  SEK kr1,071.59
You save:  SEK kr321.50
ARS$62,063.63
List Price:  ARS$88,666.13
You save:  ARS$26,602.50
Bs483.56
List Price:  Bs690.83
You save:  Bs207.27
COP$266,463.69
List Price:  COP$380,678.73
You save:  COP$114,215.04
₡35,817.30
List Price:  ₡51,169.76
You save:  ₡15,352.46
L1,723.87
List Price:  L2,462.78
You save:  L738.91
₲524,369.02
List Price:  ₲749,130.70
You save:  ₲224,761.68
$U2,697.33
List Price:  $U3,853.49
You save:  $U1,156.16
zł274.39
List Price:  zł392.01
You save:  zł117.61
Already have an account? Log In

Transcript

Conditional execution. This represents testing the porosity and falsity of an expression. Any numeric value other than zero is true, only the value zero is false. This small script above will only display faults. If you enter the value zero. For any other numeric value, you will get true.

If you enter a string or list, you will still get true. Only channels or empty list will be considered false conditional stages. program x is equal to five if x is smaller than 10. Print smaller if x is greater than 20, print bigger outs print and output smaller comma and as you can see, only two of the words were written. Since only these conditions were met. comparison operators, Boolean expressions query and respond with yes or no which allows us to control the question The program will read expressions that use the comparison operators evaluated by true false.

Yes, no comparison operators control the variables, but do not change them. Remember, the equal sign is used for assignment, you can find in the table the meaning of each of the signs. We have here a list of tests for x, if it is equal to greater than four, equal to five, and so on one way decisions, here's the line of thought. If x is equal to five, or to six, we also wrote print for each statement to observe the line of thought. We tested if it is equal to five printed that it is equal to five. Then we went to the second condition and printed the status to observe the problem actually tested X to six.

This method can be useful to debug a program intonation rules, increase indentation after you do Clear and f are for statement after the column. Maintain indentation to indicate the scope of the block lines that are affected by if or for return the intonation to the return of the decrease in level if or for to indicate the end of the block. blank lines are ignored. They do not affect indentation. Comments on a line are either ignored with regard to indentation, warning, disable tab, most text editors can convert tabs to spaces. Make sure to enable this function notepad plus plus, go to settings, preferences, language menu, tap Settings, Text Wrangler, go to Text Wrangler preferences editor defaults.

At what point the line is indented. It matters very much for Python. If you mix tabs and spaces, you might have indentation errors. Even if everything looks normal to you, please do this now while you're thinking about it. So that everything's fine in the future. Here's some examples of increased and decreased indentation, pick up the beginning and end of the blocks.

Purple increases, maintains after if for yellow arrows indicates decreases to indicate the end of the block. Think of the beginning and end of the blocks. Each block of code needs to have his own level of indentation nested decisions. They represent conditions that can be met inside another condition. an if statement inside another if statement. Let's say that x is equal to 42.

We take into consideration if x is greater than one. In this case, we print is more than one. Then we also consider if x is less than 100. And print lower than 100. If this condition is met, if either of the two conditions are not met, we print done. We also print done at the end If one or both conditions are true to waste decisions, sometimes we want to do one thing if the expression is true, and something else that the expression is false.

It's like a bifurcation on the road, we have to choose one path or the other, but not both. If x is less than two, we print not bigger. As we print bigger, two ways using the same situation as before can be done without instead of the second if multiline. This is when multiple options are considered if x is less than two, print small outs, if x is less than 10, print medium, and otherwise, print large. Here we have a second example, when x is equal to 20. multiple options when there isn't an option. In this case, we did not consider other possible options, just a few nothing else happens if any is not an any of the considered options.

And the case in the right we consider all of the possible options multitrack puzzles, which fine will never be printed on the screen. On the left side example, it will never print something else. Since any number is less than or higher than or equal to two, while on the right side at least one of the print statements will be printed. Try accept structure delimiting a section of code with try and accept in cases where we want to test an option or where the code can cause a failure of the program called dangerous code. If the code in the try section works, except the signal word, if the code and try fails to code and except is executed, whenever you run the following code, it stops where it finds an error. In the second example, the same code is written but try accept structure.

Here when the first step fails. The except clause executes The program continues. When the second class exceeds the except clause is ignored, and the program continues, we created a variable called a str that equals to Hello Bob. In the try statement, we try to convert the string str to an integer. Since this gives an arrow, the Excel class executes and prints negative one. In the second example, same variable equals to 123.

The conversion to an integer will be successful and 123 will be printed to the screen. In this example, we created a variable called as tr, n equals to the name Bob. In the try statement, we try to convert the string as tr to an integer. Since this gives an emerald is tr will be negative one. This is a simple example to test the user input is a number or not. If the try clause returns an arrow and it is not a number.

On the right side, you can see the script executed. Here's an exercise for you to do. revise your payroll program to give the employee 1.5 times the hourly rate for hours worked over 40 hours. rewrite the payroll using try and accept so that it handles non numeric entries. Consider possible user input errors.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.