Indentation in Python with Examples

It is generally good practice for you to not mix tabs and spaces when coding in Python. Doing this will possibly cause a TabError, and your program will crash. Be consistent once you code – choose either to indent using tabs or spaces and follow your chosen convention throughout your program.

Code Blocks and Indentation

One of the foremost distinctive features of Python is its use of indentation to mark blocks of code. Consider the if-statement from our simple password-checking program:

if pwd == 'apple':
    print('Logging on ...')
    print('Incorrect password.')

print('All done!')


The lines print(‘Logging on …’) and print(‘Incorrect password.’) are two separate code blocks. These ones happen to be only one line long, but Python allows you to write code blocks consisting of any number of statements.

To indicate a block of code in Python, you want to indent each line of the block by an equivalent amount. the 2 blocks of code in our example if-statement are both indented four spaces, which may be a typical amount of indentation for Python.

In most other programming languages, indentation is employed only to assist make the code look pretty. But in Python, it’s required for indicating what block of code a press release belongs to. as an example , the ultimate print(‘All done!’) isn’t indented, then isn’t a part of the else-block.

Programmers conversant in other languages often bridle at the thought that indentation matters: Many programmers just like the freedom to format their code how they please. However, Python indentation rules are quite simple, and most programmers already use indentation to form their code readable. Python simply takes this concept one step further and provides aiming to the indentation.

READ  Python Programming - check for pair in A[] with sum as x


An if/elif-statement may be a generalized if-statement with quite one condition. it’s used for creating complex decisions. for instance , suppose an airline has the subsequent “child” ticket rates: Kids 2 years old or younger fly for free of charge , kids older than 2 but younger than 13 pay a reduced child fare, and anyone 13 years or older pays a daily adult fare. the subsequent program determines what proportion a passenger should pay:

age = int(input('How old are you? '))
if age <= 2:
    print(' free')
elif 2 < age < 13:
    print(' child fare)
    print('adult fare')

After Python gets age from the user, it enters the if/elif-statement and checks each condition one after the opposite within the order they’re given. So first it checks if age is a smaller amount than 2, and if so, it indicates that the flying is free and jumps out of the elif-condition. If age isn’t but 2, then it checks subsequent elif-condition to ascertain if age is between 2 and 13. If so, it prints the acceptable message and jumps out of the if/elif-statement. If neither the if-condition nor the elif-condition is True, then it executes the code within the else-block.

Conditional expressions

Python has another logical operator that some programmers like (and some don’t!). It’s essentially a shorthand notation for if-statements which will be used directly within expressions. Consider this code:

food = input("What's your favorite food? ")
reply = 'yuck' if food == 'lamb' else 'yum'

The expression on the right-hand side of = within the second line is named a conditional expression, and it evaluates to either ‘yuck’ or ‘yum’. It’s like the following:

food = input("What's your favorite food? ")
if food == 'lamb':
   reply = 'yuck'
   reply = 'yum'

Conditional expressions are usually shorter than the corresponding if/else-statements, although almost as flexible or easy to read. generally , you ought to use them once they make your code simpler.

READ  How to Substring a String in Python

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.