1 of 10

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

Early? Login to Canvas for PI This week:

  • Tue 11pm - Reading Chp 1(Canvas/Runestone)
  • Wed 11pm HW 1.1-1.4(Rune/GS)
  • Thu 11pm Lab 3

2 of 10

Thonny Shell - Variables + Assignments

Enter the following into a Python Shell in Thonny: Red lines have errors

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 10

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 10

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 10

Division

14. / 4.

Evaluates to

14. / 4

Evaluates to

14 / 4.

Evaluates to

14 / 4

Evaluates to

6 of 10

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 10

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 10

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 10

Runestone PI problem

Sign up if you haven’t at:

https://runestone.academy/runestone/default/user/register with your JMU dukes email

Book: jmu_cs149_fall25_py4e_chao

Or login via Canvas link:

https://canvas.jmu.edu/courses/2111786/assignments/20219739

10 of 10

  • 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>