Python 3 Tutorial for Beginners #03 – If Statement and Python 3 Script Syntax

Introduction

So in previous tutorials we got our hands dirty with some quick and easy code. Now we are going to dabble with some serious stuff using Python 3 Script Syntax.

Structure of a Python 3 Script

Structure of Python a 3 Script

  1. This line is known as shebang. You might have seen it in Unix like environments. This is the recommended shebang for Python 3.
  2. A single line comment starts with a hash sign. Comments are ignored by the python interpreter.

Please note that it is highly recommended that you use meaningful variable names instead of comments to explain what you are doing.

Example: in above case if you use money_in_euros or euros as the variable name you can avoid the comment, making the code more understandable.

Proper use of a comment is to specify what is not obvious by looking at code, such as why you are doing something.

Python 3 Code Block

  1. This is a python 3 script function definition.
  2. This is a multiline comment/string. You can use both single quote and double quote to create multiline comments. You need to use 3 of them to start and 3 of them to end the comment element.
  3. This is the body of the python 3 script function. Notice that each line is indented by 4 spaces. If you are developing python I recommend that you use 4 spaces as indentation. (It is recommended by PEP8 – the python style guide).
  4. Call the function “print_name()“.

If-else statement

Now that we got the introduction and understand the basic Python 3 script syntax let’s look at the if statement.

Python 3 If else statement

If statements allow you to make decisions. Now, do you remember the logical operators we learned in previous tutorial? Let’s learn how to use them with if statements.

Python 3 If Statement Indentation

Yay! finally we are doing something cool. Let’s see now. We have defined 2 variables called a and b and assigned some numbers to them.

  1. You need to ensure that you end the “if” or “else” with a “:” to say the Python interpreter that you are going to start giving it one or more statements to process.
  2. Notice the 4-space indentation used to mark the block of statements that belong to the “else” part of the “if” statement.

Code block in the “if” section get executed if the given expression is True. Otherwise it will execute the “else” section.

Now type that code into expressPython and see what happens. I suggest you play around with it.

You can skip the “else” section if you don’t want to do anything for a non-matching scenario.

Python 3 Trying out If Statement
How to check for more scenarios using “elif

Python 3 not only allows you to write if-else statements it also support a special statement called “elif” which is a shortened form of “else if“.
Let’s see the below example.

marks = 75
if marks >= 80:
    print("Grade A")
elif marks >= 70:
    print("Grade B")
elif marks >= 50:
    print("Pass")
else:
    print("Fail") 

I suggest you copy the code and try changing marks and run it.

Python 3 If Statement with elif

  1. Print “Grade A” if marks are greater than or equal to 80.
  2. This statement is checked only if “if” statement is false. So, the numbers belong to 70-79 (inclusive) range will get “Grade B“.
  3. If you didn’t get an “A” or “B” but you got marks greater than or equal to 50, you have passed the exam.
  4. None of the statements match therefore you fail the exam.

You may have any number of “elif” statements and “else” is optional.

Nested statements

Assume you are going to a bank to open an account. Let’s see how they handle the situation in code.

age = 18
money = 1800
if age >= 18:
    print("Age level passed")
    if money >= 1500:
        print("Can start account")
    else:
        print("Cannot start account")    
else:
    print("Come back when 18")

Let’s try that out in expressPython.
Python 3 Nested If statements
Let’s see how this works.

Python 3 Nested If Statement

  1. This section depicts a single code block indented with 4 spaces.
  2. This section depicts a nested code block which has an indentation of 8 spaces.

Print function call” and “if statement” to check money, all belongs to the parent “if statement” that check for the age. For the nested “if” statements to identify its child blocks, those are needed to be indented again, hence the 8 spaces in case 2.

Exercises

Add Grade C to marks

Simply allocate ‘Grade C’ for suitable students.

Answer

marks = 65
if marks >= 80:
    print("Grade A")
elif marks >= 70:
    print("Grade B")
elif marks >= 60:
    print("Grade C")    
elif marks >= 50:
    print("Pass")
else:
    print("Fail") 

That’s it folks. Enjoy making decisions.

Here’s a link to the next Python tutorial (#04) of this series. We’ve got more Python beginner tutorials right here for you! We’ve also got tutorials on using Irfanview and Greenshot to resize/crop images. Check out our Python tutorials for dabblers while you’re at it! Also, while you’re here, don’t forget to check out the latest tech news and info on fresh tech releases too.

Like what you see? Subscribe to our email list and hit us with a like on our Facebook page to get the latest news updates and tutorials straight to your newsfeed!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.