After completing this module, you will be able to:
- Demonstrate how to correct basic syntax errors in Python.
- Demonstrate how to create a variable in Python.
- Demonstrate how to do basic math with variables in Python.
Read & Study
- Read Chapter 2 of Think Python.
- Review Chapter 2, sections 2.1 – 2.7, of Python for Everybody.
- Read the Module 2 Notes from the Professor.
You may like these as supplemental material to this week’s content. Of course, don’t forget to use a search engine to find blog posts, tutorials, articles, and documentation that aligns with your own personal learning style.
- Variable Assignment (Variables in Python)
A more technically oriented introduction to variables in Python.
- Variables and Types
If you are feeling very confident, this is a no-frills crash course into variables and types.
At this point you should already have Python installed on your computer. The next step is to start writing some Python code. Well, technically, the next step is to learn how to start writing some Python code.
So let’s do that now!
Python code is just plain text in a file that ends in `.py`. You can use any plain text editor to write code, but it will be much easier to write code if you use something called a **code editor** or **Integrated Development Environment (IDE).** Technically, a code editor is something that edits plain-text files that offers basic code editing features like _syntax highlighting_ and _code completion_, and an IDE is something that offers a very robust framework of tools for testing, building, and delivering code.
I have been writing code for over twenty years and I will only use an IDE when I am working on a huge project, like a computer game or some enterprise application. I do not believe they are very helpful to new people because it’s just too much to learn–especially for someone who is interested in learning a programming language championed for its rapid learning curve and quick get-things-done design. These days, though, the line between IDE and code editors has become so blurred that if you are using anything besides a basic plain-text editor, chances are it will contain a lightweight version of the features that people love about IDEs–without the memory and learning overhead required.
In this course, I’m leaving it to you to choose the development environment that suits your individual style the best. One of the tasks in this module is to install a code editor. The following section will help explain what they are and why you should care.
### 1.1\. What is a code editor and why should I care?
A [code editor](https://en.wikipedia.org/wiki/Source-code_editor) is software in which you, well, write and edit computer code. The most important features (in my opinion) of a code editor are **syntax****highlighting** and **code completion**. Everything else is great for when you are an actual programmer working with other programmers on big, production code projects. Otherwise, simplicity here is key.
* [Practical Benefits, Syntax Highlighting](https://en.wikipedia.org/wiki/Syntax_highlighting#practical-benefits) _The Wikipedia entry for Syntax Highlighting has a good definition and practical benefits section. Do any of these seem important to you? _* [Syntax Highlighting Off](https://news.ycombinator.com/item?id=11277183) _This is a HackerNews discussion about an article discussing the benefits of not using syntax highlighting. Go figure!_* [Code Completion in Code Editors](https://en.wikipedia.org/wiki/Autocomplete#In_source_code_editors) _Code completion is to computer languages as a citation manager and Grammarly is to a Word document._
The debate over which tools to use for writing code is older than the actual languages themselves. Programmers from all over the world will bicker and argue about which is “best” until the wee hours of the morning. I’m here to tell you that _the tools you use are far less important than the intuition you develop_. In other words, focus on learning how to think about problems the way programmers think about them–and forget about what you use to build the solution. Code is code; and code editor preferences are entirely subjective.
### 1.2\. Installing a code editor
In this module I’m expecting you to do some reading and research online to find a code editor that you want to settle on for this course. The code editors you use throughout your life will likely change over time, so don’t get too hung up on any one particular one. You can always use a different one. You can even use a new editor for each week–it’s entirely up to you.
Here are some ones that I would recommend to new programmers, in no particular order:
* [Notepad++](https://notepad-plus-plus.org/) _Very simple! Syntax highlighting and code completion. That’s it!_* [Visual Studio Code](https://code.visualstudio.com/) _Great if you are comfortable managing extensions and add-ons. I use this at work a lot because it’s pretty and has an integrated console window._* [Atom](https://atom.io/) _A non-Microsoft alternative to VS Code. I believe Atom was open-source a lot earlier than VS Code. Check it out!_
I will call anything that has syntax highlighting and code completion an integrated development environment, since the Python interpreter on the command line is going to provide you with all the tools you need to execute programs, discover errors in your code, and squash bugs (don’t worry–we’ll talk about bugs next week!).
Once you have a code editor installed and you’re comfortable with it, you can continue on.
## 2\. Expressions, Statements, and Variables
Code is written in **statements**. A statement can be thought of as one command to the computer that you tell it in your language of choice. For example, “My age is 34” is a statement that binds the number 34 to a quantity called “age,” and then binds that quantity to me. When I say “My age is 34,” your brain associates that statement with me in the same way a computer might associate a similar statement in Python.
* [What is a statement?](https://en.wikipedia.org/wiki/Statement_(computer_science)) _Remember: just because the language you are reading about is not Python does not mean you can’t learn from it. All computer languages are different ways to express computational instructions to a computer–which means every language can learn something about talking to a computer from every other language._
Statements can be made up of different parts, including **expressions**. An expression is something that _evaluates_ to a value. In other words, “My age is 34” is not an expression because the statement “My age is 34” assigns the value 34 to my age. The two parts of that statement that _are_ expressions? “Age”, which is a value that represents how many years old I am, and “34”, which is a number.
Numbers are expressions because they have a value. An expression could be
or it could be
In both cases, each expression _evaluates_ to the number 9.
* [What is an expression?](https://en.wikipedia.org/wiki/Expression_%28computer_science%29) _After reading through this, how would you describe the difference between a statement and an expression?_* [What is an expression in Python?](https://stackoverflow.com/a/4782649) _Whether you “get it” or not, this is a great explanation of what an expression is–and as a bonus, it’s in Python!_* [What is the difference between an expression and a statement in Python?](https://stackoverflow.com/a/4728147) _More good conversation to help explain both concepts._
One kind of expression you will be using a lot is called a **variable**.
### 2.1\. What is a variable and why do we use them?
A variable is something that stores a value or points to another value in memory.
But don’t take my word for it. Read through [this awesome introduction to variables](https://www.studytonight.com/python/variables-in-python) and then come back here.
Did you read it? Come on, click on that link and go read it!
Great! So you know variables have a type and a value. Variables in computer programming are a lot like variables in math. In fact, the first assignment you’ll be doing in this module is to create a simple calculator.
### 2.2\. A crash course in variable intuition
* Make sure you read and study [Chapter 2](http://greenteapress.com/thinkpython2/html/thinkpython2003.html) of _Think Python_* Variables point to data stored in memory* Variables that store numbers can be treated like variables in math (i.e., you can use arithmetic operators on them and get an expected result)* How should we name variables? Check out [this interactive example](https://repl.it/@professorlawson/variable-names-example)
## 3\. Operators
Every programming language has something called **operators**, which are symbols that the languages use to perform some thing. For example, an assignment operator common to most languages is the equals sign: `=`. Assignment means we are _assigning_ the value of one thing to another.
* [Here’s a quick demo of some operators](https://repl.it/@professorlawson/operators-example)* [Here are all of Python’s operators](https://www.programiz.com/python-programming/operators)
## 4\. Some Notes about Learning how to Program
As you have seen by now, a lot of this course involves you going to a search engine and researching topics and questions yourself. This is not meant to push you away, it’s meant to give you a safe environment to develop the most important skill a programmer can have: **the ability to use a search engine as a reference**. There is just no way to keep all this stuff in our heads, especially as you start adding more and more languages and tools and frameworks and software on top.
Instead of rote memorization, learning how to program is about _developing intuition_ and saving all the easily-referenced stuff for the search engine. Yes, you will end up memorizing lots of things, but that is due to the way in which humans learn. My primary goal for this course is for you to walk away as someone who can confidently tackle a Python project _because you know how to think like about problems the way a programmer does_.
So when you see links to outside resources, **click on them**. Consume them. **Open up a new tab** and type in whatever questions you just came up with when you were reading. There are literally hundreds of thousands of people learning programming every day, and the internet is such a magical and wonderful resource that connects them all with each other and creates a ecosystem of knowledge acquisition and sharing.
_If there is a dead link in these notes, please let me know! Add a comment below.
After learning about the basics of Python syntax–and how indentation is the primary method of organizing code–your assignment is to correct the syntax errors in a given file named “shippingcosts.py”. Step-by-step instructions are also included inside the code file.
This assignment aligns with course objectives 1, 2, 3, & 4, and all of the Module 2 Objectives.
- Create a new Python file called shippingcosts.py
- View the assignment file online, and copy all the code into your version of the file. You are free to use the online Python interpreter to work on the file if you want, but remember you need your own version of the file on your computer to upload to Canvas.
- Follow the directions in the assignment file to complete the assignment.
- Upload the error-free file on this page.
In this module you learned variables, using variables in basic math, and practiced correcting basic Python syntax. We also did our first of many assignments centered around the concept of fixing broken code until all errors are gone.Additional Resources
I wanted to mention that there does exist an online Python interpreter (https://repl.it/languages/python3) that you can use for free if you are ever away from your primary computer but still want to practice coding. All you need is a web browser!Up Next
In the next module, we will learn about string variables and their basic operations, along with keywords and statements called “conditionals” that will help control the flow of the program