[go: up one dir, main page]

0% found this document useful (0 votes)
18 views40 pages

Chrome

Uploaded by

sjm44w5bfw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views40 pages

Chrome

Uploaded by

sjm44w5bfw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ECS32A

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

Computers Represent Everything in Binary.

Binary numbers look like: 11010100

So how do we know what what this means?

Does that mean 11,010,100?

A different number?

Or a code for a letter?


These are conversion
charts from binary to
numerical values (Decimal)
and normal letters (ASCII).

You do not need to know or


memorize these yet, I have
included them only so you
can see how the binary
numbers can be
interpreted in different
ways, and why types
communicate to the
computer how to use a
binary value.
https://docs.python.org/3/library/stdtypes.html
"Strings" in English
"Strings" in Python
>>> print("Run away Spot.")
Run away Spot.
>>> print('Run, run, run.')
Run, run, run.
>>> print("You can't play here")
You can't play here
>>> print('You can't play here')

SyntaxError: invalid syntax


You can use either 'single' or "double" quotes.
If you want to print a quote or apostrophe be sure and
enclose them using the other type of quote. Fancy paired
quotes like “these” used for printed text won’t work!
(Try it)
Special Characters in Strings

>>> print("Go\nSpot\nGo")
Go
Spot
Go
>>> print("Go\tSpot\tGo")
Go Spot Go

Use \n to print a new line (return or enter key).


Use \t to print a tab (tab key).

These are called whitespace characters.


Why? Because in a book they print as the paper color.
(Try it)
Backslash vs. Forward slash

Used for the


Used for
special characters
division in Python
in strings.
/
\n and \t
Cow Joke
How would you print the joke in Python?

What's a cow's favorite holiday?


Moo years eve.

Can you do it using one line of code?

(Try it)
>>> print("What's a cow's favorite holiday?")
What's a cow's favorite holiday?
>>> print("Moo years eve.")
Moo years eve.

>>> print("What's a cow's favorite holiday?


\nMoo years eve.")
What's a cow's favorite holiday?
Moo years eve.

MOO

Can you print this? The cow said "MOO"


The print() function
• Syntax

print(value1,value2,…,valuen)

• The print function can be called with any number of input


values separated by commas

• If more than one value is given, the values are separated


by a space in the output.

• The output ends with a new line character "\n".

• All values are optional. If no input is given, a blank line is


printed.
Using print()
>>> print("Welcome\nto\nECS32A!")
>>> print("Welcome\nto\nECS32A!")
Welcome
to
>>> print()
ECS32A!
>>> print()
>>> print("Welcome","to","ECS32A!")
>>> print("Welcome","to","ECS32A!")
>>> print("The
Welcome to ECS32A!
answer is", 42)
>>> print("The answer is", 42)
>>> answer
The print("The
is 42
answer is", 6 * 7)
The print("The
>>> answer is 42answer is", 6 * 7)
The answer is 42

(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

• You can use a print statement to print the value of an


expression in a program.

• The shell window will also print the value of an


expression just by itself.
Expressions and Operators
>>> print(4+5)
9

An expression is a short piece of code that can be reduced to a


value. In this example, 4+5 is an arithmetic expression with a
value of 9.

+ is the addition operator.

An operator does computation, like a function. The only reason


there are two ways to do computation (operators and functions) is
that it is more human-readable. The inputs are called operands.

There are also -, *, /, and ** for subtraction, multiplication


division and exponentiation.
Which is the
correct answer?

a) 1
b) 10
c) 16
d) none of the
above
Operator Precedence

• Why does this expression evaluate to 16?

8/2*(2+2)

• prec·e·dence (n)
priority in importance, order, or rank.

• Some operators are evaluated before others

• Otherwise we go left to right


Operator Precedence
• PEMDAS: Each category below is evaluated first from left to
right before moving to the next.

• expressions in Parenthesis ( )

• Exponentiation **

• Multiplication and Division /,*

• Addition and Subtraction +,-

• If not obvious, parenthesize. You can always use


parentheses to explicitly define the order in which operators
are evaluated: (8/2)*(2+2) = 16.0 Expressions in nested
parentheses are evaluated first: 8/(2*(2+2)) = 1.0
Values and Types
Values and Types

• A value is one of the basic things a program


works with, like a number or some text.

• Values can be many different types of data, in


Python every value has a type.

• A type is a category, or class, of values that


determines how it is represented by the computer
and what we can do with it in our programs

• The types in Python we will discuss today are


strings, integers and floating point.
Three Python Types
Three representations of the concept 42.

42 is an integer (whole number)

42.0 is a floating point (fractional number)

"forty two" is a string (text)

Data types are important because they determine what we


can do with the value. We can add integers and floating
point values but can’t add strings.
Integers

• Whole numbers are integers

• 1, 2, 5, 978787, 0, -100 are integers

• What about 8/3 or 0.6?


Floating point numbers

• 0.6, 7.34, and -2.0 are floating point numbers

• Division in Python 3 always evaluates to a floating point

• 7/2 produces the floating point value 3.5

• 6/3 produces the floating point value 2.0

• Addition, subtraction, and multiplication in Python 3


evaluate to a floating point when either operand is
floating point, so 7.0 + 3 produces the floating point
value 10.0.
Example: Weight on the Moon
• Your weight on the Moon is approximately 1/6th your
weight on Earth. A person weighing 100 pounds on
Earth weighs 100/6 pounds on the Moon.

• The comma separated inputs to the print function can


be strings, integers, or floating point values. Nested
expressions are evaluated before the print function is
called.

>>> print("Weight on Earth:", 100)


Weight on Earth: 100
>>> print("Weight on the Moon:", 100/6)
Weight on the Moon: 16.666666666666667
Floating point is not exact
>>> 100/6
16.666666666666667
>>> 100*(1/6)
16.666666666666665
• This is weird...why?

• Computer numbers have a fixed number of


decimal places. They are represented by binary
integers under the hood.

• Exact results with floating point numbers would


have an infinite number of decimal places:
100/6 has the value 16.666666.......
The type() function
The built-in type() function can be used in the
Python shell to get the type of a value.

>>> type('42')
<class 'str'>

>>> type(42)
<class 'int'>

>>> type(42.0)
<class 'float'>

For now think of this word 'class' in the sense of


defining a class (or category) of Python values.
Example: Calculating Tax and Tip
>>> print("Item:", "ramen")
Item: ramen
>>> print("Cost:", 10)
Cost: 10
>>> print("Tax:", 10 * 0.0825)
Tax: 0.8250000000000001
>>> print("Tip:", 10*0.2)
Tip: 2.0
>>> type("ramen")
<class 'str'>
>>> type(10)
<class 'int'>
>>> type(10 * 0.2)
<class 'float'>
(Try it)
Example: Parentheses Required?
3 × (4 − 5) >>> 3 * (4 - 5)

20 + 10
>>> (20 + 10) / 3
3

3 × 100 >>> (3 * 100) / 2


2

7 >>> 7 / (100 * 12)


100 × 12

(Try it)
Example: Parentheses Required?
3 × (4 − 5) >>> 3 * (4 - 5) Yes
-3
20 + 10 >>> (20 + 10) / 3 Yes
3 10.0

3 × 100 >>> 3 * 100 / 2 No


2 150.0
7 >>> 7 / (100 * 12) Yes
100 × 12 0.005833333333333334
Cheat Sheet
Variables

ECS32A
Variables
• We use named variables to store and reference stored
values in a program.

• An assignment statement creates a variable and gives


it a value, or updates the value of an existing variable.

>>> message = "Welcome to 32A"


>>> pi = 3.14
>>> x = 2
>>> print(x)
2

So x = 2 is not algebra! It is an assignment statement.


You can pronounce this "assign 2 to x" to avoid confusion.
Variables
• Metaphor:
x = 5

• Variables names refer to locations in memory that store


data. Python variables can hold any type of data.

• You can think of a variable as a box.

• The name x is the label on the box.


Assignment statement

x = 5

5 5
x

In an assignment statement, the box on the left of = is


filled with the value on the right. The value on the right
could be from an evaluated expression.

You might also like