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
Intro to Programming and Data Science (Fall 2024)/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 into a Jupyter notebook is a little different. Let's break down how to do this (type this out step by step): First, type the <code>if 6 > 5:</code>, then hit enter. ''Not'' Shift + Enter, because this time we want to write more than one line before we ask Jupyter to run it! Once you've hit enter, you'll see that your carat is indented already for you; Jupyter understands that you're now inside the <code>if</code> ''code block'', and adjusts your indentation position for you. In Python, all lines of a block are at the same indentation level, and the block ends when you reduce your indentation again. Write <code>print("Six is greater than five!")</code>, then hit enter, and now hit Shift+Enter to run this if statement! <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>You'll notice that your text caret (|) will be indented by four spaces. This is important, and it tells python that you're telling it what to do with your if statement.</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 shift-enter to tell Jupyter to run that block of code.</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