ECS32A F23 Unit2 FirstPrograms
ECS32A F23 Unit2 FirstPrograms
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
x = 5
5
x
y = x
5 5
y x
x = 2
5
2 2 y
x
count = count + 1
count = count + 1
2
3 count +1
Step 1:
Evaluate the expression on the right.
It evaluates to the integer 3
Assignment Statements
count = count + 1
2
3
count
Step 2:
Put the value 3 in the box named count.
This "kicks out" 2.
Assignment Statements
count = count + 1
3
count
2 5
y x
5 5
y x
2
z
2 5
y x
2
z
5 5
y x
2
z
5 2
y x
2
z
5 2
y x
a = 35 meal_cost = 35
b = 0.2 tip_rate = 0.2
c = a * b tip = meal_cost * tip_rate
print(c) print(tip)
Naming Variables
• Use your own meaningful variable names!
cats = 1
count1 = 2
number of cats = 3
cat-count = 4
numCats = 5
cat_number = 6
7cats = 7
CATS = 8
(Try it)
Valid Variables?
cats = 1 Yes
count1 = 2 Yes, numbers OK
number of cats = 3 No, spaces allowed
cat-count = 4 No, underscore only symbol
numCats = 5 Yes, this is camelCase
cat_number = 6 Yes
7cats = 7 No, begins with a number
CATS = 8 Syntax OK, but all caps
reserved for constants
Break
• https://amazingtimer.com/countdown_timer.php
Writing Programs
ECS32A
Terminology: Statement
2
17
xx
x x ++ 117
If you type an expression in the shell window, the interpreter evaluates it
( nds the value) and displays the result:
>>> 1 + 2
3
HW 1 Problem 1
(See the next few slides for how to type this in)
Making a program
(Try it)
Making a program
• Run a program from the run menu
by selecting run module.
(Try it)
ECS32A
Introduction to Programming
Spring Quarter 2023
Unit 2
First Programs
Continued
Dr. Kristian Stevens
Python Tutor
Program Output
Memory
Python Tutor: https://tinyurl.com/rkehapd
Python Tutor
Program Output
Memory
Python Tutor: https://tinyurl.com/rkehapd
Comments
# fahr2cel.py
# Author Name, Partner Name (optional)
#
# Homework assignment 1
# Convert degrees Fahrenheit to Celsius
Building Blocks of
Programs
ECS32A
Framework
Input Output
Get data from the Communicate
“outside world” results to the user
Sequential Execution
Perform statements one after another, in the
order they are written.
Code Reuse
Write a set of instructions once, and then
reuse those instructions as needed
throughout your program
Advanced Operators
ECS32A
The oor division // operator
Also known as integer division because result is rounded
down to the nearest integer. It doesn’t always result in an
integer type value.
>>> 42.0 // 10
4.0
>>> -42 // 10
-5
ECS32A
String Operators
You can’t perform math on strings, even when the strings look like
numbers! These expressions result in errors.
>>> "2"-"1"
File "<pyshell>", line 1, in <module>
"2"-"1"
TypeError: unsupported operand type(s) for -: 'str'
and 'str'
>>> "1"/"3"
File "<pyshell>", line 1, in <module>
"1"/"3"
TypeError: unsupported operand type(s) for /: 'str'
and 'str'
String Operators
The + operator on strings performs string concatenation. It
creates a new string from the end to end join of the two
strings being concatenated.
>>> "2"+"2"
'22'
Welcome to ECS32A!
String Operators
>>> "Spam"*4
'SpamSpamSpamSpam'
>>> 3 * "$"
'$$$'
ECS32A
Temperature Converter
Our temperature converter generates output but does not
get input from the user.
• fahrenheit is a variable
(Try it)
TypeError
42 "42"
is an integer is a string
(Try it)
Introduction to Python’s
Built in Functions
ECS32A
Functions
• Use the help() function in the IDLE shell window to get more
info on how to use a function.
Function Terminology
• When we use a function, we say we call it.
x = int("42")
• int() is a function
x = float("3.2")
>>> str(42)
'42'
>>> str(4.2)
'4.2'
>>> str("forty two")
'forty two'
>>> dollars = 42
>>> "$" + str(dollars)
'$42'
ECS32A
Parrot
This program simply echos user input
# parrot.py
#
# Echo user input
text = input("Enter some text:")
print("You entered '" + text + "'")
Total bill:100.98
Number of diners:3
Each person owes $33.660000000000004
# Output result
print("First class stamps:", stamps)
print("Change:", change, "cents")