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!
==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>. <syntaxhighlight lang="python"> True type(True) False type(False) </syntaxhighlight> You can test if Python objects are equal or unequal. The result is a boolean: <syntaxhighlight lang="python"> 0 == 0 0 == 1 </syntaxhighlight> 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: <syntaxhighlight lang="python"> "a" != "a" "a" != "A" </syntaxhighlight> <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: <syntaxhighlight lang="python"> 1 > 0 2 >= 3 -1 < 0 .5 <= 1 </syntaxhighlight> You can check for containment with the <code>in</code> keyword, which also results in a boolean: <syntaxhighlight lang="python"> "H" in "Hello" "X" in "Hello" </syntaxhighlight> Or check for a lack of containment with <code>not in</code>: <syntaxhighlight lang="python"> "a" not in "abcde" "Perl" not in "Python Workshop" </syntaxhighlight>
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