0% found this document useful (0 votes)
158 views2 pages

Ipnd Reference Sheet Python Compound Statements PDF

Compound statements in Python include for loops, function definitions, and if statements. For loops repeat a block of code for each item in a list or range. Function definitions define reusable blocks of code that can be called. If statements run a block of code conditionally depending on whether a condition is true or false, and can optionally include an else block to run if the condition is false.

Uploaded by

Garry RF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views2 pages

Ipnd Reference Sheet Python Compound Statements PDF

Compound statements in Python include for loops, function definitions, and if statements. For loops repeat a block of code for each item in a list or range. Function definitions define reusable blocks of code that can be called. If statements run a block of code conditionally depending on whether a condition is true or false, and can optionally include an else block to run if the condition is false.

Uploaded by

Garry RF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 

Compound Statements in Python 


This page is a reference for the kinds of ​compound statements 
you've seen so far in Python.    
 
 
 
So far, you've seen three types of c​ ompound statements​: f
​ or​ loops, function d
​ ef​initions, and i
​ f 
statements. 
 
All of these statements have several things in common: 
● The first line of a compound statement always begins with that statement's keyword: f ​ or​, 
def​, or i
​ f​.  
● The first line always ends with a colon :
​ ​.  
● The rest of the lines in the compound statement are a b
​ lock​ of code, all i​ ndented​ by the same 
number of spaces. 
 
For Loops 
 
 
Af​ or​ loop repeats its block of code once for each item in a list or range. The number of times that 
code repeats is the number of items in that list or range. The loop has a l​ oop variable​ which is set 
equal to each item, in order. 
 
for​ item ​in​ [​1​, ​17​, ​-34​, ​"bears"​]:
​# code goes here
​# this code runs four times 
 
Ranges are useful in f​ or​ loops so you don't have to write out a long list of numbers.] 
 
martin = turtle.Turtle()
for​ num ​in​ range(​17​):
​# martin will go 0 pixels, turn, go 1 pixel, turn, etc.
martin.forward(num)
martin.right(​90​) 
 

 
version 1.0 
 

 
Function Definitions 
 
A function definition starts with the keyword d
​ ef​, the n
​ ame​ of the function, and a parenthesized 
list of its a
​ rguments​. 
 
​ pin​(tur, howfast):
def​ s
tur.speed(howfast)
tur.right(​360​) 
 
When Python sees the function definition, it does not immediately run the code inside it. When you 
want it to run the function, you c
​ all​ the function, and pass it values for its arguments: 
 
balthazar = turtle.Turtle()
spin(balthazar, ​5​) 
 
Conditionals with i
​ f​ and e
​ lse 
 
The i
​ f​ keyword introduces a c
​ onditional​ statement. It has a​ condition​, a true-or-false question. 
The block of code inside an i
​ f​ statement will either run, or not run, depending on whether the 
condition is true. 
 
if​ color == ​"pink"​:
​# Betty will spin only if the color is pink.
spin(betty, ​0​) 
 
Optionally, an i​ f​ block can be followed immediately by an e
​ lse​ block, which will run if the 
condition is false. 
 
if​ weather == ​"rainy"​:
drawCloud()
drizzle()
else​:
drawSun() 
 
Either the code under i
​ f​ will run, or the code under e
​ lse​ ​will run; never both. 

 
 
version 1.0

You might also like