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
Community Data Science Course (Spring 2015)/Day 1 Tutorial
(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!
====if statements==== The simplest way to make a choice in Python is with the <code>if</code> keyword. Here's an example (don't try to type this one, just look at it for now): <code>if 6 > 5:</code><br /> <code>print("Six is greater than five!")</code> That is our first multi-line piece of code, and the way to type it at a Python prompt is a little different. Let's break down how to do this (type this out step by step): <ol> <li>First, type the<br /> <br /> <code>if 6 > 5:</code><br /> <br /> part, and press Enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>>>></code>. This is Python telling us that we are in the middle of a '''code block''', and so long as we indent our code it should be a part of this code block.</li> <li>Press the spacebar 4 times to indent.</li> <li>Type<br /> <br /> <code>print("Six is greater than five!")</code><br /><br /></li> <li>Press Enter to end the line. The prompt will still be a <code>...</code></li> <li>Press Enter one more time to tell Python you are done with this code block. The code block will now execute.</li> </ol> All together, it will look like this: <pre> >>> if 6 > 5: ... print("Six is greater than five!") ... Six is greater than five! </pre> What is going on here? When Python encounters the <code>if</code> keyword, it <i>evaluates</i> the <i>expression</i> following the keyword and before the colon. If that expression is '''True''', Python executes the code in the indented code block under the <code>if</code> line. If that expression is '''False''', Python skips over the code block. In this case, because 6 really is greater than 5, Python executes the code block under the if statement, and we see "Six is greater than five!" printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct: <pre> if 0 > 2: print("Zero is greater than two!") </pre> <pre> if "banana" in "bananarama": print("I miss the 80s.") </pre>
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