Control Flow Logic

Control flow are the parts of a language that make it do things differently based upon various conditions. In other words: ifs and loops. Python has all of the basic control flow statements that you'd expect.

if Statements

Literal strings can be typed in using either double quotes or single quotes. This can be handy when your string contains one quote or the other. You can also use the backslash character to escape special characters. Strings that contain characters beyond 7-bit ASCII, such as é or щ need to be marked as unicode strings by placing the letter u in front of the string. There is no harm in marking all strings as unicode strings. The following prints a unicode string:

print u'été'

If statement should be familiar to anyone with a passing knowledge of programming. The idea of an if is that you want your script to execute a block of statements only if a certain condition is true. For example, this script won't do anything.

x = 15
if x < 10:
print "this will never show"

You can use the if...else form of an if statement to do one thing if a condition is true, and something else if the condition is false. This script will print out this will show!

x = 15
if x < 10:
print "this will never show"
else:
print "this will show!"

Lastly, you can use the if...elif form. This form combines multiple condition checks. elif stands for else if. This form can optionally have a catch-all else clause at the end. For example, this script will print out three:

x = 3
if x == 1:
print "one"
elif x == 2:
print "two"
elif x == 3:
print "three"
else:
print "not 1-3"