• No results found

Inline if

In document Python Book (Page 52-60)

Chapter 2 Lab Exercise

3.5 Inline if

In addition to the conditional blocks we previously learned, there is also another quicker way of writing conditions. This is known as inline if. In this case, the condition and the action are both written in

one line:

a, b = 0, 1

print (True if a == b else False)

This means that True will be printed if a is equal to b, otherwise False will be displayed. In this case, the output would be:

Figure 3.10: True is printed out as part of the if clause inside the inline if statement

Similarly, you can include other types of expressions inside the inline if and even assign the whole line to a variable:

a, b = 0, 1

var = "This is true" if a == b else "This is not true"

print (var)

The output of this is:

Figure 3.11: Text is printed out as part of the else clause inside the inline if statement

The last code is a very commonly used case of the inline if functionality as it allows variables to be assigned values depending on the conditions.

However, the inline if conditionals are restricted, meaning that they cannot be used in complex expressions. When complex expressions are

Inline If

3.5 Inline If

concerned, the standard if, else and elif statements, which you learned in the previous sections, should be used. The multiline structure of the standard conditionals allows us to write more advanced programs that have conditional aspects at the core.

Questions for Review

1. Which of the following is a correctly written expression?

a. if a = b:

2. What is not true about elif?

a. Elif is used when testing multiple statements.

b. Elif is a substitute of the switch function.

c. Elif is similar to the switch workaround method.

d. Elif initiates a condition, similar to how if does.

3. What happens when none of the conditions are true in a conditional block?

a. The action under else is executed.

b. Nothing happens.

c. The program throws an error.

d. False is printed out.

4. How would you write a code that prints “Greater” if a is greater than b, and “Less or equal” if a is less than or equal to b, using the inline if statement?

a. a, b = 10, 20

print (“Greater” if a < b else “Less or equal”) b. a, b = 10, 20

print (“Greater” if a > b elif”Less or equal”) c. a, b = 10, 20

print (“Less or equal” if a <= b else “Greater”) d. a, b = 10, 20

print (“Greater” if a >= b else “Less or equal”)

Questions for Review

53 53

C

hapTer

3 l

ab

e

XerCise

One key thing about conditional statements is their ability to create something called a truth table which holds a certain number of input (usually a minimum of two) with one exception –not. Truth tables run using binary, which is what is happening behind the scenes within Python. Your input is being converted to binary when you are doing a comparison. There are six truth tables in total, however, we will only be looking at three of them, And, Or and Not.

Take the following tables and try to convert them into if statements in order to receive the correct output. Each row shows a different set of input:

The next two truth tables are as follows:

OR

In this case I would utilize the and operator in Python to show you exactly what is happening behind the scenes for the or truth table.

Python also has this built-in using the or operator. We can use this functionality later on to save us from having to write unnecessary code.

Final Lab Exercises You can see that the only place where two ‘true’

statements get passed is 1 and 1. This is the only output which is true, the rest being false.

As you can see with OR, any case having at least one 1 is true. Try to represent this using if statements as well.

NOT

Now for the real challenge! Try to write the code for the following truth table.

The equation for this truth table is as follows:

A and not B or C = Q

The equation is read from left to right and would appear like this if written in mathematical terms:

((A&~B)|C) = Q

Using the above tables, create a program that will give you the correct output for the following table:

|---|

Note: The creation of the main challenge will involve using more variables to store the current details in than shown above.

As you can see with not, it is the exception that takes only one input. The way this works is that it basically reverses the values.

55

# noT

TruThTable

: a = 0

if(a == 1):

print('0') elif(a == 0):

print('1') else:

print('Invalid Input')

# m

ain

C

hallenge

: a = 1

b = 0 c = 1

if(b == 1):

bHolder = 0 elif(b==0):

bHolder = 1

if(a == 1 and bHolder == 1):

else:

print('Q = 1') else:

print('Q = 0')

57 57

C

hapTer

3 s

ummary

In this chapter you were introduced to conditionals, which are a very important part of every programming language as they allow conditioned programming.

We discussed how useful the if statement is when you want to perform an action that is dependent on a certain condition. Along with the if statement, you learned that the code block preceding the if statement should be properly indented.

You experimented with the else statement and understood that it can be used as an alternative condition when the if statement is not satisfied.

You performed multiple actions that were dependent on multiple conditions made possible by the elif statement.

We also discussed an alternative workaround of the switch statements used in other programming languages. You should know that the switch workaround is also an alternative to the elif statements.

Lastly, you worked out another quick way of building if statements using the inline if alternative which, even though it is more

restricted in terms of the complexity of actions it can perform, can be a quicker solution on certain occasions.

In the next chapter you will be introduced to the looping concept.

Looping is crucial when working with large amounts of data.

Basically, looping is used to run repetitive actions until a condition is specified. It has a number of methods which you will learn throughout the chapter.

In document Python Book (Page 52-60)