100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Branching Using Conditional Statement loops in python

Rating
-
Sold
-
Pages
20
Uploaded on
26-01-2022
Written in
2021/2022

This document will aid Students studying pythons as in Computer programming.

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
January 26, 2022
Number of pages
20
Written in
2021/2022
Type
Class notes
Professor(s)
Justus ngari
Contains
College and university

Subjects

Content preview

Branching with if, else and elif
One of the most powerful features of programming languages is branching: the ability to make
decisions and execute a different set of statements based on whether one or more conditions are
true.


The if statement
In Python, branching is implemented using the if statement, which is written as follows:

if condition:

statement1

statement2

The condition can be a value, variable or expression. If the condition evaluates to True, then the
statements within the if block are executed. Notice the four spaces
before statement1, statement2, etc. The spaces inform Python that these statements are
associated with the if statement above. This technique of structuring code by adding spaces is
called indentation.
Indentation: Python relies heavily on indentation (white space before a statement) to define code
structure. This makes Python code easy to read and understand. You can run into problems if you
don't use indentation properly. Indent your code by placing the cursor at the start of the line and
pressing the Tab key once to add 4 spaces. Pressing Tab again will indent the code further by 4
more spaces, and press Shift+Tab will reduce the indentation by 4 spaces.

For example, let's write some code to check and print a message if a given number is even.

,The else statement
We may want to print a different message if the number is not even in the above example. This can
be done by adding the else statement. It is written as follows:

if condition:

statement1

statement2

else:

statement4

statement5



If condition evaluates to True, the statements in the if block are executed. If it evaluates
to False, the statements in the else block are executed.

, The elif statement
Python also provides an elif statement (short for "else if") to chain a series of conditional blocks.
The conditions are evaluated one by one. For the first condition that evaluates to True, the block of
statements below it is executed. The remaining conditions and statements are not evaluated. So, in
an if, elif, elif... chain, at most one block of statements is executed, the one corresponding to
the first condition that evaluates to True.




In the above example, the first 3 conditions evaluate to False, so none of the first 3 messages are
printed. The fourth condition evaluates to True, so the corresponding message is printed. The
remaining conditions are skipped. Try changing the value of today above and re-executing the cells
to print all the different messages.

To verify that the remaining conditions are skipped, let us try another example.




Note that the message 15 is divisible by 5 is not printed because the
condition a_number % 5 == 0 isn't evaluated, since the previous condition a_number % 3 ==
$7.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
Mnyazi

Get to know the seller

Seller avatar
Mnyazi Freelancer
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 year
Number of followers
0
Documents
119
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions