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!
====compound conditionals: <code>and</code> and <code>or</code>==== You can check multiple expressions together using the '''<code>and</code>''' and '''<code>or</code>''' keywords. If two expressions are joined by an <code>and</code>, they '''both''' have to be <code>True</code> for the overall expression to be <code>True</code>. If two expressions are joined by an <code>or</code>, as long as '''at least one''' is <code>True</code>, the overall expression is <code>True</code>. Try typing these out and see what you get: <pre> 1 > 0 and 1 < 2 </pre> <pre> 1 < 2 and "x" in "abc" </pre> <pre> "a" in "hello" or "e" in "hello" </pre> <pre> 1 <= 0 or "a" not in "abc" </pre> Guess what will happen when you enter these next two examples, and then type them out and see if you are correct. If you have trouble with the indenting, call over a staff member and practice together. It is important to be comfortable with indenting for tomorrow. <pre> temperature = 32 if temperature > 60 and temperature < 75: print("It's nice and cozy in here!") else: print("Too extreme for me.") </pre> <pre> hour = 11 if hour < 7 or hour > 23: print("Go away!") print("I'm sleeping!") else: print("Welcome to the cheese shop!") print("Can I interest you in some choice gouda?") </pre> You can have as many lines of code as you want in <code>if</code> and <code>else</code> blocks; just make sure to indent them so Python knows they are a part of the block.
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