Intro. to Programming Csci 1020
Week 2 - August 28 - Sept 1

Reading notes: Chapter 2: Variables, expressions, and statements

There are many kinds of variables that can be created, but just a few basic types, and these are the three kinds in the chapter.

int : 1, -10, 0, 7132
These are integers, positive and negative whole numbers without a decimal point.
float : 2.3, -5.1132, 6.02e23, 1.03e-44
These are floating point or real numbers, and they always have a decimal point. The e following a decimal number is the power of ten for scientific notation.
6.02e23 stands for $6.02 \times 10^{23}$
str : 'cat', "Mark B.", "can't"
These are strings, symbols between single or double quotes: "string" or 'string', same thing. Note that "can't" is the string can't, so the first quote symbol starts the string, and the next quote symbol or the same kind closes it.

You can add, subtract, multiply, and divide numbers, and there are many other mathematical functions and operations which work for numbers (int or float).

>>>  3 * 5.7

Strings have their own set of operations. For one, you can add strings with + which concatenates them (tacks them together).

>>> name = 'John' + "Smith"
>>> name

Variables are created by using them. name is created above because it is on the left of = and assigned to a concatenation of two strings, and name is then a str type variable. Variables become the type (int, float, or str) when they are assigned a value.

Variable names begin with a letter followed by letters (capial and lowercase) and numbers and underscore _. There are never spaces within a variable name! Python is case sensitive at all times, for everything. Descriptive variable names can be helpful. Some examples:

  number_cats     first_name   FirstName    name1    name2
  interest_rate   Principal    velocity     x1       x2

There are certain reserved words in Python which you should not use for variable names, and your text calls them key words. The entire list is on page 15.

In working through chapter 2, there are interactive sessions which you duplicate by opening an interactive Python session and entering values after each >>> prompt. A script is a text file, a sequence of statements executed when the script is run. All programs are scripts, and scripts are programs, same thing. You create a new file for a program and name it something ending with .py when it is saved somewhere. It is saved, then run.

Here is the script on page 15.

print 1
x = 2
print x

Open an IDLE session, File>>New, type in the script, save it as or something like that on your disk, then run it. The results displays in the IDLE interactive window as on the top of page 16.


That script created the variable x and assigned it a value of 2, and this value of x is kept alive in the session. Thus, in the interactive window, this happens.

>>> x

You should always use comments in your programs to explain you code. At the top of your programs you should always have something like:

# your name     program 2     August 28, 2017

Your IDLE editor usually highlights comments in red, key words (reserved words) in orange, strings as green, and statements in black. These colors can be changed in some editors, if desired, as they are just there to be helpful and not a part of Python.

The # starts a comment till the end of the line. You can put comments after a Python statement, like this.

percent = decimal_in / 100.0   # convert decimal to a percentage

You can also use a multiline comment which is everything between triple quotes:

A multi-line
comment spanning
3 lines.

Extra Reading

Schedule for week 2

Content is neither approved nor reviewed by FDLTCC.