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!
==Output== [[File:Pacman.png|100px]] Notice how if you type a 4 and hit enter, the Python interpreter spits a 4 back out: <syntaxhighlight lang="python"> 4 </syntaxhighlight> But if you assign 4 to a variable, nothing is printed: <syntaxhighlight lang="python"> x = 4 </syntaxhighlight> You can think of it as that something needs to get the output. Without an assignment, the winner is the screen. With assignment, the output goes to the variable. You can reassign variables if you want. What do you think will print if you type in: <syntaxhighlight lang="python"> x = 4</syntaxhighlight> <syntaxhighlight lang="python"> x </syntaxhighlight> <syntaxhighlight lang="python"> x = 5 </syntaxhighlight> <syntaxhighlight lang="python"> x </syntaxhighlight> Sometimes reassigning a variable is an accident and causes bugs in programs. <syntaxhighlight lang="python"> x = 3 </syntaxhighlight> <syntaxhighlight lang="python"> y = 4 </syntaxhighlight> <syntaxhighlight lang="python"> x * y </syntaxhighlight> <syntaxhighlight lang="python"> x * x </syntaxhighlight> <syntaxhighlight lang="python"> 2 * x - 1 * y </syntaxhighlight> Order of operations works pretty much like how you learned in school. If you're unsure of an ordering, you can add parentheses like on a calculator: <syntaxhighlight lang="python"> (2 * x) - (1 * y) </syntaxhighlight> Note that the spacing doesn't matter: <syntaxhighlight lang="python"> x = 4 </syntaxhighlight> and <syntaxhighlight lang="python"> x=4 </syntaxhighlight> are both valid Python and mean the same thing. <syntaxhighlight lang="python"> (2 * x) - (1 * y) </syntaxhighlight> and <syntaxhighlight lang="python"> (2*x)-(1*y) </syntaxhighlight> are also both valid and mean the same thing. You should strive to be consistent with whatever spacing you like or a job requires, since it makes reading the code easier. You aren't cheating and skipping typing these exercises out, are you? Good! :)
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