Preparing for Python development on the Raspberry Pi (part 003)

May 4, 2013 | In: Python

Part 003 – Control Flow tutorials

The following is a code snippet showing control flow basics that will be talked about at the upcoming WNYLUG meeting. The code snippet contains a sample if statement, for statement, for statement using the range function and the while statement.

[python]

# the if statement
runIfTest = 0

if runIfTest == 1:
x = 11

if x < 10:
print(‘x is less than 10’)
elif x == 10:
print(‘x is equal to 10’)
elif x > 10:
print(‘x is greater than 10’)
else:
print(‘x is being weird right now’)

# the for statement
runForTest = 0

if runForTest == 1:
words = [‘Calling’, ‘Western’, ‘New’, ‘York’, ‘Linux’, ‘User’, ‘Group’, ‘Response?’]

for word in words:
print(word)

# the for statement with the range function
runForRangeTest = 0

if runForRangeTest == 1:
for i in range(5):
print(i)

print(‘******************’)

for i in range(5, 10):
print(i)

print(‘******************’)

for i in range(0, 100, 25):
print(i)

print(‘******************’)

for i in range(-1, -5, -1):
print(i)

# the while statement
runWhileTest = 0

if runWhileTest == 1:
x = 2
limit = 100

print(‘x is equal to ‘ + str(x))

while x < limit:
x = x * x
print(‘x is now equal to ‘ + str(x))
else:
print(‘x hit or surpassed the limit of ‘ + str(limit))

[/python]

Comments are closed.