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 so many programming Languages.
Why Python ?
4. 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
6. 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!â)
7. 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
8. 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']]
9. 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
10. 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
11. 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
13. If else structure[conditional statement]
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
14. 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)
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("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
17. 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)