Conditional (If-Else) Statements in Python

(2 votes, average 4.50 out of 5)

Below is a quick example of how to use an if statement in python:

 

1
2
3
4
5
if x==3:
	print "We are inside an if statement"
	x = x + 1
print "we are no longer in an if statement"

 

Note that unlike most languages, no parentheses are needed, although including parentheses does not violate python syntax.  Anything indented after the if statement is included in the execution if the if statement evaluates to true.  The scope is closed once indentation returns to the indent location of the original, matching if statement.  Below is an example of an if, else if, else clause in python:

1
2
3
4
5
6
7
8
if x==3:
	print "we are inside the if statement"
elif x==2:
	print "we are inside the else-if statement"
else:
	print "we are inside the else statement"
#end conditional block
print "we are outside of the if-else block"

 

Important notes about this functionality in python:

  • Instead of a traditional "else if" statement, python uses the shorter keyword elif.  
  • Any elif or else keyword must be in line with a corresponding if statement.  If either of these keywords are not matched in indentation to an existing if statement, python will throw an error and exit.
  • Variables declared in an if statement can be used elsewhere in the code.  In other words, there is no scope for variables declared in an if block.  Convenient, but use wisely!
Partner Links:
Last Updated on Monday, 28 June 2010 14:08  
Related Articles

» Verilog Operators

Operators in Verilog are the same as operators in programming languages. They take two values and compare or operate on them to yield a new result. Nearly all the operators in Verilog are exactly the same as the ones in the C programming language.Operator TypeOperator SymbolOperation PerformedArithmetic*Multiply/Division+Addition-Subtraction%Modulus+Unary plusiUnary minusRelational>Greater than<Less Than>=Greater than or equal to<=Less than or equal...

» Using Awk to Print Select Output

Awk is a very powerful utility, but its syntax is less than intuitive to a beginner. Here are a few common but very powerful things you can do with awk:1234567891011121314151617# print line 2 of the fileawk 'NR == 2' myFile.txt # print column 2 of the fileawk '{print $2}' myFile.txt # add up column 2awk '{s += $4} END {print s}' # print all the lines with more than 80 columnsawk 'length($0) > 80' myFile.txt # print the number of lines in the fileawk 'END { print NR }'...

» For Loops in PHP

To create a for loop in php, follow these simple steps:Set a counter variable to some initial value.Check to see if the conditional statement is true.Execute the code within the loop.Increment a counter at the end of each iteration through the loop.The following is pseudo php code for a for loop:123for(init counter; conditional statement; increment counter){ inner code;}As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to...