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

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

PPTX
Revision-of-thehki-basics-of-python.pptx
PraveenaFppt
 
PDF
Class 2: Welcome part 2
Marc Gouw
 
PPTX
Python.pptx
AKANSHAMITTAL2K21AFI
 
PPTX
Python PPT2
Selvakanmani S
 
PPT
Input Statement.ppt
MuhammadJaved672061
 
PPTX
Review old Pygame made using python programming.pptx
ithepacer
 
PDF
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
ArjayBalberan1
 
PPTX
Introduction to Python Values, Variables Data Types Chapter 2
Raza Ul Mustafa
 
PPT
Python programming unit 2 -Slides-3.ppt
geethar79
 
PDF
Introduction to Python
Mohammed Sikander
 
PPTX
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
PPTX
An Introduction To Python - Python Midterm Review
Blue Elephant Consulting
 
PDF
1_Python Basics.pdf
MaheshGour5
 
PDF
Python Objects
MuhammadBakri13
 
PPTX
Python Lecture 2
Inzamam Baig
 
PPTX
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
AbdulQadeerBilal
 
PDF
introduction to python programming course 2
FarhadMohammadRezaHa
 
PPTX
Engineering CS 5th Sem Python Module-1.pptx
hardii0991
 
PDF
Python lecture 03
Tanwir Zaman
 
Revision-of-thehki-basics-of-python.pptx
PraveenaFppt
 
Class 2: Welcome part 2
Marc Gouw
 
Python PPT2
Selvakanmani S
 
Input Statement.ppt
MuhammadJaved672061
 
Review old Pygame made using python programming.pptx
ithepacer
 
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
ArjayBalberan1
 
Introduction to Python Values, Variables Data Types Chapter 2
Raza Ul Mustafa
 
Python programming unit 2 -Slides-3.ppt
geethar79
 
Introduction to Python
Mohammed Sikander
 
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
An Introduction To Python - Python Midterm Review
Blue Elephant Consulting
 
1_Python Basics.pdf
MaheshGour5
 
Python Objects
MuhammadBakri13
 
Python Lecture 2
Inzamam Baig
 
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
AbdulQadeerBilal
 
introduction to python programming course 2
FarhadMohammadRezaHa
 
Engineering CS 5th Sem Python Module-1.pptx
hardii0991
 
Python lecture 03
Tanwir Zaman
 

Recently uploaded (20)

PPTX
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Different types of inheritance in odoo 18
Celine George
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
Introduction to Indian Writing in English
Trushali Dodiya
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
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 ...