SlideShare a Scribd company logo
Chapter 2
Python for Everybody
ed values such as numbers, letters, and strings, are ca
nstants” because their value does not change
meric constants are as you expect
ng constants use single quotes (')
ouble quotes (")
>>> print(123)
123
>>> print(98.6)
98.6
>>> print('Hell
Hello world
annot use reserved words as variable names / identifi
e await else import pass
break except in raise
class finally is retur
continue for lambda try
def from nonlocal while
rt del global not with
c elif if or yield
able is a named place in the memory where a programmer ca
nd later retrieve the data using the variable “name”
ammers get to choose the names of the variables
an change the contents of a variable in a later statement
12.2
x
14
y
x = 12.2
y = 14
able is a named place in the memory where a programmer ca
nd later retrieve the data using the variable “name”
ammers get to choose the names of the variables
an change the contents of a variable in a later statement
12.2
x
14
y
100
x = 12.2
y = 14
x = 100
t start with a letter or underscore _
t consist of letters, numbers, and underscores
e Sensitive
ood: spam eggs spam23 _speed
ad: 23spam #sign var.12
ifferent: spam Spam SPAM
ce we programmers are given a choice in how we cho
able names, there is a bit of “best practice”
name variables to help us remember what we intend
hem (“mnemonic” = “memory aid”)
s can confuse beginning students because well-named
ables often “sound” so good that they must be keywor
http://en.wikipedia.org/wiki/Mnemonic
ocd = 35.0
afd = 12.50
afd = x1q3z9ocd * x1q3z9afd
x1q3p9afd)
is this bit of
de doing?
ocd = 35.0
afd = 12.50
afd = x1q3z9ocd * x1q3z9afd
x1q3p9afd)
a = 35.0
b = 12.50
c = a * b
print(c)
are these bits
code doing?
ocd = 35.0
afd = 12.50
afd = x1q3z9ocd * x1q3z9afd
x1q3p9afd)
hours = 35.0
rate = 12.50
pay = hours * rate
print(pay)
a = 35.0
b = 12.50
c = a * b
print(c)
are these bits
code doing?
2
x + 2
nt(x)
ble Operator Constant Functi
Assignment statemen
Assignment with exp
Print statement
sign a value to a variable using the assignment statem
ignment statement consists of an expression on the
and side and a variable to store the result
x = 3.9 * x * ( 1 - x )
x = 3.9 * x * ( 1 -
0.6
x
ide is an expression.
expression is evaluated, the
aced in (assigned to) x.
0.6 0
0.4
0.936
is a memory location
ore a value (0.6)
x = 3.9 * x * ( 1 -
0.6 0.93
x
0.4
0.936
de is an expression. Once the
is evaluated, the result is
ssigned to) the variable on the
., x).
s a memory location used to
e. The value stored in a
be updated by replacing the
.6) with a new value (0.936).
0.6 0
cel shading as PDF and Python description
use of the lack of mathematical
ols on computer keyboards - we
computer-speak” to express the
c math operations
sk is multiplication
nentiation (raise to a power) looks
ent than in math
O
Operator
A
+
Su
-
Mul
*
D
/
**
Re
%
xx = 2
xx = xx + 2
print(xx)
yy = 440 * 12
print(yy)
zz = yy / 1000
print(zz)
>>> jj = 23
>>> kk = jj % 5
>>> print(kk)
3
>>> print(4 ** 3)
64
Operator
+
-
*
/
**
%
5 23
4 R 3
20
3
n we string operators together - Python must know whi
first
s called “operator precedence”
h operator “takes precedence” over the others?
x = 1 + 2 * 3 - 4 / 5 ** 6
ecedence rule to lowest precedence rule:
ntheses are always respected
onentiation (raise to a power)
plication, Division, and Remainder
tion and Subtraction
to right
Paren
Po
Multip
Add
Left to
1 + 2 ** 3 /
1 + 8 / 4 *
1 + 2 * 5
1 + 10
11
x = 1 + 2 ** 3 / 4 * 5
print(x)
Parenthesis
Power
Multiplication
Addition
Left to Right
ember the rules top to bottom
n writing code - use parentheses
n writing code - keep mathematical expressions simple
hey are easy to understand
k long series of mathematical operations up to make th
clear
Parenth
Powe
Multiplic
Additi
Left to R
hon variables, literals, and
ants have a “type”
n knows the difference between
eger number and a string
xample “+” means “addition” if
thing is a number and
atenate” if something is a string
>>> ddd = 1 + 4
>>> print(ddd)
5
>>> eee = 'hello '
>>> print(eee)
hello there
concatenate = put
n knows what “type”
thing is
e operations are
bited
annot “add 1” to a string
an ask Python what type
thing is by using the
function
>>> eee = 'hello ' + 't
>>> eee = eee + 1
Traceback (most recent
File "<stdin>", line 1,
<module>
TypeError: can only con
str (not "int") to str
>>> type(eee)
<class'str'>
>>> type('hello')
<class'str'>
>>> type(1)
<class'int'>
>>>
bers have two main types
gers are whole numbers:
-2, 0, 1, 100, 401233
ating Point Numbers have
al parts: -2.5 , 0.0, 98.6, 14.0
e are other number types - they
ariations on float and integer
>>> xx = 1
>>> type (xx
<class 'int'
>>> temp = 9
>>> type(tem
<class'float
>>> type(1)
<class 'int'
>>> type(1.0
<class'float
>>>
n you put an integer and
ng point in an
ssion, the integer is
itly converted to a float
an control this with the
n functions int() and
>>> print(float(99)
199.0
>>> i = 42
>>> type(i)
<class'int'>
>>> f = float(i)
>>> print(f)
42.0
>>> type(f)
<class'float'>
>>>
division produces a floating
sult
>>> print(10 / 2)
5.0
>>> print(9 / 2)
4.5
>>> print(99 / 100
0.99
>>> print(10.0 / 2
5.0
>>> print(99.0 / 1
0.99
as different in Python 2.x
an also use int() and
to convert between
s and integers
will get an error if the string
not contain numeric
cters
>>> sval = '123'
>>> type(sval)
<class 'str'>
>>> print(sval + 1)
Traceback (most recent cal
File "<stdin>", line 1,
TypeError: can only concat
(not "int") to str
>>> ival = int(sval)
>>> type(ival)
<class 'int'>
>>> print(ival + 1)
124
>>> nsv = 'hello bob'
>>> niv = int(nsv)
Traceback (most recent cal
File "<stdin>", line 1, in
ValueError: invalid litera
with base 10: 'x'
can instruct Python to
se and read data from
user using the input()
ction
e input() function
urns a string
nam = input('Who are
print('Welcome', nam)
Who are you? Chuc
Welcome Chuck
e want to read a number
m the user, we must
vert it from a string to a
mber using a type
version function
er we will deal with bad
ut data
inp = input('Europe fl
usf = int(inp) + 1
print('US floor', usf)
Europe floor? 0
US floor 1
ing after a # is ignored by Python
comment?
scribe what is going to happen in a sequence of code
cument who wrote the code or other ancillary informat
n off a line of code - perhaps temporarily
# Get the name of the file and open it
name = input('Enter file:')
handle = open(name, 'r')
# Count word frequency
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1
# Find the most common word
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
# All done
print(bigword, bigcount)
pe
served words
riables (mnemonic)
erators
erator precedence
• Integer Division
• Conversion between
• User input
• Comments (#)
e
Write a program to prompt the user for hours
and rate per hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Acknowledgements / Contributions
re Copyright 2010- Charles R. Severance of the
ichigan School of Information and made available
ve Commons Attribution 4.0 License. Please
st slide in all copies of the document to comply
tion requirements of the license. If you make a
ee to add your name and organization to the list of
this page as you republish the materials.
ment: Charles Severance, University of Michigan
mation
Contributors and Translators here
...

More Related Content

PPTX
PPT_1_9102501a-a7a1-493e-818f-cf699918bbf6.pptx
PPTX
Pythonlearn-02-Expressions123AdvanceLevel.pptx
PDF
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
PPTX
Lec2_cont.pptx galgotias University questions
PDF
03-Variables, Expressions and Statements (1).pdf
PPT
PythonCourse_02_Expressions.ppt Python introduction turorial for beginner.
PPTX
Intro to CS Lec03 (1).pptx
PPTX
unit1.pptx for python programming CSE department
PPT_1_9102501a-a7a1-493e-818f-cf699918bbf6.pptx
Pythonlearn-02-Expressions123AdvanceLevel.pptx
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
Lec2_cont.pptx galgotias University questions
03-Variables, Expressions and Statements (1).pdf
PythonCourse_02_Expressions.ppt Python introduction turorial for beginner.
Intro to CS Lec03 (1).pptx
unit1.pptx for python programming CSE department

Similar to cel shading as PDF and Python description (20)

PPTX
PPt Revision of the basics of python1.pptx
PPTX
Revision-of-thehki-basics-of-python.pptx
PPT
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
PDF
Class 2: Welcome part 2
PPTX
PPTX
Python PPT2
PPT
Input Statement.ppt
PPTX
Review old Pygame made using python programming.pptx
PDF
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
PPTX
Introduction to Python Values, Variables Data Types Chapter 2
PPT
Python programming unit 2 -Slides-3.ppt
PDF
Introduction to Python
PPTX
Python-Certification-Training-Day-1-2.pptx
PPTX
An Introduction To Python - Python Midterm Review
PDF
1_Python Basics.pdf
PDF
Python Objects
PPTX
Python Lecture 2
PPTX
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
PDF
introduction to python programming course 2
PPt Revision of the basics of python1.pptx
Revision-of-thehki-basics-of-python.pptx
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
Class 2: Welcome part 2
Python PPT2
Input Statement.ppt
Review old Pygame made using python programming.pptx
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
Introduction to Python Values, Variables Data Types Chapter 2
Python programming unit 2 -Slides-3.ppt
Introduction to Python
Python-Certification-Training-Day-1-2.pptx
An Introduction To Python - Python Midterm Review
1_Python Basics.pdf
Python Objects
Python Lecture 2
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
introduction to python programming course 2
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Insiders guide to clinical Medicine.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Business Ethics Teaching Materials for college
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Pre independence Education in Inndia.pdf
PDF
Classroom Observation Tools for Teachers
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Types and Its function , kingdom of life
Module 4: Burden of Disease Tutorial Slides S2 2025
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Basic Mud Logging Guide for educational purpose
Renaissance Architecture: A Journey from Faith to Humanism
Insiders guide to clinical Medicine.pdf
Complications of Minimal Access Surgery at WLH
Business Ethics Teaching Materials for college
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
Week 4 Term 3 Study Techniques revisited.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pre independence Education in Inndia.pdf
Classroom Observation Tools for Teachers
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Ad

cel shading as PDF and Python description

  • 2. ed values such as numbers, letters, and strings, are ca nstants” because their value does not change meric constants are as you expect ng constants use single quotes (') ouble quotes (") >>> print(123) 123 >>> print(98.6) 98.6 >>> print('Hell Hello world
  • 3. annot use reserved words as variable names / identifi e await else import pass break except in raise class finally is retur continue for lambda try def from nonlocal while rt del global not with c elif if or yield
  • 4. able is a named place in the memory where a programmer ca nd later retrieve the data using the variable “name” ammers get to choose the names of the variables an change the contents of a variable in a later statement 12.2 x 14 y x = 12.2 y = 14
  • 5. able is a named place in the memory where a programmer ca nd later retrieve the data using the variable “name” ammers get to choose the names of the variables an change the contents of a variable in a later statement 12.2 x 14 y 100 x = 12.2 y = 14 x = 100
  • 6. t start with a letter or underscore _ t consist of letters, numbers, and underscores e Sensitive ood: spam eggs spam23 _speed ad: 23spam #sign var.12 ifferent: spam Spam SPAM
  • 7. ce we programmers are given a choice in how we cho able names, there is a bit of “best practice” name variables to help us remember what we intend hem (“mnemonic” = “memory aid”) s can confuse beginning students because well-named ables often “sound” so good that they must be keywor http://en.wikipedia.org/wiki/Mnemonic
  • 8. ocd = 35.0 afd = 12.50 afd = x1q3z9ocd * x1q3z9afd x1q3p9afd) is this bit of de doing?
  • 9. ocd = 35.0 afd = 12.50 afd = x1q3z9ocd * x1q3z9afd x1q3p9afd) a = 35.0 b = 12.50 c = a * b print(c) are these bits code doing?
  • 10. ocd = 35.0 afd = 12.50 afd = x1q3z9ocd * x1q3z9afd x1q3p9afd) hours = 35.0 rate = 12.50 pay = hours * rate print(pay) a = 35.0 b = 12.50 c = a * b print(c) are these bits code doing?
  • 11. 2 x + 2 nt(x) ble Operator Constant Functi Assignment statemen Assignment with exp Print statement
  • 12. sign a value to a variable using the assignment statem ignment statement consists of an expression on the and side and a variable to store the result x = 3.9 * x * ( 1 - x )
  • 13. x = 3.9 * x * ( 1 - 0.6 x ide is an expression. expression is evaluated, the aced in (assigned to) x. 0.6 0 0.4 0.936 is a memory location ore a value (0.6)
  • 14. x = 3.9 * x * ( 1 - 0.6 0.93 x 0.4 0.936 de is an expression. Once the is evaluated, the result is ssigned to) the variable on the ., x). s a memory location used to e. The value stored in a be updated by replacing the .6) with a new value (0.936). 0.6 0
  • 16. use of the lack of mathematical ols on computer keyboards - we computer-speak” to express the c math operations sk is multiplication nentiation (raise to a power) looks ent than in math O Operator A + Su - Mul * D / ** Re %
  • 17. xx = 2 xx = xx + 2 print(xx) yy = 440 * 12 print(yy) zz = yy / 1000 print(zz) >>> jj = 23 >>> kk = jj % 5 >>> print(kk) 3 >>> print(4 ** 3) 64 Operator + - * / ** % 5 23 4 R 3 20 3
  • 18. n we string operators together - Python must know whi first s called “operator precedence” h operator “takes precedence” over the others? x = 1 + 2 * 3 - 4 / 5 ** 6
  • 19. ecedence rule to lowest precedence rule: ntheses are always respected onentiation (raise to a power) plication, Division, and Remainder tion and Subtraction to right Paren Po Multip Add Left to
  • 20. 1 + 2 ** 3 / 1 + 8 / 4 * 1 + 2 * 5 1 + 10 11 x = 1 + 2 ** 3 / 4 * 5 print(x) Parenthesis Power Multiplication Addition Left to Right
  • 21. ember the rules top to bottom n writing code - use parentheses n writing code - keep mathematical expressions simple hey are easy to understand k long series of mathematical operations up to make th clear Parenth Powe Multiplic Additi Left to R
  • 22. hon variables, literals, and ants have a “type” n knows the difference between eger number and a string xample “+” means “addition” if thing is a number and atenate” if something is a string >>> ddd = 1 + 4 >>> print(ddd) 5 >>> eee = 'hello ' >>> print(eee) hello there concatenate = put
  • 23. n knows what “type” thing is e operations are bited annot “add 1” to a string an ask Python what type thing is by using the function >>> eee = 'hello ' + 't >>> eee = eee + 1 Traceback (most recent File "<stdin>", line 1, <module> TypeError: can only con str (not "int") to str >>> type(eee) <class'str'> >>> type('hello') <class'str'> >>> type(1) <class'int'> >>>
  • 24. bers have two main types gers are whole numbers: -2, 0, 1, 100, 401233 ating Point Numbers have al parts: -2.5 , 0.0, 98.6, 14.0 e are other number types - they ariations on float and integer >>> xx = 1 >>> type (xx <class 'int' >>> temp = 9 >>> type(tem <class'float >>> type(1) <class 'int' >>> type(1.0 <class'float >>>
  • 25. n you put an integer and ng point in an ssion, the integer is itly converted to a float an control this with the n functions int() and >>> print(float(99) 199.0 >>> i = 42 >>> type(i) <class'int'> >>> f = float(i) >>> print(f) 42.0 >>> type(f) <class'float'> >>>
  • 26. division produces a floating sult >>> print(10 / 2) 5.0 >>> print(9 / 2) 4.5 >>> print(99 / 100 0.99 >>> print(10.0 / 2 5.0 >>> print(99.0 / 1 0.99 as different in Python 2.x
  • 27. an also use int() and to convert between s and integers will get an error if the string not contain numeric cters >>> sval = '123' >>> type(sval) <class 'str'> >>> print(sval + 1) Traceback (most recent cal File "<stdin>", line 1, TypeError: can only concat (not "int") to str >>> ival = int(sval) >>> type(ival) <class 'int'> >>> print(ival + 1) 124 >>> nsv = 'hello bob' >>> niv = int(nsv) Traceback (most recent cal File "<stdin>", line 1, in ValueError: invalid litera with base 10: 'x'
  • 28. can instruct Python to se and read data from user using the input() ction e input() function urns a string nam = input('Who are print('Welcome', nam) Who are you? Chuc Welcome Chuck
  • 29. e want to read a number m the user, we must vert it from a string to a mber using a type version function er we will deal with bad ut data inp = input('Europe fl usf = int(inp) + 1 print('US floor', usf) Europe floor? 0 US floor 1
  • 30. ing after a # is ignored by Python comment? scribe what is going to happen in a sequence of code cument who wrote the code or other ancillary informat n off a line of code - perhaps temporarily
  • 31. # Get the name of the file and open it name = input('Enter file:') handle = open(name, 'r') # Count word frequency counts = dict() for line in handle: words = line.split() for word in words: counts[word] = counts.get(word,0) + 1 # Find the most common word bigcount = None bigword = None for word,count in counts.items(): if bigcount is None or count > bigcount: bigword = word bigcount = count # All done print(bigword, bigcount)
  • 32. pe served words riables (mnemonic) erators erator precedence • Integer Division • Conversion between • User input • Comments (#)
  • 33. e Write a program to prompt the user for hours and rate per hour to compute gross pay. Enter Hours: 35 Enter Rate: 2.75 Pay: 96.25
  • 34. Acknowledgements / Contributions re Copyright 2010- Charles R. Severance of the ichigan School of Information and made available ve Commons Attribution 4.0 License. Please st slide in all copies of the document to comply tion requirements of the license. If you make a ee to add your name and organization to the list of this page as you republish the materials. ment: Charles Severance, University of Michigan mation Contributors and Translators here ...