Python Machine Learning: A Complete Guide for Beginners on Machine Learning and Deep Learning with Python (Data Science Mastery Book 4) by Andrew Park

Python Machine Learning: A Complete Guide for Beginners on Machine Learning and Deep Learning with Python (Data Science Mastery Book 4) by Andrew Park

Author:Andrew Park [Park, Andrew]
Language: eng
Format: azw3, epub
Published: 2021-05-04T16:00:00+00:00


Conditional Tests in Python

In the center of any if statement lies an expression, which must be evaluated to be either true or false. This is what is normally known as a conditional test because Python uses both values to determine if a particular code should be executed. If the particular statement is true, Python executes the code that follows it. However, if it is false, it ignores the code after it.

Checking Equality

At times, we may test for the equality of a particular condition. In this situation, we test if the value of the variable is equal to the other variable we decide. For instance:

>>>color = “green”

>>> color == “green”

True

In this example, we first assign the variable color with the value “green by using the single equal sign. This is not something new, as we have been using it throughout this book. However, the second line checks if the value of color is green, which has a double equal sign. It will return true if the value on the left side and that on the right side are both true. If it doesn’t match, then the result will be false. When the value of the color is anything besides green, then this condition equates to false. The example below will clarify that.

>>>color = “green”

>>> color == “blue”

False

Note: When you test for equality, you should know that it is case sensitive. For instance, two values that have different capitalizations won’t be regarded as equal. For instance,

>>>color = “Green”

>>> color == “green”

False

If the case is important, then this is advantageous. However, if the case of the variable isn’t important, and you want to check the values, then you can convert the value of the variable to lowercase before checking for equality.

>>>color = “Green”

>>> color.lower() == “green”

True

This code will return True irrespective of how to format the value “Green” is because the conditional tests aren’t case sensitive. Please note that the lower() function we used in the program does not change the value originally stored in color.

In the same way, we can check for equality; we can also check for inequality in a program code. In checking for inequality, we verify if two values are not equal and then return it as true. To check for inequality, Python has its unique symbol, which is a combination of the exclamation sign with an equal sign (!=). Most programming language uses these signs to represent inequality.

The example below shows the use of if statement to test for inequality:

color = “green”

if color != “blue”

print(“The color doesn’t match”)

In the second line, the interpreter matches the value of color to that of “blue.” If the values match, then Python return false; however, if it is true, Python returns true before executing the statement following it “The color doesn’t match”

The color doesn’t match

Numerical Comparison in Python

We can also test numerical values in Python, but it is very straightforward. For instance, the code below determines if a person’s age is 25 years old:

>>>myage = 25

>>>myage == 25

True

Additionally, we can also test if two numbers are unequal.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.