Lecture
2 Variables, Expressions, Statements, and Data Types
Spyder
Shortcuts:
ctrl + L: clear
all the history in the console window
ctrl + 1:
comment/uncomment the selected scripts or the entire line
%reset: clear
all the variables in the memory
ctrl + Enter:
change the line in console without executing the current
code
ctrl + shift +
Enter: start a new line in the script window inspite of the location of
the cursor.
F5: Run the
script.
1.
Hello world
You can print a
text ('string' type data) using either ' ', or " ".
2.
Data Type
Not
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
called floating-point.
3.
Variables:
Programmers
generally choose names for their variables that are
meaningful. They document what the variable is used for.
The underscore
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.
Some
illegal
variable names:
7sixers: it
doesn't begin with a letter
more$: contains
illegal character
class: one of
python keywords.
Python has
twenty-nine keywords:
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.
4.
Operators and operands
Operators are
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
less clear:
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.
5.
Order of operations
a. Parentheses
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.
b.
Exponentiation has the next highest precedence, so 2**1+1 is 3 and
not 4, and 3*1**3 is 3 and not 27.
c.
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).
d. Operators
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.
6.
Operations on strings
Interestingly,
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:
7.
Comments:
1. In the same
row, all script after '#' will be commented (won't be
executed)
2. Shortcut:
ctrl + 1 (number 1, not L)
Tasks:
1. Print the following
text
separately:
- I am a SkyHawk
- What's up, man?
- I am very 'happy' to be in this class
2. Show the
keyboard shortcuts for the following actions (to be used in
Spyder): (Type
these key combinations as comments 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
code.
- Move cursor to a new line after the current line in spite
of the current location of the cursor.
3. Complete the
following calculation in Spyder:
(1)
(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
in Spyder.
(3) If
you
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)
(1)
(2)
(3)
5. Are these
variable names legal? (Answer these questions
using comments in the same script file)
Return
1Result
%result
resultieeeeeeeee
global
yield
rrrrrsulteeeeee
Send a SINGLE
'.py' file to the email.