SlideShare a Scribd company logo
what if…
Basics: conditionals
The plan!
Basics: data types (and operations & calculations) ← recap
Basics: conditionals & iteration
Basics: lists, tuples, dictionaries
Basics: writing functions
Reading & writing files: opening, parsing & formats
Working with numbers: numpy & scipy
Making plots: matplotlib & pylab
… if you guys want more after all of that …
Writing better code: functions, pep8, classes
Working with numbers: Numpy & Scipy (advanced)
Other modules: Pandas & Scikit-learn
Interactive notebooks: ipython & jupyter
Advanced topics: virtual environments & version control
recap: ints, floats, strings & booleans
---- file contents ----
from __future__ import division
# the “+” operation works differently on strings and integers
print “Python”, 1 + 3, “the win!”
print “1” + “3”, “is an unlucky number.”
# integers (and floats) are great for maths
# we can use variables to store values
two = 3 - 1
print “Python is”, two “much fun.”
# strings are for text manipulation
first_numbers = “1” + “2” + “3”
print “The first few numbers are:”, first_numbers
print “Lots of nonsense:” first_numbers * 123
ints & floats
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
strings
+ concatenate
* copy
… no spaces
… cannot begin with number: 1, 2 …
… cannot contain ?$@# (except _)
… cannot be a “built-in” name: and, or,
int, float, str, char, exec, file, open,
object, print, quit… (approx. 80
reserved names)
recap: ints, floats, strings & booleans
---- file contents ----
from __future__ import division
# “raw_input” always returns a string
number = raw_input(“Gimme a number:”)
print “Here is your number 3 times:” number * 3
# we can use functions to check and change the types
print “number is a:” type(number)
new_number = int(number)
print “new_number is a:” type(new_number)
print “Here is your number times 3 :” new_number * 3
# boolean values and equality testing
number = 3
print “Does number equal 3?”, number == 3
print “Is number > 10? ” number ?”, 3 > 10
int() convert to an integer
str() convert to a string
float() convert to a float
type() determine the type of a variable
= = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
Some exercises...
- Write a script that asks the user for their height (in meters), and prints their
height in imperial:
- version 1: prints the result using only feet (1m = 3.28084ft)
- version 2: prints the result using feet and inches, to the nearest inch. (1ft = 12 inches)
Making choices
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
# here comes an “if else” block
if chairs >= lions:
# everything “indented” is part of the “true” block
print “You should be fine.”
# so you can write as much code as you want
print “Really, you’ll be fine!”
else:
print “Run away!!!”
# structure of an “if else” block
if boolean :
the “True” code goes here
else:
the “False” code goes here
Making choices: elif
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
# here comes an “if else” block.
if chairs > lions:
# everything “indented” is part of the “true” block
print “You should be fine.”
# so you can write as much code as you want
print “Really, you’ll be fine!”
elif chairs == lions: # “elif” is short for “else, if”
print “You’re OK.”
print “But should get some more chairs soon!”
else:
print “Run away!!!”
# structure of an “if elif else” block
# Only ONE block is executed!
if boolean :
the “True” code goes here
elif boolean :
next “True” code goes here
else:
the “False” code goes here
Making complex choices: nesting
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
hungry = raw_input(“Are the lions hungry? (yes/no)?:”)
# only worry about chairs if the lions are hungry
if hungry == ‘yes’:
# you can “nest” one “if” in another
if chairs > lions:
# notice the extra indentation?
print “You’ll be fine!”
else:
print “Run away!”
else:
print “The lions are not hungry right now.”
# structure of an “if else” block
if boolean :
if boolean :
… code goes here
else:
… code goes here
else:
… code goes here
Making complex choices: nesting
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
traps = int(raw_input(“How many traps do you have?:”))
bears = int(raw_input(“How many bears are there?:”))
# assume traps work against bears
# and chairs against lions
if (traps >= bears) and (chairs >= lions):
print “You’ll be fine!”
else:
print “Run away!”
# structure of an “if else” block
if (boolean and/or boolean…) :
the “True” code goes here
elif (boolean and/or boolean…) :
next “True” code goes here
else:
the “False” code goes here
Making complex choices: nesting
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
traps = int(raw_input(“How many traps do you have?:”))
bears = int(raw_input(“How many bears are there?:”))
# assume traps work against bears
# and chairs against lions
if (traps < bears) or (chairs < lions):
print “Run away!”
else:
print “You’ll be fine!”
# structure of an “if else” block
if (boolean and/or boolean…) :
the “True” code goes here
elif (boolean and/or boolean…) :
next “True” code goes here
else:
the “False” code goes here
Let’s write the following variants of the lion/chair/hungry program. They start as
above, but:
- If the ‘user’ does not write ‘yes’ or ‘no’ when answering ‘hungry?’, tells them that
their choice is not valid, and nothing else.
Exercise!
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
hungry = raw_input(“Are the lions hungry? (yes/no)?:”)
Moar exercises
- Write a script that asks the user 3 times to insert a number: First hours, next
minutes, and last seconds.
- The script should output the time in seconds only.
- Bonus: Write a script that does the opposite (i.e asks for seconds, and writes hr/min/sec)

More Related Content

What's hot

Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
Matt Harrison
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
Eueung Mulyana
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
Tiji Thomas
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
Kálmán "KAMI" Szalai
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
Priyanka Pradhan
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
Paige Bailey
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
Fariz Darari
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
Rohan Gupta
 
An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutes
Sumit Raj
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Python in 90 minutes
Python in 90 minutesPython in 90 minutes
Python in 90 minutes
Bardia Heydari
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
Damian T. Gordon
 
Python review2
Python review2Python review2
Python review2
vibrantuser
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
Go Asgard
 
Python review2
Python review2Python review2
Python review2
vibrantuser
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
Assem CHELLI
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
Panimalar Engineering College
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
Chiyoung Song
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban KGetting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
GDSCKYAMBOGO
 

What's hot (20)

Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutes
 
Python basic
Python basicPython basic
Python basic
 
Python in 90 minutes
Python in 90 minutesPython in 90 minutes
Python in 90 minutes
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
 
Python review2
Python review2Python review2
Python review2
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
 
Python review2
Python review2Python review2
Python review2
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban KGetting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
 

Viewers also liked

Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
Marc Gouw
 
Class 2: Welcome part 2
Class 2: Welcome part 2Class 2: Welcome part 2
Class 2: Welcome part 2
Marc Gouw
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
Marc Gouw
 
Class 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibClass 8b: Numpy & Matplotlib
Class 8b: Numpy & Matplotlib
Marc Gouw
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programming
Marc Gouw
 
Class 8a: Modules and imports
Class 8a: Modules and importsClass 8a: Modules and imports
Class 8a: Modules and imports
Marc Gouw
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionaries
Marc Gouw
 
Class 7b: Files & File I/O
Class 7b: Files & File I/OClass 7b: Files & File I/O
Class 7b: Files & File I/O
Marc Gouw
 
Introduction to SlideShare for Businesses
Introduction to SlideShare for BusinessesIntroduction to SlideShare for Businesses
Introduction to SlideShare for Businesses
SlideShare
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
SlideShare
 

Viewers also liked (11)

Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
 
Class 2: Welcome part 2
Class 2: Welcome part 2Class 2: Welcome part 2
Class 2: Welcome part 2
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
Class 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibClass 8b: Numpy & Matplotlib
Class 8b: Numpy & Matplotlib
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programming
 
Class 8a: Modules and imports
Class 8a: Modules and importsClass 8a: Modules and imports
Class 8a: Modules and imports
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionaries
 
Class 7b: Files & File I/O
Class 7b: Files & File I/OClass 7b: Files & File I/O
Class 7b: Files & File I/O
 
Introduction to SlideShare for Businesses
Introduction to SlideShare for BusinessesIntroduction to SlideShare for Businesses
Introduction to SlideShare for Businesses
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Class 3: if/else

Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.
Python Meetup
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
Sunjid Hasan
 
Pythonintro
PythonintroPythonintro
Pythonintro
Hardik Malhotra
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
sagar414433
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
sagar414433
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming course
Alexander Galkin
 
Python introduction
Python introductionPython introduction
Python introduction
leela rani
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
PranavSB
 
Python language data types
Python language data typesPython language data types
Python language data types
James Wong
 
Python language data types
Python language data typesPython language data types
Python language data types
Harry Potter
 
Python language data types
Python language data typesPython language data types
Python language data types
Hoang Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data types
Young Alista
 
Python language data types
Python language data typesPython language data types
Python language data types
Luis Goldster
 
Python language data types
Python language data typesPython language data types
Python language data types
Tony Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data types
Fraboni Ec
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
Mohd Aves Malik
 
Python slide
Python slidePython slide
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 

Similar to Class 3: if/else (20)

Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming course
 
Python introduction
Python introductionPython introduction
Python introduction
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
Python slide
Python slidePython slide
Python slide
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 

Recently uploaded

Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdfTopic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
TinyAnderson
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
pablovgd
 
Randomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNERandomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNE
University of Maribor
 
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
Sérgio Sacani
 
BREEDING METHODS FOR DISEASE RESISTANCE.pptx
BREEDING METHODS FOR DISEASE RESISTANCE.pptxBREEDING METHODS FOR DISEASE RESISTANCE.pptx
BREEDING METHODS FOR DISEASE RESISTANCE.pptx
RASHMI M G
 
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
University of Maribor
 
ESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptxESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptx
PRIYANKA PATEL
 
Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.
Aditi Bajpai
 
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
Abdul Wali Khan University Mardan,kP,Pakistan
 
20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx
Sharon Liu
 
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
David Osipyan
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills MN
 
Shallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptxShallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptx
Gokturk Mehmet Dilci
 
Medical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptxMedical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptx
terusbelajar5
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
yqqaatn0
 
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero WaterSharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Texas Alliance of Groundwater Districts
 
The debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically youngThe debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically young
Sérgio Sacani
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
Sérgio Sacani
 
Bob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdfBob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdf
Texas Alliance of Groundwater Districts
 
Phenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvementPhenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvement
IshaGoswami9
 

Recently uploaded (20)

Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdfTopic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
 
Randomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNERandomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNE
 
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
 
BREEDING METHODS FOR DISEASE RESISTANCE.pptx
BREEDING METHODS FOR DISEASE RESISTANCE.pptxBREEDING METHODS FOR DISEASE RESISTANCE.pptx
BREEDING METHODS FOR DISEASE RESISTANCE.pptx
 
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
 
ESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptxESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptx
 
Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.Micronuclei test.M.sc.zoology.fisheries.
Micronuclei test.M.sc.zoology.fisheries.
 
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
 
20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx
 
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
 
Shallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptxShallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptx
 
Medical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptxMedical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptx
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
 
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero WaterSharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
 
The debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically youngThe debris of the ‘last major merger’ is dynamically young
The debris of the ‘last major merger’ is dynamically young
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
 
Bob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdfBob Reedy - Nitrate in Texas Groundwater.pdf
Bob Reedy - Nitrate in Texas Groundwater.pdf
 
Phenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvementPhenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvement
 

Class 3: if/else

  • 2. The plan! Basics: data types (and operations & calculations) ← recap Basics: conditionals & iteration Basics: lists, tuples, dictionaries Basics: writing functions Reading & writing files: opening, parsing & formats Working with numbers: numpy & scipy Making plots: matplotlib & pylab … if you guys want more after all of that … Writing better code: functions, pep8, classes Working with numbers: Numpy & Scipy (advanced) Other modules: Pandas & Scikit-learn Interactive notebooks: ipython & jupyter Advanced topics: virtual environments & version control
  • 3. recap: ints, floats, strings & booleans ---- file contents ---- from __future__ import division # the “+” operation works differently on strings and integers print “Python”, 1 + 3, “the win!” print “1” + “3”, “is an unlucky number.” # integers (and floats) are great for maths # we can use variables to store values two = 3 - 1 print “Python is”, two “much fun.” # strings are for text manipulation first_numbers = “1” + “2” + “3” print “The first few numbers are:”, first_numbers print “Lots of nonsense:” first_numbers * 123 ints & floats + addition - subtraction * multiplication / division ** exponent % modulus strings + concatenate * copy … no spaces … cannot begin with number: 1, 2 … … cannot contain ?$@# (except _) … cannot be a “built-in” name: and, or, int, float, str, char, exec, file, open, object, print, quit… (approx. 80 reserved names)
  • 4. recap: ints, floats, strings & booleans ---- file contents ---- from __future__ import division # “raw_input” always returns a string number = raw_input(“Gimme a number:”) print “Here is your number 3 times:” number * 3 # we can use functions to check and change the types print “number is a:” type(number) new_number = int(number) print “new_number is a:” type(new_number) print “Here is your number times 3 :” new_number * 3 # boolean values and equality testing number = 3 print “Does number equal 3?”, number == 3 print “Is number > 10? ” number ?”, 3 > 10 int() convert to an integer str() convert to a string float() convert to a float type() determine the type of a variable = = equal ! = not equal > greater than >= greater or equal than < less than <= less or equal than
  • 5. Some exercises... - Write a script that asks the user for their height (in meters), and prints their height in imperial: - version 1: prints the result using only feet (1m = 3.28084ft) - version 2: prints the result using feet and inches, to the nearest inch. (1ft = 12 inches)
  • 6. Making choices ---- file good_advice.py ---- from __future__ import division # “raw_input” always returns a string chairs = int(raw_input(“How many chairs do you have?:”)) lions = int(raw_input(“How many lions are there?:”)) # here comes an “if else” block if chairs >= lions: # everything “indented” is part of the “true” block print “You should be fine.” # so you can write as much code as you want print “Really, you’ll be fine!” else: print “Run away!!!” # structure of an “if else” block if boolean : the “True” code goes here else: the “False” code goes here
  • 7. Making choices: elif ---- file good_advice.py ---- from __future__ import division # “raw_input” always returns a string chairs = int(raw_input(“How many chairs do you have?:”)) lions = int(raw_input(“How many lions are there?:”)) # here comes an “if else” block. if chairs > lions: # everything “indented” is part of the “true” block print “You should be fine.” # so you can write as much code as you want print “Really, you’ll be fine!” elif chairs == lions: # “elif” is short for “else, if” print “You’re OK.” print “But should get some more chairs soon!” else: print “Run away!!!” # structure of an “if elif else” block # Only ONE block is executed! if boolean : the “True” code goes here elif boolean : next “True” code goes here else: the “False” code goes here
  • 8. Making complex choices: nesting ---- file good_advice.py ---- from __future__ import division # “raw_input” always returns a string chairs = int(raw_input(“How many chairs do you have?:”)) lions = int(raw_input(“How many lions are there?:”)) hungry = raw_input(“Are the lions hungry? (yes/no)?:”) # only worry about chairs if the lions are hungry if hungry == ‘yes’: # you can “nest” one “if” in another if chairs > lions: # notice the extra indentation? print “You’ll be fine!” else: print “Run away!” else: print “The lions are not hungry right now.” # structure of an “if else” block if boolean : if boolean : … code goes here else: … code goes here else: … code goes here
  • 9. Making complex choices: nesting ---- file good_advice.py ---- from __future__ import division # “raw_input” always returns a string chairs = int(raw_input(“How many chairs do you have?:”)) lions = int(raw_input(“How many lions are there?:”)) traps = int(raw_input(“How many traps do you have?:”)) bears = int(raw_input(“How many bears are there?:”)) # assume traps work against bears # and chairs against lions if (traps >= bears) and (chairs >= lions): print “You’ll be fine!” else: print “Run away!” # structure of an “if else” block if (boolean and/or boolean…) : the “True” code goes here elif (boolean and/or boolean…) : next “True” code goes here else: the “False” code goes here
  • 10. Making complex choices: nesting ---- file good_advice.py ---- from __future__ import division # “raw_input” always returns a string chairs = int(raw_input(“How many chairs do you have?:”)) lions = int(raw_input(“How many lions are there?:”)) traps = int(raw_input(“How many traps do you have?:”)) bears = int(raw_input(“How many bears are there?:”)) # assume traps work against bears # and chairs against lions if (traps < bears) or (chairs < lions): print “Run away!” else: print “You’ll be fine!” # structure of an “if else” block if (boolean and/or boolean…) : the “True” code goes here elif (boolean and/or boolean…) : next “True” code goes here else: the “False” code goes here
  • 11. Let’s write the following variants of the lion/chair/hungry program. They start as above, but: - If the ‘user’ does not write ‘yes’ or ‘no’ when answering ‘hungry?’, tells them that their choice is not valid, and nothing else. Exercise! ---- file good_advice.py ---- from __future__ import division # “raw_input” always returns a string chairs = int(raw_input(“How many chairs do you have?:”)) lions = int(raw_input(“How many lions are there?:”)) hungry = raw_input(“Are the lions hungry? (yes/no)?:”)
  • 12. Moar exercises - Write a script that asks the user 3 times to insert a number: First hours, next minutes, and last seconds. - The script should output the time in seconds only. - Bonus: Write a script that does the opposite (i.e asks for seconds, and writes hr/min/sec)