10/13/2011 CS Variables and Expressions in Python
-
Upload
matt-carlberg -
Category
Documents
-
view
216 -
download
0
Transcript of 10/13/2011 CS Variables and Expressions in Python
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
1/19
Do Now: 10 minutes
Figure out what the % operator does in python.
1. Start by working in the interpreter window. Type random
things like 4%4, 9%2, 10%12, etc. Try to figure out why you
get the each answer.
2. Open a new window to write your own mini-program. The
program should be:
for i in range(10):
print("i is ", i, " and i%4 is ", i%4)
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
2/19
Homework
Hand write lines of Python code that will swap the value of two
variables, no matter what the values of the two variableshappen to be. If you get the chance, test it out.
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
3/19
Expressions
Examples:
2+2+2+2+2
8*6
10-5+6
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
4/19
Expressions
Examples:
2+2+2+2+2
8*6
10-5+6
Evaluating an expression: taking a line like 2+2+2+2+2
and reducing it to a single value, i.e. 10
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
5/19
Expressions
Examples:
2+2+2+2+2
8*6
10-5+6
Evaluating an expression: taking a line like 2+2+2+2+2
and reducing it to a single value, i.e. 8
Order: How does the computer evaluate 3*4 + 2*(8 + 2) ?PEMDAS applies
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
6/19
Variables
Containers that hold the results of our
expressions for later use.
Example: spam = 15
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
7/19
Variables
Containers that hold the results of our
expressions for later use.
Example: spam = 15
The = sign is actually called the assignment
operator
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
8/19
In the interpreter window:
Explanation:
1. Assigning the spam variable to be 152. spam + 5 is the same as 15 + 5 = 20
3. Assigning the spam variable to 3 (15 gets
overwritten)
4. spam + 5 is same as 3 + 5 = 8
>>> spam = 15>>> spam + 5>>> spam = 3
>>> spam + 5
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
9/19
In the interpreter window:
Explanation:1. Assigning 15 to the spam variable
2. spam + spam is same as 15 + 15 = 30
3. spam spam is same as 15 15 = 0
>>> spam = 15>>> spam + spam>>> spam - spam
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
10/19
In the interpreter window:
Explanation:
1. Assigning 15 to the variable spam
2. First, adds spam with spam (15 + 15) and then stores
the result (30) back in spam.
3. First, multiplies spam by 3 (3*30) and then stores the
result (90) back in spam.
>>> spam = 15>>> spam = spam + spam>>> print(spam)
>>> spam = 3*spam>>> print(spam)
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
11/19
Variable Names
One Rule:
-variable names cannot include a space
mattsAge = 26
matts age = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
12/19
Variable Names
One Rule:
-variable names cannot include a space
mattsAge = 26
matts age = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
13/19
Variable Names
One Rule:
-variable names cannot include a space
mattsAge = 26
matts age = 26
Two conventions:
-variable names should be descriptive
stuff = 26
mattsAge = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
14/19
Variable Names
One Rule:
-variable names cannot include a space
mattsAge = 26
matts age = 26
Two conventions:
-variable names should be descriptive
stuff = 26
mattsAge = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
15/19
Variable Names
One Rule:
-variable names cannot include a space
mattsAge = 26
matts age = 26
Two conventions:
-variable names should be descriptive
stuff = 26
mattsAge = 26-use Caps or underscores to separate words
mattsAge = 26
mattsage = 26
matts_age = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
16/19
Variable Names
One Rule:
-variable names cannot include a space
mattsAge = 26
matts age = 26
Two conventions:
-variable names should be descriptive
stuff = 26
mattsAge = 26-use Caps or underscores to separate words
mattsAge = 26
mattsage = 26
matts_age = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
17/19
Variable Names
IN PYTHON, VARIABLE NAMES ARE CASE SENSATIVE.
mattsAge = 26
MattsAge = 26
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
18/19
Variable Names
IN PYTHON, VARIABLE NAMES ARE CASE SENSATIVE.
mattsAge = 26
MattsAge = 26
(you have just created two separate variables, one called
mattsAge and one called MattsAge)
-
8/3/2019 10/13/2011 CS Variables and Expressions in Python
19/19
In the interpreter window:
Explanation:
1. Store the number 15 in myNumber
2. Store the number 30 in yourNumber3. Puts whatever is in yourNumber (30) into myNumber
(15). Now, myNumber is 30. 15 is overwritten.
4. Put whatever is in myNumber (30) into yourNumber
(30). Now, yourNumber is 30.
>>> myNumber = 15>>> yourNumber = 30>>> myNumber = yourNumber>>> yourNumber = myNumber>>> print(myNumber)
>>> print(yourNumber)