Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
Wordplay handout
(section)
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Concept review=== ====Indentation reminder==== In Python, indentation matters. Everything is indented by a multiple of some number of spaces, often 4. In <code>if</code> statements, you indent everything you want to be run if the if conditional is <code>True</code>. For example: <pre> >>> James = 35 >>> Alice = 30 >>> if James > Alice: ... print "James is older than Alice." ... James is older than Alice. >>></pre> Because James really is older than Alice, the <code>if</code> conditional is <code>True</code>, so Python does execute the code indented under the if line. In this case we print "James is older than Alice." <pre> >>> James = 35 >>> Alice = 30 >>> if James < Alice: ... print "James is younger than Alice." ... >>></pre> Because James is not older than Alice, the <code>if</code> conditional is <code>False</code>, so Python does not execute the code indented under the if line. In for loops, you indent everything you want to be run each loop For example: <pre> >>> names = ["Jessica", "Adam", "Liz"] >>> for name in names: ... print "Hello " + name ... Hello Jessica Hello Adam Hello Liz</pre> The <code>print</code> line is indented 4 spaces under the <code>for</code>. That's how Python knows to execute the print line for every name in names. ====range==== <pre> >>> range(5) [0, 1, 2, 3, 4] >>> for i in range(5): ... print "Hi" * i ... Hi HiHi HiHiHi HiHiHiHi</pre> ====<code>if</code> statements inside <code>for</code> loops==== <pre> >>> for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]: ... if name[0] in "AEIOU": ... print name + " starts with a vowel." ... Alice starts with a vowel. Ellen starts with a vowel.</pre> ====<code>for</code> loops inside <code>for</code> loops==== <pre> >>> letters = ["a", "b", "c"] >>> numbers = [1, 2, 3] >>> for letter in letters: ... for number in numbers: ... print letter * number ... a aa aaa b bb bbb c cc ccc >>> for number in numbers: ... for letter in letters: ... print number * letter ... a b c aa bb cc aaa bbb ccc</pre> ====Imports==== Imports look like this: <pre> >>> import random >>> import time</pre> In the above example, <code>random</code> and <code>time</code> are both Python modules. Modules are Python files outside of the current Python file that contain Python code, like functions and variables. You can use code from modules by first importing the module. Here's an example from the <code>random</code> module: <pre> >>> import random >>> random.randint(0, 10) 7 >>> random.randint(0, 10) 6 >>> random.randint(0, 10) 1 >>> random.randint(0, 10) 3 >>> random.randint(0, 10) 4 >>> random.randint(0, 10) 9</pre> <code>randint</code> is a function in the <code>random</code> module. It takes a lower bound as the first argument and an upper bound as the second argument and returns a random integer between those bounds.
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information