1 of 9

CS 149

Professor: Alvin Chao

20/5 - 7 + 5*3 - 3

20//5 - 9 + 5*3 - 3

The answer is the number of people out of 10 that will have issues with this problem.

Variables and Expressions

2 of 9

Thonny Shell - Variables + Assignments

Enter the following into a Python Shell in Thonny: Red lines have errors https://strawpoll.com/e7ZJGvQ3vy3

input("enter the mass in grams: ")

mass = input("enter another mass in grams: ")

mass

unit = input("enter the units for mass: ")

print(mass, unit)

ten = 10

print(ten / 2)

abs( - 1)

abs(-1 * 10)�data = 12

data

Data

Data = 34

data

Data

my data = 56

My_data = 78�3data = "hello"

data3 = "world"

data3 = hello

mass = 273 + 100

273 + 100 = mass

mass

Mass + 100

mass -100

3 of 9

Why use float versus int

1. Identify the Python keyword used in a variable declaration to indicate

a) an integer:� b) a floating-point number:

2. Consider numbers of dollar bills, cents, and grams. Which of these units only makes sense as an integer, and why?

3. What would you expect the following statements to print out?

a) print(dollars)

b) print(cents)

c) print(grams)

4. Which side of the equals sign (left or right) was assigned a new value?

5. In your own words, explain how you should read the = sign in Python.For example,the Python statement x = a + b should be read as “x _____ a plus b.”

dollars = int(3)

cents = int(90)

grams = float(3)

4 of 9

Float vs int

For the following questions, assume you have these two variables:

int(x) = 0 �float(y) = 0.0

Questions (10 min)

6. What operator has the lowest precedence?Why do you think Python is designed that way?

7. The + and - operators show up twice in the table of operator precedence. For the Python expression x = 5 * -3; explain how you know whether the - operator is being used as an unary or binary operator in this expression.

y = 9 / 2 versus y = 9 // 2

5 of 9

Division

14. / 4.

Evaluates to

14. / 4

Evaluates to

14 / 4.

Evaluates to

14 / 4

Evaluates to

6 of 9

Floor / regular / remainder division

9 // 4 evaluates to 2 9 / 4.0 evaluates to 2.25 9 % 4 evaluates to 1

10 // 4 evaluates to 2 10 / 4.0 evaluates to 2.5 10 % 4 evaluates to 2

11 // 4 evaluates to 2 11. / 4 evaluates to 2.75 11 % 4 evaluates to 3

12 // 4 evaluates to 3 12 / 4.0 evaluates to 3.0 12 % 4 evaluates to 0

13 // 4 evaluates to 3 13 / 4. evaluates to 3.25 13 % 4 evaluates to 1

14 // 4 evaluates to 3 14.0 / 4 evaluates to 3.5 14 % 4 evaluates to 2

15 // 4 evaluates to 3 15 / 4.0 evaluates to 3.75 15 % 4 evaluates to 3

16 // 4 evaluates to 4 16 / 4. evaluates to 4.0 16 % 4 evaluates to 0

7 of 9

Output

Single line output

# Including end=' ' keeps output on same line

print('Hello,', end=' ')print('this text is all', end=' ')

print('on one line.')

Multiple items in one print

age = 20print('You are', age, 'years old.')

Calculations in a print statement

age = 20print('Next year you will be', age + 1, 'years old.')

8 of 9

Strings and operators

first = "James"

last = "Madison"

full = first + last

quad = first * 4

print(first + last)

print(first, last)

print(first * 4)

print(full)

print(quad)

8.What is the output of these statements:

9 of 9

  • Acknowledgements
  • Parts of this activity are based on materials developed by Helen Hu and Urik Halliday, modified by Chris Mayfield and Nathan Sprague, and licensed under CC BY-NC 4.0 International

</end>