1 of 32

Introduction to Python and Programming

Winter 2025

1

Adrian Salguero

2 of 32

Announcements

  • Homework 0 is available - due this Friday at 11:59pm on Gradescope
  • Check-in 0 is available - due this Friday at 11:59pm on Gradescope
  • Coding Practice 1 is available - due next Friday (Jan 17) at 11:59pm on Gradescope
  • Appointment Calendar available for 1-1 meetings with instructor (~15 minutes)

2

3 of 32

3

1. Python is a calculator

2. A variable is a container

3. Different types cannot be compared

4. A program is a recipe

4 of 32

1. Python is a calculator

4

5 of 32

You type expressions.�Python computes their values.

  • 5
  • 3 + 4
  • 44 / 2
  • 2 ** 3
  • 3 * 4 + 5 * 6
    • If precedence is unclear, use parentheses: 3 * (4 + 5) * 6
  • (72 – 32) / 9 * 5

5

Try typing some of these expressions into a python interpreter

6 of 32

An expression is evaluated�from the inside out

  • How many expressions are in this Python code?

6

(72 – 32) / 9.0 * 5

an expression

values

(7232) / 9.0 * 5

(40) / 9.0 * 5

40 / 9.0 * 5

4.44 * 5

22.2

7 of 32

Another evaluation example

7

(7232) / (9.0 * 5)

(40) / (9.0 * 5)

40 / (9.0 * 5)

40 / (45.0)

40 / 45.0

.888

8 of 32

Order of Operations

8

Operator

Description

Precedence

Direction

( )

Parenthesis

1 (Highest Precedence)

**

Exponentiation

2

//, /, *, %

Integer division, Float division, multiplication, and modulus �(same level of precedence)

3

Left to right

+, -

Addition and Subtraction

(same level of precedence)

4 (Lowest Precedence)

Left to right

9 of 32

2. A variable is a container

9

10 of 32

Variables hold values

  • Recall variables from algebra:
    • Let x = 2 …
    • Let y = x …
  • In Python: “varname = expression

pi = 3.14

pi

avogadro = 6 * 10 ** 23

avogadro

22 = x # Error!

  • Not all variable names are permitted

10

Nothing printed from an assignment statement

An expression that can be typed into a python interpreter to be evaluated. Not a statement to put into a python program.

Try typing into a python interpreter

11 of 32

Changing existing variables�(“re-binding” or “re-assigning”)

x = 2

x

y = 2

y

x = 5

x

y

  • =” in an assignment is not a promise of eternal equality
    • This is different than the mathematical meaning of “=”
  • Evaluating an expression gives a new (copy of a) number, rather than changing an existing one

11

An expression that can be typed into a python interpreter to be evaluated. Not a statement to put into a python program.

Nothing printed from an assignment statement

Try typing into a python interpreter

12 of 32

How an assignment is executed

  1. Evaluate the right-hand side to a value
  2. Store that value in the variable

x = 2

print(x)

y = x

print(y)

z = x + 1

print(z)

x = 5

print(x)

print(y)

print(z)

12

State of the computer:

Printed output:

To visualize a program’s execution: http://pythontutor.com A custom link to this program is here

This is a python program that could be typed into a text editor.

13 of 32

How an assignment is executed

  1. Evaluate the right-hand side to a value
  2. Store that value in the variable

x = 2

print(x)

y = x

print(y)

z = x + 1

print(z)

x = 5

print(x)

print(y)

print(z)

13

State of the computer:

Printed output:

x: 2

y: 2

z: 3

x: 5

To visualize a program’s execution: http://pythontutor.com A custom link to this program is here

This is a python program that could be typed into a text editor.

2

2

3

5

2

3

14 of 32

Boolean Expressions�(value is True or False)

22 > 4

22 < 4

22 == 4

x = 100 # Assignment, not conditional!

22 = 4 # Error!

x >= 5

x >= 100

x >= 200

not True

not (x >= 200)

3 < 4 and 5 < 6

4 < 3 or 5 < 6

temp = 72

water_is_liquid = temp > 32 and temp < 212

14

Order of Precedence:

Numeric operators: +, *, **

Mixed operators: <, >=, ==

Boolean operators: not, and, or

Try typing these expressions into a python interpreter

15 of 32

What do you think?

What is printed out by the following Python code:�

  1. print(2 < 7 or 3 > 12)�
  2. print(not ((2 < 3) and (4 > 100)))�

temp = 72

is_liquid = temp > 32 and temp < 212

print(is_liquid)

temp = 300

print(is_liquid)

15

16 of 32

More expressions: strings

A string represents text

'Python'

this_class = "CSE 160"

""

Empty string is not the same as an unbound variable

Operations on strings:

  • Length:

len(this_class)

  • Concatenation:

"CSE" + "160" + 'is' + "fun!"

  • Containment/searching:

'0' in this_class

"O" in this_class

16

Try typing these expressions into a python interpreter

17 of 32

3. Different types cannot be compared

17

18 of 32

Types of values

  • Integers (int): -22, 0, 44
    • Arithmetic is exact

  • Real numbers (float): 2.718, 3.1415
    • float, for “floating point”
    • Arithmetic is approximate

  • Strings (str): "I love Python", ""

  • Truth values (bool): True, False
    • bool, for “Boolean”

18

19 of 32

Operations behave differently�on different types

3.0 + 4.0

3 + 4

3 + 4.0

"3" + "4"

3 + "4" # Error

3 + True # Don’t do this.

Moral: Python sometimes tells you when you do something that does not make sense.

19

Try typing these expressions into a python interpreter

20 of 32

Operations behave differently�on different types

15.0 + 4.0

15 + 4

15.0 + 4

15 + 4.0

Type conversion:

float(15)

int(15.0)

int(15.5)

int("15")

str(15.5)

float(15) + 4

20

Try typing these expressions into a python interpreter

21 of 32

4. A program is a recipe

21

22 of 32

What is a program?

  • A program is a sequence of instructions
  • The computer executes one after the other
  • Saving your work as a program is better than re-typing from scratch

x = 1

y = 2

x + y

print(x + y)

print("The sum of", x, "and", y, "is", x + y)

22

23 of 32

Interlude: The print statement

  • The print statement always prints one line
    • The next print statement prints below that one
  • Write 0 or more expressions inside the parentheses, separated by commas
    • In the output, the values are separated by spaces
  • Examples:

print(3.1415)

print(2.718, 1.618)

print()

print(20 + 2, 7 * 3, 4 * 5)

print("The sum of", x, "and", y, "is", x + y)

23

24 of 32

Expressions, statements, and programs

  • An expression evaluates to a value
    • 3 + 4
    • pi * r ** 2
  • A statement causes an effect
    • pi = 3.14159
    • print(pi)
  • Expressions appear within other expressions and statements
    • (fahr - 32) * (5.0 / 9)
    • print(pi * r ** 2)
  • A statement may not appear within an expression
    • 3 + print(pi) # Error!!
  • A program is made up of statements
    • A program should do something or communicate information
    • Just evaluating an expression does not accomplish either goal

24

25 of 32

25

1. Python is a calculator

2. A variable is a container

3. Different types cannot be compared

4. A program is a recipe

26 of 32

Todos for next lecture

  • Start Homework 0, Check-in 0, and Coding Practice 1
  • Set up your Python environment (Anaconda or VS Code)
  • Attend Section this Thursday
  • Come to OHs or post on Ed if you have any questions

26

27 of 32

Using Gradescope for CSE160

28 of 32

Ungraded

You will see some, but not all of the autograder tests

Can resubmit as many times until the due date

29 of 32

Graded

Automatically Graded

Autograder Score: Points given from all autograder tests

Manually Graded

Autograder Correction: TA’s giving/taking away points in case of autograder error

Internal Correctness: Points given for following directions, good style, efficiency, etc.

Autograder + Autograder Correction + Internal Correctness = Total Score

30 of 32

Comments

Two tabs - results and code. Click on ‘Code’ to see list of files you submitted. Each file will have the number of comments that were made on it.

31 of 32

Comments Example

32 of 32

Resubmissions

Two steps:

  1. Fill out the google form! (Due 11 pm)
  2. Resubmit on gradescope

(Also, please read comments before resubmitting!)