Chrome
Chrome
Introduction to Programming
Fall Quarter 2023
Unit 2
First Programs
Dr. Kristian Stevens
“Programs do things with stuff”
-Mark Lutz
Learning Python
Printing to the Screen
ECS32A
Unpacking our First Line of Python
• The IDLE Python Shell window
• This is a statement executed
by the Python interpreter
• A statement is a unit of code
that has an effect, and this is
the result in blue
• print() is a built-in function
• does some computation (runs some code) on the input in the
parentheses.
• recognize a function by parentheses e.g. f(x)
• "Welcome" is a string. Strings are text (i.e. strings of letters).
• "42" and "9/7" would also be a strings
• Recognize a string by quotes.
Background Info - Why Types
A different number?
>>> print("Go\nSpot\nGo")
Go
Spot
Go
>>> print("Go\tSpot\tGo")
Go Spot Go
(Try it)
>>> print("What's a cow's favorite holiday?")
What's a cow's favorite holiday?
>>> print("Moo years eve.")
Moo years eve.
MOO
print(value1,value2,…,valuen)
(Try it)
Values, Types
& Expressions
ECS32A
Python as a Calculator
Expressions in the IDLE Shell
• You can use the Python Shell window like a calculator
a) 1
b) 10
c) 16
d) none of the
above
Operator Precedence
8/2*(2+2)
• prec·e·dence (n)
priority in importance, order, or rank.
• expressions in Parenthesis ( )
• Exponentiation **
>>> type('42')
<class 'str'>
>>> type(42)
<class 'int'>
>>> type(42.0)
<class 'float'>
20 + 10
>>> (20 + 10) / 3
3
(Try it)
Example: Parentheses Required?
3 × (4 − 5) >>> 3 * (4 - 5) Yes
-3
20 + 10 >>> (20 + 10) / 3 Yes
3 10.0
ECS32A
Variables
• We use named variables to store and reference stored
values in a program.
x = 5
5 5
x