Lesson 5 Conditional execution in Python

7 minutes
Share the link to this page
You need to have access to the item to view this lesson.
One-time Fee
List Price:  $99.99
You save:  $30
List Price:  €91.90
You save:  €27.57
List Price:  £78.70
You save:  £23.61
List Price:  CA$136.14
You save:  CA$40.84
List Price:  A$149.21
You save:  A$44.76
List Price:  S$134.58
You save:  S$40.38
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
List Price:  NZ$162.99
You save:  NZ$48.90
List Price:  د.إ367.26
You save:  د.إ110.19
List Price:  ৳11,707.96
You save:  ৳3,512.74
List Price:  ₹8,329.22
You save:  ₹2,499.01
List Price:  RM468.70
You save:  RM140.62
List Price:  ₦146,985.30
You save:  ₦44,100
List Price:  ₨27,706.17
You save:  ₨8,312.68
List Price:  ฿3,624.43
You save:  ฿1,087.43
List Price:  ₺3,224.01
You save:  ₺967.30
List Price:  B$510.40
You save:  B$153.13
List Price:  R1,807.25
You save:  R542.23
List Price:  Лв179.86
You save:  Лв53.96
List Price:  ₩135,425.45
You save:  ₩40,631.70
List Price:  ₪370.41
You save:  ₪111.13
List Price:  ₱5,770.97
You save:  ₱1,731.46
List Price:  ¥15,565.94
You save:  ¥4,670.25
List Price:  MX$1,660.69
You save:  MX$498.25
List Price:  QR362.75
You save:  QR108.83
List Price:  P1,354.73
You save:  P406.46
List Price:  KSh13,098.69
You save:  KSh3,930
List Price:  E£4,690.53
You save:  E£1,407.30
List Price:  ብር5,727.42
You save:  ብር1,718.40
List Price:  Kz84,733.22
You save:  Kz25,422.51
List Price:  CLP$89,440.82
You save:  CLP$26,834.93
List Price:  CN¥722.24
You save:  CN¥216.69
List Price:  RD$5,826.59
You save:  RD$1,748.15
List Price:  DA13,436.03
You save:  DA4,031.21
List Price:  FJ$222.79
You save:  FJ$66.84
List Price:  Q776.81
You save:  Q233.06
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
List Price:  DH990.33
You save:  DH297.13
List Price:  L1,760
You save:  L528.05
List Price:  ден5,654.52
You save:  ден1,696.52
List Price:  MOP$803.51
You save:  MOP$241.07
List Price:  N$1,819.78
You save:  N$545.99
List Price:  C$3,674.63
You save:  C$1,102.50
List Price:  रु13,329.38
You save:  रु3,999.21
List Price:  S/371.45
You save:  S/111.44
List Price:  K388.01
You save:  K116.41
List Price:  SAR374.96
You save:  SAR112.50
List Price:  ZK2,538.86
You save:  ZK761.73
List Price:  L457.57
You save:  L137.28
List Price:  Kč2,271.57
You save:  Kč681.54
List Price:  Ft35,609.88
You save:  Ft10,684.03
SEK kr750.08
List Price:  SEK kr1,071.59
You save:  SEK kr321.50
List Price:  ARS$88,666.13
You save:  ARS$26,602.50
List Price:  Bs690.83
You save:  Bs207.27
List Price:  COP$380,678.73
You save:  COP$114,215.04
List Price:  ₡51,169.76
You save:  ₡15,352.46
List Price:  L2,462.78
You save:  L738.91
List Price:  ₲749,130.70
You save:  ₲224,761.68
List Price:  $U3,853.49
You save:  $U1,156.16
List Price:  zł392.01
You save:  zł117.61
Already have an account? Log In


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 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.