Variables, expressions, and statements.
- distinguish between statements and expressions
- relate variables to real-life tasks such as labeling books
- paraphrase the definition of a computer variable (or name)
- compute simple arithmetic expressions using Python
- manipulate variables by assigning different values to the same variable name
- predict the value of a variable assigned as the result of an arithmetic expression
- sketch a generic diagram of computer memory and several arbitrary Python variables
- write a small script to perform a basic algebraic expression
- summerize your understanding of statements, expressions, and names
- explain why variables are helpful and/or important for computer programmers
Lets look at three components of many programming languages.
Three primary tools we will be working with are:
- Statements - small parts of a program that do not necessarily return a result. A program is formed by a sequence of one or more statements.
- Names (a.k.a. variables) - pointers to data stored in memory.
- Expressions - combination of names, symbols, functions, and/or operators that return another value.
These three aspects are fundamental to programming in general and will be explored throughout this course.
In Python, statements can take many forms and work with many programming elements such as names and operators.
Names are labels that point to a specific item in the computer's memory. Assignment statements are statements where we assign a name to a value, or memory location. Below, a_name points to the memory location of a_value.
>>> a_name = 'a_value'
Names, technically called identifiers, must start with an underscore (the "_" character) or a letter. Names can contain any sequence of letters, numbers, and underscores. Names cannot contain spaces or other special characters. Names are case sensetive, so 'Sunshine' and 'SUNSHINE' are two different names.
There are 33 reserved words that may not be used as names. The following table outlines Python's reserved words.
Expressions typically return some result. The result can then be assigned a name.
In the following example, the expression on the right hand side will be evaluated and then assigned to the name to the left of the '=' assignment operator.
>>> total = 35.9 + 24.29 >>> total 60.19
Basic examples of expressions include arithmetic operations. Operators are included in expressions and consist of symbols such as:
Here are some operators in action:
>>> 2 + 3 # Addition 5 >>> 3 - 2 # Subtraction 1 >>> 5 * 5 # Multiplication 25 >>> 10 / 5 # Division 2 >>> 3 ** 3 # Exponent 27
- Python for Informatics -- Chapter 2 (Slides, Printable Slides, Streaming Video, Download Video, Lecture Audio)
- Review the documents in the learning resources section.
- Be sure to complete the excercises as we are here to practice.
- Copy your completed excercises and share them via a pastebin service such as dPaste.
If you have any questions or difficulties post them here and we will work together to make sure that everyone has a positive learning experience. Please also post ideas and insights into these concepts. We can learn a lot from each others' perspectives and experience :-)