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:

[pastacode lang=”bash” manual=”if%20pwd%20%3D%3D%20’apple’%3A%0A%20%20%20%20print(‘Logging%20on%20…’)%0Aelse%3A%0A%20%20%20%20print(‘Incorrect%20password.’)%0A%0Aprint(‘All%20done!’)” message=”” highlight=”” provider=”manual”/]

 

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.

If/elif-statements

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:

[pastacode lang=”bash” manual=”%23%20airfare.py%0Aage%20%3D%20int(input(‘How%20old%20are%20you%3F%20’))%0Aif%20age%20%3C%3D%202%3A%0A%20%20%20%20print(‘%20free’)%0Aelif%202%20%3C%20age%20%3C%2013%3A%0A%20%20%20%20print(‘%20child%20fare)%0Aelse%3A%0A%20%20%20%20print(‘adult%20fare’)” message=”” highlight=”” provider=”manual”/]

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:

[pastacode lang=”bash” manual=”food%20%3D%20input(%22What’s%20your%20favorite%20food%3F%20%22)%0Areply%20%3D%20’yuck’%20if%20food%20%3D%3D%20’lamb’%20else%20’yum'” message=”” highlight=”” provider=”manual”/]

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:

[pastacode lang=”bash” manual=”food%20%3D%20input(%22What’s%20your%20favorite%20food%3F%20%22)%0Aif%20food%20%3D%3D%20’lamb’%3A%0A%20%20%20reply%20%3D%20’yuck’%0Aelse%3A%0A%20%20%20reply%20%3D%20’yum'” message=”” highlight=”” provider=”manual”/]

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.

Categorized in: