Python Concepts/If Statement
Objective[edit | edit source]
|
Lesson[edit | edit source]Indentations[edit | edit source]Unlike many other computer languages, like C++ and java, Python uses indentation to determine blocks of code. The following pseudocode below will help demonstrate. This is some code. This is some more code. This code is part of a code block. And so is this! And this!!! This code isn't part of the code block. =( But this code is part of a new code block! And this code is, as well! As we can see, there can be multiple blocks of code. You can also contain blocks of code within another block of code, so long as it indents. There isn't a limit on the indent, but it must be kept constant or you'll get an error. This is some code. This is a block of code indented very far away. This is completely legal in Python, although not encouraged. The preferred indentation is in multiples of four. This isn't a block of code; it follows no indentation style. This would cause an error! Although you're allowed to use white spaces and tabs, it's preferred that you use white spaces. Also, you cannot mix both white spaces and tabs together or you'll get an error. You should know that since we'll be using the Python Interpreter to compute the example code, >>> if True:
... print("Hello, world!")
...
Hello, world!
The If Statement[edit | edit source]In most, if not all programming languages, there are special ways to control the flow of code. Such control allows certain parts of code to execute if the conditions are right. This flow control is done by using statements and code blocks. The most common and widely used statement is the Remember that >>> if 1 == 1:
... print("This will print!")
...
This will print!
>>> if 1 == 2:
... print("This will never print. =/")
...
>>> if True:
... print("True!")
...
True!
>>> if False:
... print("False!")
...
>>> if 1 == 1 and 2 == 2:
... print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
... print("True")
...
>>> if 1 == 2 or 2 == 2:
... print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
... print("True!")
...
>>> if not False:
... print("True!")
...
True!
>>> if not 1 == 2:
... print("True!")
...
True!
>>> if 30 == 30:
... print("30 equals 30!")
... if True:
... print("True is True!")
...
30 equals 30!
True is True!
>>> if 1 == 1:
... if 2 == 2:
... if 3 == 3:
... print("1, 2, and 3 are equal to their selves!")
...
1, 2, and 3 are equal to their selves!
>>>
>>> if 1 == 1 and 2 == 2 and 3 == 3:
... print("1, 2, and 3 are equal to their selves!")
...
1, 2, and 3 are equal to their selves!
Semi-colons, Backslashes, and Parentheses[edit | edit source]You're probably going to want to keep several lines of code on just one line. This can be accomplished by using the semicolon ( >>> if 1 != 2:
... a = 1
... b = 2
... c = 3
...
>>> if 1 != 2: a = 1; b = 2; c = 3
...
>>> if 1 != 2: a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8; i = 9;
...
>>> if 1 == 1: spam = 2 * 2; eggs = spam / 2; bacon = (spam + eggs) * 4;
...
>>> if 1 == 1 and 2 == 2 and 3 == 3 and \
... 4 == 4 and 5 == 5 and 6 == 6:
... print("True!")
...
True!
A more workable solution is to use parentheses ( >>> if (1 == 1 and 2 == 2 and 3 == 3 and
... 4 == 4 and 5 == 5 and 6 == 6):
... print("True!")
...
True!
The Else Statement[edit | edit source]There will be many times when you want to execute code in the event that the >>> if 2 == 2:
... print("2 equals 2!")
... else:
... print("2 doesn't equal 2!")
...
2 equals 2!
>>> if 2 == 3:
... print("2 equals 3!")
... else:
... print("2 doesn't equal 3!")
...
2 doesn't equal 3!
>>> if 3 == 3:
... print("3 equals 3!")
... else:
... print("3 doesn't equal 3!")
...
3 equals 3!
if not (a == 1 and b == 2 and c == 3 and
d == 4 and e == 5 and f == 6):
print("Processing error!")
or you can leave the condition as is and act on the if (a == 1 and b == 2 and c == 3 and
d == 4 and e == 5 and f == 6) :
pass # situation normal
else :
print("Processing error!")
The Elif Statement[edit | edit source]Like a similar problem before, you might need to do something that might seem tedious. Say you wanted four >>> if fraction == 1:
... print("The fraction is one whole!")
... elif fraction == 3/4:
... print("The fraction is three fourths!")
... elif fraction == 2/4:
... print("The fraction is one half!")
... elif fraction == 1/4:
... print("The fraction is one fourth!")
... else:
... print("I have no idea what the fraction is!")
...
The fraction is one fourth!
The status = -2
if status in [-3,-1,0,2,4,7] :
print ('status', status, 'recognized')
else :
print ('status', status, 'not recognized')
|
Assignments[edit | edit source]
|
|
References
[edit | edit source]1. Python's documentation:
"4.1. if Statements," "4.8. Intermezzo: Coding Style," "6.10. Comparisons," "8. Compound statements," "8.1. The if statement," "Why does Python use indentation for grouping of statements?" "Why isn’t there a switch or case statement in Python?" "Why are colons required for the if/while/def/class statements?"
2. Python's methods:
3. Python's built-in functions: