top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Conditionals and logic for Python

We'll often want the computer only to take an action under certain circumstances. For example, we might want a game to print the message 'High score!', but only if the player's score is higher than the previous high score. We can write this as a formal logical statement: if the player's score is higher than the previous high score then print 'High score!'.

The syntax for expressing this logic in Python is very similar. Let's define a function that accepts the player's score and the previous high score as arguments. If the player's score is higher, then it will print 'High score!'. Finally, it will return the new high score (whichever one that is).

ree

With if statements we use a similar syntax as we used for organizing functions. With functions we had a def statement ending with :, and an indented body. Similarly for a conditional, we have an if statement ending with :, and an indented body.

Conditional statements are used to control program flow. We can visualize our example, test_high_score, in a decision tree.


We can nest if statements to make more complicated trees.

ree

In this example, we have an if statement nested under another if statement. As we change the input, we end up on different branches of the tree.


The statement that follows the if is called the condition. The condition can be either true or false. If the condition is true, then we execute the statements under the if. If the condition is false, then we execute the statements under the else (or if there is no else, then we do nothing).

Conditions themselves are instructions that Python can interpret.

ree

Conditions are evaluated as booleans, which are True or False. We can combine conditions by asking of condition A and condition B are true. We could also ask if condition A or condition B are true. Let's consider whether such statements are true overall based on the possible values of condition A and condition B.

ree


ree

The keywords or and and are called logical operations (in the same sense that we call +, -, *, etc. arithmetic operations). The last logical operation is not: not True is False, not False is True.

ree



 
 
 

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page