2 Variables, Expressions, Statements, and Data Types
ctrl + L: clear
all the history in the console window
ctrl + 1:
comment/uncomment the selected scripts or the entire line
all the variables in the memory
ctrl + Enter:
change the line in console without executing the current
ctrl + shift +
Enter: start a new line in the script window inspite of the location of
F5: Run the
You can print a
text ('string' type data) using either ' ', or " ".
surprisingly, strings belong to the type str and integers belong to
the type int. Less obviously, numbers with a decimal point belong to a
type called float, because these numbers are represented in a format
generally choose names for their variables that are
meaningful. They document what the variable is used for.
character _ can appear in a name. It is often used in
names with multiple words, such as my_name or price_of_tea_in_china.
doesn't begin with a letter
class: one of
You might want
to keep this list handy. If the interpreter complains
about one of your variable names and you don’t know why, see if it is
on this list.
Operators and operands
special symbols that represent computations like addition
and multiplication. The values the operator uses are called operands.
The following are all legal Python expressions whose meaning is more or
The symbols +,
-, and /, and the use of parenthesis for grouping, mean
in Python what they mean in mathematics. The asterisk (*) is the symbol
for multiplication, and **
is the symbol for exponentiation.
When a variable
name appears in the place of an operand, it is replaced
with its value before the operation is performed. Addition,
subtraction, multiplication, and exponentiation all do what you expect.
However, '/' is division, '//' is integer division. Integer division
always rounds DOWN,
even in cases like this where the next integer is very close.
Order of operations
have the highest precedence and can be used to force an
expression to evaluate in the order you want. Since expressions in
parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8.
You can also use parentheses to make an expression easier to read, as
in (minute * 100) / 60, even though it doesn’t change the result.
Exponentiation has the next highest precedence, so 2**1+1 is 3 and
not 4, and 3*1**3 is 3 and not 27.
Multiplication and Division have the same precedence, which is
higher than Addition and Subtraction, which also have the same
precedence. So 2*3-1 yields 5 rather than 4, and 2/3-1 is -1, not 1
(remember that in integer division, 2/3=0).
with the same precedence are evaluated from left to right.
So in the expression minute*100/60, the multiplication happens first,
yielding 5900/60, which in turn yields 98. If the operations had been
evaluated from right to left, the result would have been 59*1, which is
59, which is wrong.
Operations on strings
the + operator does work with strings, although it does
not do exactly what you might expect. For strings, the + operator
represents concatenation, which means joining the two operands by
linking them end-to-end. For example:
1. In the same
row, all script after '#' will be commented (won't be
ctrl + 1 (number 1, not L)
1. Print the following
2. Show the
keyboard shortcuts for the following actions (to be used in
these key combinations as comments in Spyder)
- I am a SkyHawk
- What's up, man?
- I am very 'happy' to be in this class
3. Complete the
following calculation in Spyder:
- Clear all the console history
- Clear all the memory variables
- Comment scripts in the script window
- Change lines in the console without execute the current
- Move cursor to a new line after the current line in spite
of the current location of the cursor.
(2) Type 'import
math', then start a new line and type
'math.exp(1). Run the file (F5). Explain your results in a comment line
understand what you got from the problem above, you should be able to
do this in Spyder:
4. What are the
results for the following scripts? (Answer these questions
using comments in the same script file)
5. Are these
variable names legal? (Answer these questions
using comments in the same script file)
Send a SINGLE
'.py' file to the email.