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
Python Machine Learning: A Complete Guide for Beginners on Machine Learning and Deep Learning with Python (Data Science Mastery Book 4) by Andrew Park.epub
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.
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9793)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8288)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6778)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6505)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6372)
Kotlin in Action by Dmitry Jemerov(5058)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4313)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3975)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3758)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3702)
The Ultimate iOS Interview Playbook by Avi Tsadok(3676)
