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 2016)/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!
==Booleans== Please return to the interactive Python interpreter for the rest of the tutorial. And remember: type out the examples. You'll thank yourself tomorrow. :) [[File:Scales.png|100px]] So far, the code we've written has been <i>unconditional</i>: no choice is getting made, and the code is always run. Python has another data type called a '''boolean''' that is helpful for writing code that makes decisions. There are two booleans: <code>True</code> and <code>False</code>. True type(True) False type(False) You can test if Python objects are equal or unequal. The result is a boolean: 0 == 0 0 == 1 Use <code>==</code> to test for equality. Recall that <code>=</code> is used for <i>assignment</i>. This is an important idea and can be a source of bugs until you get used to it: '''= is assignment, == is comparison'''. Use <code>!=</code> to test for inequality: "a" != "a" "a" != "A" <code><</code>, <code><=</code>, <code>></code>, and <code>>=</code> have the same meaning as in math class. The result of these tests is a boolean: 1 > 0 2 >= 3 -1 < 0 .5 <= 1 You can check for containment with the <code>in</code> keyword, which also results in a boolean: "H" in "Hello" "X" in "Hello" Or check for a lack of containment with <code>not in</code>: "a" not in "abcde" "Perl" not in "Python Workshop"
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