SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Internet-of-Things (IoT)
Summer Engineering Program 2018
University of Notre Dame
LAB COMPONENT
Setting Up Python
Jupyter Notebook:
https://jupyter.readthedocs.io/en/latest/install.ht
ml
IDLE: Integrated Development and Learning
Environment
Python 101
• Unlike C/C++ or Java, Python statements do not
end in a semicolon
• In Python, indentation is the way you indicate
the scope of a conditional, function, etc.
• Look, no braces!
• Python is interpretive, meaning you don’t have
to write programs
• You can just enter statements into the Python
environment and they’ll execute
Python 101
• As in every language, a variable is the name of a
memory location
• Python is weakly typed, i.e., you don’t declare
variables to be a specific type
• A variable has the type that corresponds to the value
you assign to it
• Variable names begin with a letter or an underscore
and can contain letters, numbers, and underscores
• Python has reserved words that you can’t use as
variable names
Python 101
At the >>> prompt, do the following:
x=5
type(x)
x=“this is text”
type(x)
x=5.0
type(x)
Python 101
• You’ve already seen the print statement
• You can also print numbers with formatting
– [flags][width][.precision]type
– print (”pi: {0:8.2f}”.format(3.141592))
• These are identical to Java or C format specifiers
Python 101
• All code should contain comments that describe
what it does
• In Python, lines beginning with a # sign are
comment lines
• You can also have comments on the same line as
a statement
# This entire line is a comment
x=5 # Set up loop counter
Python 101
• Arithmetic operators we will use:
– + - * / addition, subtraction/negation,
multiplication, division
– % modulus, a.k.a. remainder
– ** exponentiation
• Precedence: Order in which operations are
computed.
– * / % ** have a higher precedence than + -
1 + 3 * 4 is 13
– Parentheses can be used to force a certain order of
evaluation.
(1 + 3) * 4 is 16
Python 101
• When integers and reals are mixed, the result is a
real number.
–Example: 1 / 2.0 is 0.5
–The conversion occurs on a per-operator basis.
– 7 / 3 * 1.2 + 3 / 2
– 2 * 1.2 + 3 / 2
– 2.4 + 3 / 2
– 2.4 + 1
– 3.4
Python 101
• Use this at the top of your program: from math
import *
Python 101
• Many logical expressions use relational
operators:
Python 101
• These operators return true or false
Python 101
• Syntax:
if <condition>:
<statements>
x = 5
if x > 4:
print(“x is greater than 4”)
print(“This is not in the scope of the
if”)
Python 101
• The colon is required for the if
• Note that all statements indented by one level
below the if are within it scope:
x = 5
if x > 4:
print(“x is greater than 4”)
print(“This is also in the scope of
the if”)
Python 101
if <condition>:
<statements>
else:
<statements>
• Note the colon following the else
• This works exactly the way you would expect
Python 101
• Syntax for “for” statement:
for variableName in groupOfValues:
<statements>
• variableName gives a name to each value, so you can
refer to it in the statements
• groupOfValues can be a range of integers, specified
with the range function
• Example:
for x in range(1, 6):
print x, "squared is", x * x
Python 101
The range function specifies a range of integers:
range(start, stop)- the integers between start
(inclusive) and stop (exclusive)
It can also accept a third value specifying the
change between values:
range(start, stop, step)- the integers
between start (inclusive) and stop (exclusive) by
step
Python 101
• “While:” executes a group of statements as long as a
condition is True
• Good for indefinite loops (repeat an unknown number of
times)
• Syntax:
while <condition>:
<statements>
• Example:
number = 1
while number < 200:
print number,
number = number * 2
Exercise
• Write a Python program to compute and display
the first 16 powers of 2, starting with 1
Strings
• String: A sequence of text characters in a
program
• Strings start and end with quotation mark " or
apostrophe ' characters
• Examples:
"hello"
"This is a string"
‘This, too, is a string. It can be very long!’
Strings
• A string can represent characters by preceding
them with a backslash
– t tab character
– n new line character
– " quotation mark character
–  backslash character
• Example: "HellottherenHow are you?"
Strings
• As with other languages, you can use square
brackets to index a string as if it were an array:
name = “Arpita Nigam”
print(name, “starts with “, name[0])
Strings
• len(string) - number of characters in a
string
• str.lower(string) - lowercase version of a string
• str.upper(string) - uppercase version of a string
• str.isalpha(string) - True if the string has
only alpha chars
• Many others: split, replace, find, format, etc.
• Note the “dot” notation: These are static methods
Byte Arrays and Strings
• Strings are Unicode text and not mutable
• Byte arrays are mutable and contain raw bytes
• For example, reading Internet data from a URL
gets bytes
• Convert to string:
cmd = response.read()
strCmd = str(cmd)
Other Built-In Types
• Tuples, lists, sets, and dictionaries
• They all allow you to group more than one item
of data together under one name
• You can also search them
Tuples
• Unchanging sequences of data
• Enclosed in parentheses:
tuple1 = (“This”, “is”, “a”, “tuple”)
print(tuple1)
• This prints the tuple exactly as shown
print(tuple1[1])
• Prints “is” (without the quotes)
Lists
• Changeable sequences of data
• Lists are created by using square brackets:
breakfast = [ “coffee”, “tea”, “toast”, “egg”
]
• You can add to a list:
breakfast.append(“waffles”)
breakfast.extend([“cereal”, “juice”])
Dictionaries
• Groupings of data indexed by name
• Dictionaries are created using braces
sales = {}
sales[“January”] = 10000
sales[“February”] = 17000
sales[“March”] = 16500
food = {"ham" : "yes", "egg" : "yes", "spam" :
"no"}
food
food[“ham”]
food[“egg”] = ‘no’
Sets
• Sets are similar to dictionaries in Python, except
that they consist of only keys with no associated
values
• Essentially, they are a collection of data with no
duplicates
• They are very useful when it comes to removing
duplicate data from data collections.
Writing Functions
• Define a function:
def <function name>(<parameter list>)
• The function body is indented one level:
def computeSquare(x)
return x * x
# Anything at this level is not part of the
function
Error Handling
• Use try/except blocks, similar to try/catch:
fridge_contents = {“egg”:8, “mushroom”:20,
“pepper”:3, “cheese”:2, “tomato”:4,
“milk”:13}
try:
if fridge_contents[“orange juice”] > 3:
print(“Let’s have some juice!”)
except KeyError:
print(“Awww, there is no orange juice.”)
Error Handling
• Note that you must specify the type of error
• Looking for a key in a dictionary that doesn’t
exist is an error
• Another example:
try:
sock = BluetoothSocket(RFCOMM)
sock.connect((bd_addr, port))
except BluetoothError as bt
Print(“Cannot connect to host: “ +
str(bt))
File I/O
• You can read and write text files in Python much
as you can in other languages, and with a similar
syntax
• To open a file for reading:
try:
configFile = open(configName, "r")
except IOError as err:
print(“could not open file: “ + str(err))
File I/O
• To read from a file:
while 1:
line = configFile.readline()
if len(line) == 0:
break
File I/O
• You can also read all lines from a file into a set,
then iterate over the set:
lines = file.readlines()
for line in lines:
print(line)
file.close()
File I/O
• Writing to a text file
file=open(‘test.txt’,”w”)
file.write(“This is how you create a new text
file”)
file.close()
with open('/etc/passwd') as f:
for line in f:
print(line)
Assignment 1: Task 1
stock = { "banana": 6,
"apple": 0,
"orange": 32,
"pear": 15 }
prices = { "banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3 }
Write a function “Rechnung” (bill) that takes a list of foods (e.g.,
["banana", "orange", "apple"]) as input and computes the total
price (only for items in stock) and adjusts the stock accordingly.
Write the bill computation as function that takes one parameter
(list of foods).
Assignment 1: Task 2
Continue Task 1 by writing the shopping list into a
newly created file, each item in a new line. Then
write a second program that reads the file in a
loop, line by line and prints each line.
Feierabend

Weitere ähnliche Inhalte

Ähnlich wie IoT-Week1-Day1-Lab.pptx

Introductio_to_python_progamming_ppt.ppt
Introductio_to_python_progamming_ppt.pptIntroductio_to_python_progamming_ppt.ppt
Introductio_to_python_progamming_ppt.pptHiralPatel798996
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxNimrahafzal1
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptxssuser92d141
 
modul-python-part1.pptx
modul-python-part1.pptxmodul-python-part1.pptx
modul-python-part1.pptxYusuf Ayuba
 
Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.supriyasarkar38
 
Basics of Python Programming
Basics of Python ProgrammingBasics of Python Programming
Basics of Python ProgrammingManishJha237
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way PresentationAmira ElSharkawy
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptxsangeeta borde
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : PythonRaghu Kumar
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 
Programming in Python
Programming in Python Programming in Python
Programming in Python Tiji Thomas
 

Ähnlich wie IoT-Week1-Day1-Lab.pptx (20)

coolstuff.ppt
coolstuff.pptcoolstuff.ppt
coolstuff.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Introductio_to_python_progamming_ppt.ppt
Introductio_to_python_progamming_ppt.pptIntroductio_to_python_progamming_ppt.ppt
Introductio_to_python_progamming_ppt.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python ppt
Python pptPython ppt
Python ppt
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptx
 
modul-python-part1.pptx
modul-python-part1.pptxmodul-python-part1.pptx
modul-python-part1.pptx
 
Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.
 
ENGLISH PYTHON.ppt
ENGLISH PYTHON.pptENGLISH PYTHON.ppt
ENGLISH PYTHON.ppt
 
Basics of Python Programming
Basics of Python ProgrammingBasics of Python Programming
Basics of Python Programming
 
Kavitha_python.ppt
Kavitha_python.pptKavitha_python.ppt
Kavitha_python.ppt
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
 

Mehr von afsheenfaiq2

Anime Display for Weekly Passion Hour Club
Anime Display for Weekly Passion Hour ClubAnime Display for Weekly Passion Hour Club
Anime Display for Weekly Passion Hour Clubafsheenfaiq2
 
Creating Python Variables using Replit software
Creating Python Variables using Replit softwareCreating Python Variables using Replit software
Creating Python Variables using Replit softwareafsheenfaiq2
 
Introduction to Declaring Functions in Python
Introduction to Declaring Functions in PythonIntroduction to Declaring Functions in Python
Introduction to Declaring Functions in Pythonafsheenfaiq2
 
Sample Exam Questions on Python for revision
Sample Exam Questions on Python for revisionSample Exam Questions on Python for revision
Sample Exam Questions on Python for revisionafsheenfaiq2
 
GR 12 IOT Week 2.pptx
GR 12 IOT Week 2.pptxGR 12 IOT Week 2.pptx
GR 12 IOT Week 2.pptxafsheenfaiq2
 
Lesson 17 - Pen Shade and Stamp.pptx
Lesson 17 - Pen Shade and Stamp.pptxLesson 17 - Pen Shade and Stamp.pptx
Lesson 17 - Pen Shade and Stamp.pptxafsheenfaiq2
 
AP CS PD 1.3 Week 4.pptx
AP CS PD 1.3 Week 4.pptxAP CS PD 1.3 Week 4.pptx
AP CS PD 1.3 Week 4.pptxafsheenfaiq2
 
2D Polygons using Pen tools- Week 21.pptx
2D Polygons using Pen tools- Week 21.pptx2D Polygons using Pen tools- Week 21.pptx
2D Polygons using Pen tools- Week 21.pptxafsheenfaiq2
 
Chapter 11 Strings.pptx
Chapter 11 Strings.pptxChapter 11 Strings.pptx
Chapter 11 Strings.pptxafsheenfaiq2
 
Lesson 10_Size Block.pptx
Lesson 10_Size Block.pptxLesson 10_Size Block.pptx
Lesson 10_Size Block.pptxafsheenfaiq2
 
Gr 12 - Buzzer Project on Sound Production (W10).pptx
Gr 12 - Buzzer Project on Sound Production (W10).pptxGr 12 - Buzzer Project on Sound Production (W10).pptx
Gr 12 - Buzzer Project on Sound Production (W10).pptxafsheenfaiq2
 
Network Topologies
Network TopologiesNetwork Topologies
Network Topologiesafsheenfaiq2
 
IoT-Week1-Day1-Lecture.pptx
IoT-Week1-Day1-Lecture.pptxIoT-Week1-Day1-Lecture.pptx
IoT-Week1-Day1-Lecture.pptxafsheenfaiq2
 

Mehr von afsheenfaiq2 (17)

Anime Display for Weekly Passion Hour Club
Anime Display for Weekly Passion Hour ClubAnime Display for Weekly Passion Hour Club
Anime Display for Weekly Passion Hour Club
 
Creating Python Variables using Replit software
Creating Python Variables using Replit softwareCreating Python Variables using Replit software
Creating Python Variables using Replit software
 
Introduction to Declaring Functions in Python
Introduction to Declaring Functions in PythonIntroduction to Declaring Functions in Python
Introduction to Declaring Functions in Python
 
Sample Exam Questions on Python for revision
Sample Exam Questions on Python for revisionSample Exam Questions on Python for revision
Sample Exam Questions on Python for revision
 
GR 12 IOT Week 2.pptx
GR 12 IOT Week 2.pptxGR 12 IOT Week 2.pptx
GR 12 IOT Week 2.pptx
 
IOT Week 20.pptx
IOT Week 20.pptxIOT Week 20.pptx
IOT Week 20.pptx
 
Lesson 17 - Pen Shade and Stamp.pptx
Lesson 17 - Pen Shade and Stamp.pptxLesson 17 - Pen Shade and Stamp.pptx
Lesson 17 - Pen Shade and Stamp.pptx
 
AP CS PD 1.3 Week 4.pptx
AP CS PD 1.3 Week 4.pptxAP CS PD 1.3 Week 4.pptx
AP CS PD 1.3 Week 4.pptx
 
2D Polygons using Pen tools- Week 21.pptx
2D Polygons using Pen tools- Week 21.pptx2D Polygons using Pen tools- Week 21.pptx
2D Polygons using Pen tools- Week 21.pptx
 
Chapter 11 Strings.pptx
Chapter 11 Strings.pptxChapter 11 Strings.pptx
Chapter 11 Strings.pptx
 
Lesson 10_Size Block.pptx
Lesson 10_Size Block.pptxLesson 10_Size Block.pptx
Lesson 10_Size Block.pptx
 
CH05.ppt
CH05.pptCH05.ppt
CH05.ppt
 
Chapter05.ppt
Chapter05.pptChapter05.ppt
Chapter05.ppt
 
Gr 12 - Buzzer Project on Sound Production (W10).pptx
Gr 12 - Buzzer Project on Sound Production (W10).pptxGr 12 - Buzzer Project on Sound Production (W10).pptx
Gr 12 - Buzzer Project on Sound Production (W10).pptx
 
Network Topologies
Network TopologiesNetwork Topologies
Network Topologies
 
IoT-Week1-Day1-Lecture.pptx
IoT-Week1-Day1-Lecture.pptxIoT-Week1-Day1-Lecture.pptx
IoT-Week1-Day1-Lecture.pptx
 
Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
 

Kürzlich hochgeladen

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

IoT-Week1-Day1-Lab.pptx

  • 1. Internet-of-Things (IoT) Summer Engineering Program 2018 University of Notre Dame LAB COMPONENT
  • 2. Setting Up Python Jupyter Notebook: https://jupyter.readthedocs.io/en/latest/install.ht ml IDLE: Integrated Development and Learning Environment
  • 3. Python 101 • Unlike C/C++ or Java, Python statements do not end in a semicolon • In Python, indentation is the way you indicate the scope of a conditional, function, etc. • Look, no braces! • Python is interpretive, meaning you don’t have to write programs • You can just enter statements into the Python environment and they’ll execute
  • 4. Python 101 • As in every language, a variable is the name of a memory location • Python is weakly typed, i.e., you don’t declare variables to be a specific type • A variable has the type that corresponds to the value you assign to it • Variable names begin with a letter or an underscore and can contain letters, numbers, and underscores • Python has reserved words that you can’t use as variable names
  • 5. Python 101 At the >>> prompt, do the following: x=5 type(x) x=“this is text” type(x) x=5.0 type(x)
  • 6. Python 101 • You’ve already seen the print statement • You can also print numbers with formatting – [flags][width][.precision]type – print (”pi: {0:8.2f}”.format(3.141592)) • These are identical to Java or C format specifiers
  • 7. Python 101 • All code should contain comments that describe what it does • In Python, lines beginning with a # sign are comment lines • You can also have comments on the same line as a statement # This entire line is a comment x=5 # Set up loop counter
  • 8. Python 101 • Arithmetic operators we will use: – + - * / addition, subtraction/negation, multiplication, division – % modulus, a.k.a. remainder – ** exponentiation • Precedence: Order in which operations are computed. – * / % ** have a higher precedence than + - 1 + 3 * 4 is 13 – Parentheses can be used to force a certain order of evaluation. (1 + 3) * 4 is 16
  • 9. Python 101 • When integers and reals are mixed, the result is a real number. –Example: 1 / 2.0 is 0.5 –The conversion occurs on a per-operator basis. – 7 / 3 * 1.2 + 3 / 2 – 2 * 1.2 + 3 / 2 – 2.4 + 3 / 2 – 2.4 + 1 – 3.4
  • 10. Python 101 • Use this at the top of your program: from math import *
  • 11. Python 101 • Many logical expressions use relational operators:
  • 12. Python 101 • These operators return true or false
  • 13. Python 101 • Syntax: if <condition>: <statements> x = 5 if x > 4: print(“x is greater than 4”) print(“This is not in the scope of the if”)
  • 14. Python 101 • The colon is required for the if • Note that all statements indented by one level below the if are within it scope: x = 5 if x > 4: print(“x is greater than 4”) print(“This is also in the scope of the if”)
  • 15. Python 101 if <condition>: <statements> else: <statements> • Note the colon following the else • This works exactly the way you would expect
  • 16. Python 101 • Syntax for “for” statement: for variableName in groupOfValues: <statements> • variableName gives a name to each value, so you can refer to it in the statements • groupOfValues can be a range of integers, specified with the range function • Example: for x in range(1, 6): print x, "squared is", x * x
  • 17. Python 101 The range function specifies a range of integers: range(start, stop)- the integers between start (inclusive) and stop (exclusive) It can also accept a third value specifying the change between values: range(start, stop, step)- the integers between start (inclusive) and stop (exclusive) by step
  • 18. Python 101 • “While:” executes a group of statements as long as a condition is True • Good for indefinite loops (repeat an unknown number of times) • Syntax: while <condition>: <statements> • Example: number = 1 while number < 200: print number, number = number * 2
  • 19. Exercise • Write a Python program to compute and display the first 16 powers of 2, starting with 1
  • 20. Strings • String: A sequence of text characters in a program • Strings start and end with quotation mark " or apostrophe ' characters • Examples: "hello" "This is a string" ‘This, too, is a string. It can be very long!’
  • 21. Strings • A string can represent characters by preceding them with a backslash – t tab character – n new line character – " quotation mark character – backslash character • Example: "HellottherenHow are you?"
  • 22. Strings • As with other languages, you can use square brackets to index a string as if it were an array: name = “Arpita Nigam” print(name, “starts with “, name[0])
  • 23. Strings • len(string) - number of characters in a string • str.lower(string) - lowercase version of a string • str.upper(string) - uppercase version of a string • str.isalpha(string) - True if the string has only alpha chars • Many others: split, replace, find, format, etc. • Note the “dot” notation: These are static methods
  • 24. Byte Arrays and Strings • Strings are Unicode text and not mutable • Byte arrays are mutable and contain raw bytes • For example, reading Internet data from a URL gets bytes • Convert to string: cmd = response.read() strCmd = str(cmd)
  • 25. Other Built-In Types • Tuples, lists, sets, and dictionaries • They all allow you to group more than one item of data together under one name • You can also search them
  • 26. Tuples • Unchanging sequences of data • Enclosed in parentheses: tuple1 = (“This”, “is”, “a”, “tuple”) print(tuple1) • This prints the tuple exactly as shown print(tuple1[1]) • Prints “is” (without the quotes)
  • 27. Lists • Changeable sequences of data • Lists are created by using square brackets: breakfast = [ “coffee”, “tea”, “toast”, “egg” ] • You can add to a list: breakfast.append(“waffles”) breakfast.extend([“cereal”, “juice”])
  • 28. Dictionaries • Groupings of data indexed by name • Dictionaries are created using braces sales = {} sales[“January”] = 10000 sales[“February”] = 17000 sales[“March”] = 16500 food = {"ham" : "yes", "egg" : "yes", "spam" : "no"} food food[“ham”] food[“egg”] = ‘no’
  • 29. Sets • Sets are similar to dictionaries in Python, except that they consist of only keys with no associated values • Essentially, they are a collection of data with no duplicates • They are very useful when it comes to removing duplicate data from data collections.
  • 30. Writing Functions • Define a function: def <function name>(<parameter list>) • The function body is indented one level: def computeSquare(x) return x * x # Anything at this level is not part of the function
  • 31. Error Handling • Use try/except blocks, similar to try/catch: fridge_contents = {“egg”:8, “mushroom”:20, “pepper”:3, “cheese”:2, “tomato”:4, “milk”:13} try: if fridge_contents[“orange juice”] > 3: print(“Let’s have some juice!”) except KeyError: print(“Awww, there is no orange juice.”)
  • 32. Error Handling • Note that you must specify the type of error • Looking for a key in a dictionary that doesn’t exist is an error • Another example: try: sock = BluetoothSocket(RFCOMM) sock.connect((bd_addr, port)) except BluetoothError as bt Print(“Cannot connect to host: “ + str(bt))
  • 33. File I/O • You can read and write text files in Python much as you can in other languages, and with a similar syntax • To open a file for reading: try: configFile = open(configName, "r") except IOError as err: print(“could not open file: “ + str(err))
  • 34. File I/O • To read from a file: while 1: line = configFile.readline() if len(line) == 0: break
  • 35. File I/O • You can also read all lines from a file into a set, then iterate over the set: lines = file.readlines() for line in lines: print(line) file.close()
  • 36. File I/O • Writing to a text file file=open(‘test.txt’,”w”) file.write(“This is how you create a new text file”) file.close() with open('/etc/passwd') as f: for line in f: print(line)
  • 37. Assignment 1: Task 1 stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } Write a function “Rechnung” (bill) that takes a list of foods (e.g., ["banana", "orange", "apple"]) as input and computes the total price (only for items in stock) and adjusts the stock accordingly. Write the bill computation as function that takes one parameter (list of foods).
  • 38. Assignment 1: Task 2 Continue Task 1 by writing the shopping list into a newly created file, each item in a new line. Then write a second program that reads the file in a loop, line by line and prints each line.