Introduction to Basic Python Programming Concepts

undefined
COSC 1306
COMPUTER
SCIENCE AND
PROGRAMMING
 
Jehan-François Pâris
Fall 2017
undefined
THE ONLINE BOOK
CHAPTER II
BASIC PYTHON
 
 
 
 
Values
 
Python programs manipulate values
 "Hello world!"
 "2+3"
 5
 
Values have types
 
Value types (I)
 
>>> type ("Hello world!")
<class 'str'>
>>> type ("5 + 3")
<class 'str'>
>>> type(24)
<class 'int'>
>>> type(3.1416)
<class 'float'>
S
t
r
i
n
g
s
,
 
i
n
t
e
g
e
r
 
a
n
d
f
l
o
a
t
i
n
g
 
p
o
i
n
t
 
a
r
e
 
b
a
s
i
c
 
P
y
t
h
o
n
 
t
y
p
e
s
 
Value types (II)
 
>>> type("3.1416")
<class 'str'>
>>> type('3.1416')
<class 'str'>
>>>
A
n
y
t
h
i
n
g
 
b
e
t
w
e
e
n
m
a
t
c
h
i
n
g
 
q
u
o
t
e
s
 
i
s
a
 
s
t
r
i
n
g
 
Strings
 
D
e
l
i
m
i
t
e
d
 
b
y
 
m
a
t
c
h
i
n
g
 
s
i
n
g
l
e
 
q
u
o
t
e
s
 
(
'
 
'
)
Can contain double quotes (")
'The subject was "touchy", she said'
Delimited by matching double quotes ("…")
Can contain signgle quotes (')
"Let's go!"
Delimited by  three matching double quotes
("""…""")
Can contain almost anything
 
Find the malformed strings
 
"Hello!"
'I'm busy!'
"I like sugar cones.'
"He asked for a "petit noir"."
"""I don't want a "petit noir"."""
'Cookies and cream'
 
Find the malformed strings
 
"Hello!"
'
I
'
m
 
b
u
s
y
!
'
 
s
i
n
g
l
e
 
q
u
o
t
e
 
i
n
s
i
d
e
 
'
'
"
I like sugar cones.
'
 
mismatched quotes
"
He asked for a 
"
petit noir
"
.
"
"""I don't want a "petit noir"."""
'Cookies and cream'
 
Integers
 
0
,
 
1
 
,
2
,
 
 
3
,
 
 
a
n
d
 
-
1
,
 
-
2
,
 
-
3
 
,
 
No separating quotes
I
t
 
i
s
 
1
3
0
0
0
 
a
n
d
 
n
o
t
 
1
3
,
0
0
0
Can handle really huge large numbers
Computations are always exact
Nice feature of Python 3
 
Floating-point numbers
 
0
.
1
,
 
3
.
1
4
1
6
,
 
 
-
5
.
7
5
,
 
But also
.
1
,
 
.
2
5
,
 
.
0
0
1
You could also meet later
1E3
 (for 
1 ×10
3
), 
1E-3
 (for 
0.001
), …
Limited precision
Fine for most applications
 
Find the malformed numbers
 
55,000
0.375
-32
3E3
-.5
3 000
 
Find the malformed numbers
 
55,000
0.375
-32
3E3
-.5
3 000
 
Numbers and strings
 
'
9
'
 
a
n
d
 
9
 
a
r
e
 
t
w
o
 
d
i
f
f
e
r
e
n
t
 
v
a
l
u
e
s
'9'
 means the digit "9"
 
9
 means the number 9
T
h
e
y
 
a
r
e
 
n
o
t
 
e
q
u
a
l
Y
o
u
 
c
a
n
n
o
t
 
u
s
e
 
o
n
e
 
i
n
s
t
e
a
d
 
o
f
 
t
h
e
 
o
t
h
e
r
 
Converting numbers into strings
 
>>> str(9)
'9'
>>> str(3.1416)
'3.1416'
 
Converting strings into int
 
>>>  int("35")
35
>>>
 
int("six")
Does not work
>>> 
int("3.1416")
Does not work
 
Converting strings into float
 
>>> float("3.1416")
3.1416
 
Converting numbers
 
>>> float(3)
3.0
>>> int(3.1416)
3
>>> int(2.90)
2
>>> int(-2.90)
-2
 
Truncates
Does not round up
 
Variables
 
Names that refer to a value
 
pi
 
3.1416
 
Assigning a value
 
name = 'Alice'
 
Value assigned to  variable can change
result = 0
result =  3.1415927
result = 'undefined'
 
 
This is Python, not algebra (I)
 
In Python, Ruby, C, C++, Java, Javascript, …
The = sign does not mean equality
It means an assignment
variable 
  value
When you see
shirt_price = 24.99
 
think of
shirt_price 
 24.99
 
This is Python, not algebra (II)
 
Why?
Using an equal sign for assignments started
with FORTRAN in mid fifties
Equal sign remains easier to type than left
arrow
The symbol for equality is the double equal sign
a == b
 
Variable names
 
 
Can consist of multiple characters:
Must be letters, digits and underscore (“_”)
Cannot start with a digit
Case sensitive
T
o
t
a
l
,
 
t
o
t
a
l
 
a
n
d
 
T
O
T
A
L
 
a
r
e
 
t
h
r
e
e
 
d
i
f
f
e
r
e
n
t
v
a
r
i
a
b
l
e
 
n
a
m
e
s
 
Prohibited variable names
 
Some possible variable names are already used
by Python
Keywords
Here is the full list
 
Variables have types
 
>>> a = "hello!"
>>> type(a)
<class 'str'>
>>> a = 5
>>> type(a)
<class 'int'>
>>> a = 3.5
>>> type(a)
<class 'float'>
 
The type of a variable
is the type of the
value it refers to
 
It can change over
time
 
Find the wrong variable names
 
aleph
Fall16
easy_money
bigBrother
2
s
t
e
p
 
s
t
a
r
t
s
 
w
i
t
h
 
a
 
d
i
g
i
t
Texas2step
raise
 
is a reserved word
 
Find the wrong variable name
 
aleph
Fall16
easy_money
bigBrother
2step
Texas2step
raise
 
Picking good variable names
 
U
s
i
n
g
 
m
n
e
m
o
n
i
c
 
n
a
m
e
s
 
h
e
l
p
s
 
m
a
k
i
n
g
p
r
o
g
r
a
m
s
 
m
o
r
e
 
r
e
a
d
a
b
l
e
 
b
y
 
h
u
m
a
n
s
instructor_name
, 
student__name
Instead of
nm1
, 
nm2
K
e
e
p
 
i
n
 
m
i
n
d
 
t
h
a
t
 
P
y
t
h
o
n
 
d
o
e
s
 
n
o
t
 
u
n
d
e
r
s
t
a
n
d
t
h
e
i
r
 
m
e
a
n
i
n
g
 
Statements and expressions (I)
 
A
 
s
t
a
t
e
m
e
n
t
 
s
p
e
c
i
f
i
e
s
 
a
n
 
a
c
t
i
o
n
 
t
h
a
t
 
t
h
e
 
P
y
t
h
o
n
i
n
t
e
r
p
r
e
t
e
r
 
c
a
n
 
e
x
e
c
u
t
e
print("Hello world!")
count = 0
Typically occupies a line
Basic element of a program
 
Statements and expressions (II)
 
Statements or expressions?
 
>>> name = "Mike"
>>> print(name)
Mike
>>> len(name)
4
>>> nameLength = len(name)
>>> 10 + 3
13
>>> total = 10 + 3
 
Statements or expressions?
 
>
>
>
 
n
a
m
e
 
=
 
"
M
i
k
e
"
 
 
S
t
a
t
e
m
e
n
t
>
>
>
 
p
r
i
n
t
(
n
a
m
e
)
 
 
S
t
a
t
e
m
e
n
t
Mike
>
>
>
 
l
e
n
(
n
a
m
e
)
 
 
E
x
p
r
e
s
s
i
o
n
4
>
>
>
 
n
a
m
e
_
l
e
n
g
t
h
 
=
 
l
e
n
(
n
a
m
e
)
 
 
S
t
a
t
e
m
e
n
t
>
>
>
 
1
0
 
+
 
3
 
 
E
x
p
r
e
s
s
i
o
n
13
>
>
>
 
t
o
t
a
l
 
=
 
1
0
 
+
 
3
 
 
S
t
a
t
e
m
e
n
t
 
Operators and Operands
 
Most of the operators are the same as those you
encountered in HS Algebra but
Multiplication sign is 
*
Multiplication is never explicit
Must distinguish between 
max
 and 
m*a*x
Exponentiation is 
**
Operators obey the same precedence rules as
in HS algebra
 
Examples
 
>>> 5 - 2.5
2.5
>>> 1*2*3
6
>>> 25/2
12.5
>>> 16/2
8.0
I
n
 
P
y
t
h
o
n
 
t
h
e
 
d
i
v
i
s
i
o
n
o
p
e
r
a
t
o
r
 
a
l
w
a
y
s
r
e
t
u
r
n
s
 
a
 
f
l
o
a
t
 
>>> 5//4
1
 
 
 
(and not 
1.25
)
>>> 8//4
2
 
(
a
n
d
 
n
o
t
 
2
.
0
)
>
>
>
 
6
.
0
/
/
4
1.0
 
  
(and not 
 1.5
)
>>> 3.5//2
1.0
 
  
(and not 
1.75
)
 
Integer division  (//)
 
The modulus operator (%)
 
a
%
b
 
r
e
t
u
r
n
s
 
t
h
e
 
r
e
m
a
i
n
d
e
r
 
o
f
 
t
h
e
 
i
n
t
e
g
e
r
d
i
v
i
s
i
o
n
 
o
f
 
a
 
b
y
 
b
>>> 9//4
2
>>> 9 - (9//4)*4
1
>>> 9%4
1
 
The modulus operator (%)
 
Also work for floating-point numbers
>>> 4.6//2
2.0
>>> 4.6%2
0.5999999999999996
F
l
o
a
t
i
n
g
-
p
o
i
n
t
 
c
o
m
p
u
t
a
t
i
o
n
 
w
a
s
n
o
t
 
a
c
c
u
r
a
t
e
 
b
u
t
 
f
a
i
r
l
y
 
c
l
o
s
e
 
Our friend the input() function (I)
 
Specific to Python
xyz = input("Blablabla: ")
Will print 
Blablabla:
_
W
i
l
l
 
s
t
o
r
e
 
i
n
 
x
y
z
 
t
h
e
 
c
h
a
r
a
c
t
e
r
s
 
y
o
u
 
w
i
l
l
 
e
n
t
e
r
 
Our friend the input() function (II)
 
T
h
e
 
v
a
l
u
e
 
r
e
t
u
r
n
e
d
 
b
y
 
i
n
p
u
t
 
w
i
l
l
 
a
l
w
a
y
s
 
b
e
 
a
s
t
r
i
n
g
price = enter("Enter a price: ")
 
If you want an 
int
 or a 
float
 you must convert
the string
p
r
i
c
e
 
=
 
f
l
o
a
t
(
e
n
t
e
r
(
"
E
n
t
e
r
 
a
 
p
r
i
c
e
:
 
"
)
)
Note the two closing parentheses
 
 
Wishing a happy birthday (I)
 
We want to print a personalized birthday wish
Happy birthday Michael
User will enter the name of the person
Program will do the rest
Will also ask the program to prompt the user for
a name
 
Enter a name:
 
Wishing a happy birthday (II)
 
name = input("Enter a name:")
print("Happy birthday", name)
 
When you specify several items as the
arguments of a print function, print separates
them with spaces
>>> print ("Hello", "world!")
Hello world!
 
The concatenation operator
 
T
h
e
 
s
a
m
e
 
+
 
a
s
 
f
o
r
 
a
d
d
i
t
i
o
n
"
U
"
 
+
 
"
H
"
 
 
r
e
t
u
r
n
s
 
"
U
H
"
"
H
o
w
"
 
+
 
"
a
r
e
"
 
+
 
y
o
u
"
 
+
 
"
?
"
 
r
e
t
u
r
n
s
"
H
o
w
a
r
e
y
o
u
?
"
N
o
 
s
p
a
c
e
s
 
a
r
e
 
a
d
d
e
d
!
"
H
o
w
_
"
 
+
 
"
a
r
e
_
"
 
+
 
y
o
u
_
"
 
+
 
"
?
"
 
r
e
t
u
r
n
s
"
H
o
w
 
a
r
e
 
y
o
u
?
"
 
It works only with strings!
 
Order of operations
 
Consider
a*b**2 +c*d/4 – e/f
In which order are the operations performed?
In the same order as in algebra
F
i
r
s
t
 
e
x
p
o
n
e
n
t
i
a
t
i
o
n
s
 
r
i
g
h
t
 
t
o
 
l
e
f
t
T
h
e
n
 
m
u
l
t
i
p
l
i
c
a
t
i
o
n
s
 
a
n
d
 
d
i
v
i
s
i
o
n
s
l
e
f
t
 
t
o
 
r
i
g
h
t
F
i
n
a
l
l
y
 
a
d
d
i
t
i
o
n
s
 
a
n
d
 
s
u
b
t
r
a
c
t
i
o
n
s
 
 
l
e
f
t
 
t
o
 
r
i
g
h
t
Parentheses override these priorities
 
Precedence rules
**
*, / and //
+ and -
 
With or without parentheses?
 
Which expressions are equivalent?
a + (b – c)
   
a + b – c
(a*b)**2
   
a*b**2
(a + b)/2
   
a + b/2
a/(b*c)
   
a/b*c
(a*b)/c
   
a*b/c
(a**b)**c
   
a**b**c
a**(b**c)
   
a**(b**c)
 
The answers
 
Which expressions are equivalent?
a + (b – c)
  
a + b – c
(a*b)**2
   
a*b**2
  = a*(b**2)
(a + b)/2
   
a + b/2
 = a + (b/2)
a/(b*c)
   
a/b*c
   = (a/b)*c
(a*b)/c
   
a*b/c
(a**b)**c
   
a**b**c
 = a**(b**c)
a**(b**c)
   
a**b**c
 
My take
 
Know the rules
For the quiz
For deciphering complex expressions
Use wisely parentheses in your code
When it is needed
When it clarifies dubious cases
(
a
*
b
)
/
c
a
*
*
(
b
*
*
c
)
 
Remember
 
 
Nobody ever failed a test because
he or she used a few extra
parentheses
 
The reverse is not true
 
Reassignment
 
Including variables
At the beginning of the season
shirt_price = 24.99
Then discounted
shirt_price = 12.99
Finally red-tagged
shirt_price = 7.99
 
Updating a variable (I)
 
Coffeehouse tab
Open tab:
tab = 0
Add a latte:
tab = tab + 4.95
Add sales tax
tab =  tab + 4.95*0.0825
 
 A short cut
 
Rather than typing
tab = tab + 4.95
 
you can type
tab += 4.95
It works for +, -, * and /
price /= 2
 
Updating a variable revisited
 
>>> tab = 0
>>> tab
0
>>> tab += 4.95
>>> tab
4.95
>>> tab += 4.95*0.0825
>>> tab
5.3583750000000006
>>>
 
A warning
 
Unlike C, C++, Java, …
A
u
t
o
i
n
c
r
e
m
e
n
t
s
 
(
i
+
+
,
 
+
+
i
)
 
a
n
d
a
u
t
o
d
e
c
r
e
m
e
n
t
s
 
(
i
-
-
,
 
-
-
i
)
 
a
r
e
 
n
o
t
 
a
l
l
o
w
e
d
C
a
n
n
o
t
 
u
s
e
 
t
h
e
m
 
No
i++?
 
Review
 
a
s
s
i
g
n
m
e
n
t
 
s
t
a
t
e
m
e
n
t
n
 
=
 
n
 
+
 
1
 
 
(
m
e
a
n
s
 
n
 
 
n
 
+
1
)
a
s
s
i
g
n
m
e
n
t
 
t
o
k
e
n
=
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
(
m
e
a
n
s
 
)
d
a
t
a
 
t
y
p
e
A
 
c
l
a
s
s
 
o
f
 
v
a
l
u
e
s
:
 
i
n
t
,
 
f
l
o
a
t
,
 
s
t
r
,
 
 
Review
 
e
x
p
r
e
s
s
i
o
n
An algebraic/string expression that produces
a result are evaluated to give that result.
f
l
o
a
t
Stores 
floating-point
 numbers.
Only approximate values.
i
n
i
t
i
a
l
i
z
a
t
i
o
n
 
(
o
f
 
a
 
v
a
r
i
a
b
l
e
)
To give it an initial value.
E
s
s
e
n
t
i
a
l
 
Review
 
i
n
t
H
o
l
d
s
 
p
o
s
i
t
i
v
e
 
a
n
d
 
n
e
g
a
t
i
v
e
 
w
h
o
l
e
 
n
u
m
b
e
r
s
.
i
n
t
e
g
e
r
 
d
i
v
i
s
i
o
n
Yields only the whole number of times that its
numerator is divisible by its denominator
k
e
y
w
o
r
d
A word that  you cannot use as  a variable
names
 
Review
 
r
u
l
e
s
 
o
f
 
p
r
e
c
e
d
e
n
c
e
Tells in which order expressions are evaluated
s
t
r
i
n
g
H
o
l
d
s
 
a
 
s
t
r
i
n
g
 
o
f
 
c
h
a
r
a
c
t
e
r
s
.
t
y
p
e
 
c
o
n
v
e
r
s
i
o
n
 
f
u
n
c
t
i
o
n
i
n
t
(
)
,
 
f
l
o
a
t
(
)
,
 
s
t
r
(
)
.
 
Review
 
v
a
l
u
e
A number or string (or other things to be
discussed later) that can be stored in a
variable or computed in an expression.
v
a
r
i
a
b
l
e
A name that refers to a value.
v
a
r
i
a
b
l
e
 
n
a
m
e
M
u
s
t
 
s
t
a
r
t
 
w
i
t
h
 
a
 
l
e
t
t
e
r
 
(
a
.
.
z
,
 
A
.
.
Z
,
 
a
n
d
 
_
)
C
a
n
 
c
o
n
t
a
i
n
 
d
i
g
i
t
s
(
0
.
.
9
)
Slide Note
Embed
Share

In this Fall 2017 course, COSC 1306 - Computer Science and Programming by Jehan-François Périr, students delve into the fundamental concepts of Python programming. The course covers values, data types, strings, and identifying malformed strings in Python code.

  • Python Programming
  • COSC 1306
  • Data Types
  • Strings
  • Malformed Strings

Uploaded on Sep 16, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-Fran ois P ris jfparis@uh.edu Fall 2017

  2. THE ONLINE BOOK CHAPTER II BASIC PYTHON

  3. Values Python programs manipulate values "Hello world!" "2+3" 5 Values have types

  4. Value types (I) >>> type ("Hello world!") <class 'str'> >>> type ("5 + 3") Strings, integer and floating point are basic Python types <class 'str'> >>> type(24) <class 'int'> >>> type(3.1416) <class 'float'>

  5. Value types (II) >>> type("3.1416") <class 'str'> >>> type('3.1416') <class 'str'> >>> Anything between matching quotes is a string

  6. Strings Delimited by matching single quotes (' ') Can contain double quotes (") 'The subject was "touchy", she said' Delimited by matching double quotes (" ") Can contain signgle quotes (') "Let's go!" Delimited by three matching double quotes (""" """) Can contain almost anything

  7. Find the malformed strings "Hello!" 'I'm busy!' "I like sugar cones.' "He asked for a "petit noir"." """I don't want a "petit noir".""" 'Cookies and cream'

  8. Find the malformed strings "Hello!" 'I'm busy!' single quote inside ' ' "I like sugar cones.' mismatched quotes "He asked for a "petit noir"." """I don't want a "petit noir".""" 'Cookies and cream'

  9. Integers 0, 1 ,2, 3, and-1, -2, -3 , No separating quotes It is 13000 and not 13,000 Can handle really huge large numbers Computations are always exact Nice feature of Python 3

  10. Floating-point numbers 0.1, 3.1416, -5.75, But also .1, .25, .001 You could also meet later 1E3 (for 1 103), 1E-3 (for 0.001), Limited precision Fine for most applications

  11. Find the malformed numbers 55,000 0.375 -32 3E3 -.5 3 000

  12. Find the malformed numbers 55,000 0.375 -32 3E3 -.5 3 000

  13. Numbers and strings '9' and 9 are two different values '9' means the digit "9" 9 means the number 9 They are not equal You cannot use one instead of the other

  14. Converting numbers into strings >>> str(9) '9' >>> str(3.1416) '3.1416'

  15. Converting strings into int >>> int("35") 35 >>>int("six") Does not work >>> int("3.1416") Does not work

  16. Converting strings into float >>> float("3.1416") 3.1416

  17. Converting numbers >>> float(3) 3.0 >>> int(3.1416) 3 >>> int(2.90) 2 >>> int(-2.90) -2 Truncates Does not round up

  18. Variables Names that refer to a value pi 3.1416

  19. Assigning a value name = 'Alice' Value assigned to variable can change result = 0 result = 3.1415927 result = 'undefined'

  20. This is Python, not algebra (I) In Python, Ruby, C, C++, Java, Javascript, The = sign does not mean equality It means an assignment variable value When you see shirt_price = 24.99 think of shirt_price 24.99

  21. This is Python, not algebra (II) Why? Using an equal sign for assignments started with FORTRAN in mid fifties Equal sign remains easier to type than left arrow The symbol for equality is the double equal sign a == b

  22. Variable names Can consist of multiple characters: Must be letters, digits and underscore ( _ ) Cannot start with a digit Case sensitive Total, total and TOTAL are three different variable names

  23. Prohibited variable names Some possible variable names are already used by Python Keywords Here is the full list and as assert def del elif finally for from in is lambda pass raise return yield True False break else global nonlocal not try None class except if continue exec import or with while

  24. Variables have types The type of a variable is the type of the value it refers to >>> a = "hello!" >>> type(a) <class 'str'> >>> a = 5 >>> type(a) <class 'int'> >>> a = 3.5 >>> type(a) <class 'float'> It can change over time

  25. Find the wrong variable names aleph Fall16 easy_money bigBrother 2step starts with a digit Texas2step raise is a reserved word

  26. Find the wrong variable name aleph Fall16 easy_money bigBrother 2step Texas2step raise

  27. Picking good variable names Using mnemonic names helps making programs more readable by humans instructor_name, student__name Instead of nm1, nm2 Keep in mind that Python does not understand their meaning

  28. Statements and expressions (I) A statement specifies an action that the Python interpreter can execute print("Hello world!") count = 0 Typically occupies a line Basic element of a program

  29. Statements and expressions (II) An expression is Python's equivalent of an algebraic expression Three differences They are always evaluated They use somewhat different conventions ?2 + 2?? + ?2becomes a**2 + 2*a*b + b**2 They are more general

  30. Statements or expressions? >>> name = "Mike" >>> print(name) Mike >>> len(name) 4 >>> nameLength = len(name) >>> 10 + 3 13 >>> total = 10 + 3

  31. Statements or expressions? >>> name = "Mike"Statement >>> print(name) Mike >>> len(name) 4 >>> name_length = len(name)Statement >>> 10 + 3 13 >>> total = 10 + 3Statement Statement Expression Expression

  32. Operators and Operands Most of the operators are the same as those you encountered in HS Algebra but Multiplication sign is * Multiplication is never explicit Must distinguish between max and m*a*x Exponentiation is ** Operators obey the same precedence rules as in HS algebra

  33. Examples >>> 5 - 2.5 2.5 >>> 1*2*3 6 >>> 25/2 12.5 >>> 16/2 8.0 In Python the division operator always returns a float

  34. Integer division (//) >>> 5//4 1 >>> 8//4 2 >>> 6.0//4 1.0 >>> 3.5//2 1.0 (and not 1.25) (and not 2.0) (and not 1.5) (and not 1.75)

  35. The modulus operator (%) a%b returns the remainder of the integer division of a by b >>> 9//4 2 >>> 9 - (9//4)*4 1 >>> 9%4 1

  36. The modulus operator (%) Also work for floating-point numbers >>> 4.6//2 2.0 >>> 4.6%2 0.5999999999999996 Floating-point computation was not accurate but fairly close

  37. Our friend the input() function (I) Specific to Python xyz = input("Blablabla: ") Will print Blablabla:_ Will store in xyzthe characters you will enter

  38. Our friend the input() function (II) The value returned by input will always be a string price = enter("Enter a price: ") If you want an int or a float you must convert the string price = float(enter("Enter a price: ")) Note the two closing parentheses

  39. Wishing a happy birthday (I) We want to print a personalized birthday wish Happy birthday Michael User will enter the name of the person Program will do the rest Will also ask the program to prompt the user for a name Enter a name:

  40. Wishing a happy birthday (II) name = input("Enter a name:") print("Happy birthday", name) When you specify several items as the arguments of a print function, print separates them with spaces >>> print ("Hello", "world!") Hello world!

  41. The concatenation operator The same + as for addition "U" + "H"returns"UH" "How" + "are" + you" + "?"returns "Howareyou?" No spaces are added! "How_" + "are_" + you_" + "?"returns "How are you?" It works only with strings!

  42. Order of operations Consider a*b**2 +c*d/4 e/f In which order are the operations performed? In the same order as in algebra First exponentiationsright to left Then multiplications and divisions left to right Finally additions and subtractions left to right Parentheses override these priorities

  43. Precedence rules ** *, / and // + and -

  44. With or without parentheses? Which expressions are equivalent? a + (b c) a + b c (a*b)**2 a*b**2 (a + b)/2 a + b/2 a/(b*c) a/b*c (a*b)/c a*b/c (a**b)**c a**b**c a**(b**c) a**(b**c)

  45. The answers Which expressions are equivalent? a + (b c) a + b c (a*b)**2 a*b**2 = a*(b**2) (a + b)/2 a + b/2 = a + (b/2) a/(b*c) a/b*c = (a/b)*c (a*b)/c a*b/c (a**b)**c a**b**c = a**(b**c) a**(b**c) a**b**c

  46. My take Know the rules For the quiz For deciphering complex expressions Use wisely parentheses in your code When it is needed When it clarifies dubious cases (a*b)/c a**(b**c)

  47. Remember Nobody ever failed a test because he or she used a few extra parentheses The reverse is not true

  48. Reassignment Including variables At the beginning of the season shirt_price = 24.99 Then discounted shirt_price = 12.99 Finally red-tagged shirt_price = 7.99

  49. Updating a variable (I) Coffeehouse tab Open tab: tab = 0 Add a latte: tab = tab + 4.95 Add sales tax tab = tab + 4.95*0.0825

  50. A short cut Rather than typing tab = tab + 4.95 you can type tab += 4.95 It works for +, -, * and / price /= 2

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#