Internet-of-Things (IoT)

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
JavaScript, Third Edition
String Escape Sequences
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
1 Introduction to Programming with Python. 2 Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Input, Output, and Processing
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to Programming with Python
1 Introduction to Programming with Python: overview.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
28 Formatted Output.
String and Lists Dr. José M. Reyes Álamo.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Information and Computer Sciences University of Hawaii, Manoa
© by Pearson Education, Inc. All Rights Reserved.
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Python
Chapter 2 - Introduction to C Programming
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Chapter 2 - Introduction to C Programming
Java Programming: From Problem Analysis to Program Design, 4e
Arrays, For loop While loop Do while loop
Arithmetic operations, decisions and looping
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 2: Basic Elements of Java
Chapter 2 - Introduction to C Programming
An Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Chapter 2 - Introduction to C Programming
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
elementary programming
Chapter 2: Introduction to C++.
Topics Introduction to Functions Defining and Calling a Function
Chapter 2 Programming Basics.
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Python Basics with Jupyter Notebook
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
Introduction to Programming with Python
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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.html 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” x=5.0

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: 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: "Hello\tthere\nHow 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