Python Programming
By: Swati Choudhary
Programming : Python
• What is Python ?
A dynamic, open source programming language with a focus on
simplicity and productivity.
It has an elegant syntax that is natural to read and easy to write.
There are so many programming Languages.
Why Python ?
Why Python?
 Python is Simple & Beautiful
 Python is Object-Oriented
• Structure supports concepts as Polymorphism, Operation overloading & Multiple Inheritance
 Python is Free (Open Source Language )
• Downloading & Installing Python is Free & Easy
• Source Code is easily Accessible
 Python is Portable & Powerful
• Runs virtually on every major Platforms used today
• Dynamic Typing, Built-in types & tools, Library Utilities, Automatic Memory Management
Why Python?
Python Versions
Python 2.x is legacy; Python 3.x is the present and future of the language.
The most visible difference is probably the way the “print” statement works.
In python2 it’s a statement
print “Hello World!”
In Python3 it’s a function
print(“Hello World!”)
Python Strings:
Strings in Python are identified as a contiguous set of characters in between
quotation marks.
str = 'Hello World!'
Print(str) # Prints complete string
print(str[0]) # Prints first character of the string
Print(str[2:5]) # Prints characters starting from 3rd to 6th
print(str[2:]) # Prints string starting from 3rd character
print(str * 2) # Prints string two times
print(str + "TEST“) # Prints concatenated string
Python Lists:
Lists are the most versatile of Python's compound data types. A list
contains items separated by commas and enclosed within square
brackets ([]).
list1 = [ ‘hello', 786 , 4.1, ‘Ram', 702 ]
list2 = [123, ‘Victor']
Nested List
my_list = ["mouse", [8, 4, 6], ['a']]
Python List Methods
Methods that are available with list object in Python programming are tabulated below.
They are accessed as list.method()
append() - Add an element to the end of the list
extend() - Add all elements of a list to another list
insert() - Insert an item at the defined index
remove() - Removes an item from the list
pop() - Removes and returns an element at the given index
clear() - Removes all items from the list
index() - Returns the index of the first matched item
count() - Returns the count of number of items passed as an argument
sort() - Sort items in a list in ascending order
reverse() - Reverse the order of items in the list
copy() - Returns a shallow copy of the list
Python Tuples:
A tuple is another sequence data type that is similar to the list. Unlike
lists, however, tuples are enclosed within parentheses.
tuple1 = ( ‘hello', 786 , 2.23, ‘victor', 70.2 )
tuple2 = (123, ‘jay')
print(tuple1) # Prints complete list
print(tuple1[0]) # Prints first element of the list
print(tuple1[1:3]) # Prints elements starting from 2nd to 4th
print(tuple1[2:]) # Prints elements starting from 3rd element
print(tuple2 * 2) # Prints list two times
print(tuple1 + tuple2) # Prints concatenated lists
Python Dictionary:
Python 's dictionaries consist of key-value pairs.
dict1 = {'name': 'john','code':6734, 'dept': 'sales'}
print(dict1['name’]) # Prints value for 'one' key
print(dict1['code’]) # Prints value for 2 key
print(dict1) # Prints complete dictionary
print(dict1.keys()) # Prints all the keys
Print(dict1.values()) # Prints all the values
Python Operators
• Arithmetic operators (+, _, *, /, %, //, **)
• Comparison operators (>, <, ==, !=, >=, <=)
• Logical operators (And, OR, NOT)
• Bitwise operators (|OR, ^ XOR, ~ COMP)
• Assignment operators (=, +=, -=, *=, /=, //=, %=)
• Special operators
• Identity operators (is, is not)
• Membership operators (in, not in)
If else structure[conditional statement]
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
For loop using Range
# Prints out the numbers 0,1,2,3,4
for x in range(5):
print(x)
# Prints out 3,4,5
for x in range(3, 6):
print(x)
# Prints out 3,5,7
for x in range(3, 8, 2):
print(x)
While loop
while expression:
statements(s)
"break" and "continue" statements
break is used to exit a for loop or a while loop, whereas continue is used to skip the current
block, and return to the "for" or "while" statement
Defining Functions
def calculate(a,b):
print("Sum is=",a+b)
print("Difference is=",a-b)
print("Product is=",a*b)
calculate(20,10)
Functions are nothing but the pieces of code which are define for it
re-usability any time we call it
IoT example
# Program to load data into cloud
import time # To use sleep function
import requests
import random
baseurl="https://api.thingspeak.com/update?api_key="
apikey=“XXXXXXXXXXXX" #WriteAPI key
while True:
temp=random.randrange(15,50)
hum=random.randrange(30,70)
x=requests.get(baseurl+apikey+"&field1=%d&field2=%d"%(temp,hum))
time.sleep(1)
print(x)

Python_IoT.pptx

  • 1.
  • 2.
    Programming : Python •What is Python ? A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
  • 3.
    There are somany programming Languages. Why Python ?
  • 4.
    Why Python?  Pythonis Simple & Beautiful  Python is Object-Oriented • Structure supports concepts as Polymorphism, Operation overloading & Multiple Inheritance  Python is Free (Open Source Language ) • Downloading & Installing Python is Free & Easy • Source Code is easily Accessible  Python is Portable & Powerful • Runs virtually on every major Platforms used today • Dynamic Typing, Built-in types & tools, Library Utilities, Automatic Memory Management
  • 5.
  • 6.
    Python Versions Python 2.xis legacy; Python 3.x is the present and future of the language. The most visible difference is probably the way the “print” statement works. In python2 it’s a statement print “Hello World!” In Python3 it’s a function print(“Hello World!”)
  • 7.
    Python Strings: Strings inPython are identified as a contiguous set of characters in between quotation marks. str = 'Hello World!' Print(str) # Prints complete string print(str[0]) # Prints first character of the string Print(str[2:5]) # Prints characters starting from 3rd to 6th print(str[2:]) # Prints string starting from 3rd character print(str * 2) # Prints string two times print(str + "TEST“) # Prints concatenated string
  • 8.
    Python Lists: Lists arethe most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). list1 = [ ‘hello', 786 , 4.1, ‘Ram', 702 ] list2 = [123, ‘Victor'] Nested List my_list = ["mouse", [8, 4, 6], ['a']]
  • 9.
    Python List Methods Methodsthat are available with list object in Python programming are tabulated below. They are accessed as list.method() append() - Add an element to the end of the list extend() - Add all elements of a list to another list insert() - Insert an item at the defined index remove() - Removes an item from the list pop() - Removes and returns an element at the given index clear() - Removes all items from the list index() - Returns the index of the first matched item count() - Returns the count of number of items passed as an argument sort() - Sort items in a list in ascending order reverse() - Reverse the order of items in the list copy() - Returns a shallow copy of the list
  • 10.
    Python Tuples: A tupleis another sequence data type that is similar to the list. Unlike lists, however, tuples are enclosed within parentheses. tuple1 = ( ‘hello', 786 , 2.23, ‘victor', 70.2 ) tuple2 = (123, ‘jay') print(tuple1) # Prints complete list print(tuple1[0]) # Prints first element of the list print(tuple1[1:3]) # Prints elements starting from 2nd to 4th print(tuple1[2:]) # Prints elements starting from 3rd element print(tuple2 * 2) # Prints list two times print(tuple1 + tuple2) # Prints concatenated lists
  • 11.
    Python Dictionary: Python 'sdictionaries consist of key-value pairs. dict1 = {'name': 'john','code':6734, 'dept': 'sales'} print(dict1['name’]) # Prints value for 'one' key print(dict1['code’]) # Prints value for 2 key print(dict1) # Prints complete dictionary print(dict1.keys()) # Prints all the keys Print(dict1.values()) # Prints all the values
  • 12.
    Python Operators • Arithmeticoperators (+, _, *, /, %, //, **) • Comparison operators (>, <, ==, !=, >=, <=) • Logical operators (And, OR, NOT) • Bitwise operators (|OR, ^ XOR, ~ COMP) • Assignment operators (=, +=, -=, *=, /=, //=, %=) • Special operators • Identity operators (is, is not) • Membership operators (in, not in)
  • 13.
    If else structure[conditionalstatement] if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s)
  • 14.
    For loop usingRange # Prints out the numbers 0,1,2,3,4 for x in range(5): print(x) # Prints out 3,4,5 for x in range(3, 6): print(x) # Prints out 3,5,7 for x in range(3, 8, 2): print(x)
  • 15.
    While loop while expression: statements(s) "break"and "continue" statements break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement
  • 16.
    Defining Functions def calculate(a,b): print("Sumis=",a+b) print("Difference is=",a-b) print("Product is=",a*b) calculate(20,10) Functions are nothing but the pieces of code which are define for it re-usability any time we call it
  • 17.
    IoT example # Programto load data into cloud import time # To use sleep function import requests import random baseurl="https://api.thingspeak.com/update?api_key=" apikey=“XXXXXXXXXXXX" #WriteAPI key while True: temp=random.randrange(15,50) hum=random.randrange(30,70) x=requests.get(baseurl+apikey+"&field1=%d&field2=%d"%(temp,hum)) time.sleep(1) print(x)