0% found this document useful (0 votes)
11 views17 pages

Python IF and ELSE Statements Guide

The document is a Python primer focusing on using IF and ELSE statements to ask questions and execute code based on conditions. It emphasizes the importance of proper indentation in Python to define blocks of code and provides examples of how to structure IF statements. Additionally, it encourages consistent spacing for better readability and includes a daily study routine for learners.

Uploaded by

zilianilucas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

Python IF and ELSE Statements Guide

The document is a Python primer focusing on using IF and ELSE statements to ask questions and execute code based on conditions. It emphasizes the importance of proper indentation in Python to define blocks of code and provides examples of how to structure IF statements. Additionally, it encourages consistent spacing for better readability and includes a daily study routine for learners.

Uploaded by

zilianilucas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Primer 102a

Asking questions with IF and ELSE

AL NAFI,
A company with a focus on education,
wellbeing and renewable energy.

© 2018 Al-Nafi. All Rights Reserved. 1


Dua of the day

The righteous dua that Abu Bakr (r) made at the end of his lifetime.

“O Allah, let the best of my lifetime be its ending, and my best deed be that which I seal [my life
with], and the best of my days the day I meet You.”

Reference: The Dua is taken from Sheikh Omer Sulaiman Dua Compilation.

© 2018 Al-Nafi. All Rights Reserved. 2


Study, Rinse and Repeat
• Please subscribe to our YouTube Channel to be on top of your studies.
• Please logon to our website [Link]
• Use your username and password to logon
• Please keep an eye on zone@[Link] emails
• Please review the videos of 30 minutes daily
• Please review the notes daily.
• Please take time for clearing up your mind and reflect on how things
are proceeding in your studies.

© 2018 Al-Nafi. All Rights Reserved. 3


© 2018 Al-Nafi. All Rights Reserved. 4
Conditions If and Else
In programming, we often ask yes or no questions, and decide to do
something based on the answer.

For example, we might ask, “Are you older than 20?” and if the answer is yes,
respond with “You are too old!”

These sorts of questions are called conditions, and we combine these


conditions and the responses into if statements.

Conditions can be more complicated than a single question, and if


statements can also be combined with multiple questions and different
responses based on the answer to each question.

© 2018 Al-Nafi. All Rights Reserved. 5


IF Statements

age = 13
if age > 20:
Print ('you are too old')

© 2018 Al-Nafi. All Rights Reserved. 6


An if statement is made up of the if keyword, followed by a condition
and a colon (:), as in if age > 20:

The lines following the colon must be in a block, and if the answer to
the question is yes (or true, as we say in Python programming), the
commands in the block will be run.

Now, let’s explore how to write blocks and conditions.

© 2018 Al-Nafi. All Rights Reserved. 7


Python Block
A BLOCK IS A GROUP OF PROGRAMMING STATEMENTS

A block of code is a grouped set of programming statements.

For example, when if age > 20: is true, you might want to do more than
just print “You are too old!” Perhaps you want to print out a few other
choice sentences, like this:

© 2018 Al-Nafi. All Rights Reserved. 8


age = 25
if age > 20:
print('You are too old!')
print('Why are you here?')
print('Why aren\'t you mowing a lawn or sorting papers?')

© 2018 Al-Nafi. All Rights Reserved. 9


This block of code is made up of three print statements that are run
only if the condition age > 20 is found to be true.

Each line in the block has four spaces at the beginning, when you
compare it with the if statement above it. Let’s look at that code again,
with visible spaces:

© 2018 Al-Nafi. All Rights Reserved. 10


The importance of Space in Python

Space equals block


In Python, whitespace, such as a tab (inserted when you press the TAB
key) or a space (inserted when you press the spacebar), is meaningful.
Code that is at the same position (indented the same number of spaces
from the left margin) is grouped into a block, and whenever you start a
new line with more spaces than the previous one, you are starting a
new block that is part of the previous one, like this:

© 2018 Al-Nafi. All Rights Reserved. 11


Block means programming statements!!
We group statements together into blocks because they are related.
The statements need to be run together. When you change the
indentation, you’re generally creating new blocks.

The following example shows three separate blocks that are created
just by changing the indentation.

© 2018 Al-Nafi. All Rights Reserved. 12


We group statements
together into blocks because
they are related. The
statements need to be run
together. When you change
the indentation, you’re
generally creating new
blocks.

The following example shows


three separate blocks that are
created just by changing the
indentation.

© 2018 Al-Nafi. All Rights Reserved. 13


Here, even though blocks 2 and 3 have
the same indentation, they are
considered different blocks because there
is a block with less indentation (fewer
spaces) between them.

For that matter, a block with four spaces


on one line and six spaces on the next will
produce an indentation error when you
run it, because Python expects you to use
the same number of spaces for all the
lines in a block.

© 2018 Al-Nafi. All Rights Reserved. 14


If the spaces are out of whack

© 2018 Al-Nafi. All Rights Reserved. 15


Points to Remember for spacing
Use consistent spacing to make your code easier to read. If you start
writing a program and put four spaces at the beginning of a block, keep
using four spaces at the beginning of the other blocks in your program.
Also, be sure to indent each line in the same block with the same
number of spaces.

© 2018 Al-Nafi. All Rights Reserved. 16


‫ه‬ ‫للا‬ ‫جزاك‬
Please send us your questions at zone@[Link]
We will only answer our Nafi Members. So please
quote your membership number within the email.

© 2018 Al-Nafi. All Rights Reserved. 17

You might also like