Agenda = '''
Quick Python Introduction,
Anaconda Python3,
Data Types
int, float , str, bool , complex
Data Structure
Mutable : List, Dictionary, ( Set )
Immutable : Tuple, String, ( Number )
Control Structure
if elif else
while
for
User Define Functions
function with parameter
global Variable
fibbonacci Sequence
factorial of number
Variable Argument *arg, **kwarg
Multiplier
Python Built in Functions
lambda
filter
map
max, min, sum
set
sorted
reversed
zip
List Comprehension,
Dictionary Comprehension
Dictionary Case Study
Summary
'''
type(True)
True + True + True - False
Str1 = 'surendra panpaliya'
Str1[::-1]
Str1[0:len(Str1):2]
list(range(0,10,2))
1) int #Classes
2) float
3) str
4) bool
5) complex
2**1000
#dir('hello')
#type('Hello')
#help(str)
print(Str1.upper())
print(Str1)
co_nu = 5 + 100j
type(co_nu)
num = complex(5,10)
print(num)
print(num.real)
print(num.imag)
print(num.conjugate())
Mutable DS : list, dictionary, set
Immutable DS : tuple, string, numbers
List1 = []
type(List1)
List2 = list()
List2
Books = []
Books.append('Python')
Books.append('Jython')
Books.append('Java')
Books.append('Cython')
Books.append('J2ee')
Books.append('Spring')
Books.append('Ruby')
print(Books)
Books.pop()
print(Books)
Books = []
Books.append('Python')
Books.append('Jython')
Books.append('Java')
Books.append('Cython')
Books.append('J2ee')
Books.append('Spring')
Books.append('Ruby')
print(Books)
Books.pop(0)
print(Books)
Books = []
Books.append('Python')
Books.append('Jython')
Books.append('Java')
Books.append('Cython')
Books.append('J2ee')
Books.append('Spring')
Books.append('Ruby')
print(Books)
Books.insert(2,'Ruby on Rails')
print(Books)
Books.remove('Jython')
print(Books)
Books = []
Books.append('Python')
Books.append('Jython')
Books.append('Java')
Books.append('Cython')
Books.append('J2ee')
Books.append('Spring')
Books.append('Ruby')
print(Books[2])
Books[2]='Perl'
print(Books)
Books = []
Books.append('Python')
Books.append('Jython')
Books.append('Java')
Books.append('Cython')
Books.append('J2ee')
Books.append('Spring')
Books.append('Ruby')
print(Books)
Book1 = ['Mongodb','Django','DataScience']
Books.extend(Book1)
print(Books)
# Sort List
Books = []
Books.append('Python')
Books.append('Jython')
Books.append('Java')
Books.append('Cython')
Books.append('J2ee')
Books.append('Spring')
Books.append('Ruby')
print(Books)
Book1 = ['Mongodb','Django','DataScience']
Books.extend(Book1)
print(Books)
Books.sort()
# Index Position of Django
print(Books.index('Django',1,7))
print(Books)
Tuple is an immutable sequence of any objects like int,float,string, complex, list, tuple , many more.
Book1 = ('Mongodb','Django','DataScience')
type(Book1)
Book1[0] = 'Java'
# Tuples are immutable sequence
dir(Book1)
Tup1 = 'hello', 23, 55, 5.0
print(Tup1)
type(Tup1)
Tup2 = (5)
print(type(Tup2))
Tup3 = (5,)
print(type(Tup3))
print(Tup3)
first = 45
second = 56.9
temp = first
first = second
second = temp
print(first)
print(second)
first, second = 45, 56.9 # parallel Assignment
first, second = second, first
print(first, second)
first, second, third, fourth = 45, 56.9, 'hello',True
# parallel Assignment
first, second, third, fourth = fourth, third, second,first
print(first, second, third, fourth)
import time
TL = time.localtime()
print(TL)
type(TL)
year, mon, mday, hour, minute, sec, wday, \
yday, isdst = time.localtime()
print(year, mon, mday, hour, minute, sec, wday, \
yday, isdst)
Tuple_Time = (year, mon, mday, hour, minute, sec, wday, \
yday, isdst)
Tuple_Time[0:3]
print(mday, mon, year)
Routers = {}
type(Routers)
Routers = {}
Routers['cisco26'] = 'cisco2600'
Routers['cisco72'] = 'cisco7200'
Routers['cisco32'] = 'cisco3200'
Routers['ip'] = '10.10.10.1'
Routers['subnet'] = '255.255.255.0'
print(Routers)
Routers.keys()
Routers.values()
Routers['ip'] = '192.168.2.1'
print(Routers)
Routers.get('ip')
Routers.pop('cisco26')
print(Routers)
Switch1 = {'cisco82': 'cisco8200',
'cisco82': 'cisco8200',
'ip': '192.168.2.6',
'subnet': '255.255.255.7'}
Routers.update(Switch1)
print(Routers)
Routers.keys()
Routers.items()
for key, value in Routers.items():
print(key, value)
data = 34
print(data)
data = 34
print(data)
first = int(input("Enter first value:"))
print(first)
print(type(first))
%%writefile greatest_three.py
first = int(input("Enter first value:"))
second = int(input("Enter second value:"))
third = int(input("Enter third value:"))
if first > second and first > third:
print('first is greatest',first)
elif second > third:
print('second is greatest',second)
else:
print('third is greatest',third)
run greatest_three.py
%%writefile greatest_three1.py
first = int(input("Enter first value:"))
second = int(input("Enter second value:"))
third = int(input("Enter third value:"))
if first > second and first > third:
print('first is greatest',first)
elif second > third:
print('second is greatest',second)
elif first == second:
print("First is equal to Second")
else:
print('third is greatest',third)
run greatest_three1.py
%%writefile greatest_three_while.py
while True:
first = int(input("Enter first value:"))
second = int(input("Enter second value:"))
third = int(input("Enter third value:"))
if first > second and first > third:
print('first is greatest',first)
elif second > third:
print('second is greatest',second)
elif first == second:
print("First is equal to Second")
else:
print('third is greatest',third)
break
run greatest_three_while.py
list(range(0,20,2))
list(range(20,0,-2))
Books
for item in Books:
print(item)
for item in Books:
print(Books.index(item),item)
for item in range(0,len(Books)):
print(item, Books[item])
for index, value in enumerate(Books,1):
print(index, value)
Switch1
Routers
for item in Routers:
print(item,Routers[item])
def function_name(arg1,arg2):
'''Document of function'''
print("Welcome to Python Function")
print("Argument1 %s and Argument2 %s"%(arg1,arg2))
function_name('Surendra','Panpaliya')
%%writefile func_ex.py
def function_name(arg1=None,arg2=None):
'''Document of function'''
print("Welcome to Python Function")
print("Argument1 %s and Argument2 %s"%(arg1,arg2))
function_name()
function_name('Surendra','Panpaliya')
run func_ex.py
Create Fibbonanci Sequence using Function
0, 1, 1, 2 , 3, 5, 8,
def fibbonacci(max_num):
'''Fibbonacci Sequence up to max_num '''
first, second = 0, 1
print(first)
while second < max_num:
print(second)
first, second = second, first + second
fibbonacci(50)
def fibbonacci(max_num):
'''Fibbonacci Sequence up to max_num '''
first, second = 0, 1
print(first)
for num in range(max_num):
print(second)
first, second = second, first + second
fibbonacci(10)
#Assignment2
def factorial(num):
'''Factorial of any number'''
if num <= 0:
return 1
else:
return num * factorial(num -1 )
factorial(6)
def factorial1(num):
'''Factorial of any number'''
result = 1
for x in range(1,num+1):
result = result * x
return result
factorial1(6)
help(factorial)
#dir(factorial)
factorial.__doc__
# Local Variable in Python
def local_func(num):
'''Local Function'''
print("Variable without assignment",num)
num = 200
print("Variable after Assignment",num)
num = 300
local_func(400)
print("Variable after Local function",num)
# Local Variable in Python
def global_func():
'''Global Function'''
global num
print("Variable without assignment",num)
num = 200
print("Variable after Assignment",num)
num = 300
global_func()
print("Variable after global function",num)
def vararg(arg1,arg2=0, *arg, **kwarg):
'''Variable Argument Function'''
print("Argument1 %s and Argument2 %s"%(arg1,arg2))
print("Variable Arguments",arg)
print(" Default Variable Arguments",kwarg)
vararg('first')
print("\n")
vararg('first','second')
print("\n")
vararg('first','second','third',5,6,7,87,'last')
print("\n")
vararg('first','second','third', 56, forth = 4, fifth = 5)
def multiplier(*arg):
'''Multiplier of any number '''
result = 1
for i in arg:
result = result * i
return result
print(multiplier(2,3,5,8))
print(multiplier(1,2,3,4))
lambda
filter
map
max, min, sum
set
sorted
reversed
zip
Lambda is single line annonymus function.
add = lambda arg1, arg2 : arg1 + arg2
add(34, 56)
filter is a function with two arguments
1) function
2) sequence
filter return those values from sequence where function is True.
list(filter(lambda arg1 : arg1 % 2 != 0
and arg1 % 3 !=0, range(2,25)))
fun = lambda arg1 : arg1 % 2 != 0 and arg1 % 3 !=0
print(fun(2))
print(fun(5))
Salary = [2000, 4000, 5000, 2300, 32000, 8999, 30000]
list(filter(lambda arg1 : arg1 > 2000
and arg1 < 25000, Salary))
Map is a function which takes two or more arguments. First argument is function and second is one or more sequences, provided number of arguments to function are same as number of sequences.
list(map(lambda arg1 : arg1*3 , Salary))
list(map(lambda arg1 : arg1*3 , Salary))
Salary1 = [2000, 4000, 5000, 2300, 32000, 8999, 30000]
Salary2 = [3000, 5000, 6000, 4300, 52000, 9999, 40000]
list(map(lambda arg1, arg2 : arg1 + arg2,
Salary1, Salary2))
print(min(Salary))
print(max(Salary))
print(sum(Salary))
print(max(Salary1 + Salary2))
print(sum(Salary1 + Salary2))
Set is a unorder collection of unique, immutable objects.
Salary1 = [2000, 4000, 5000, 2300, 32000, 5000,
2000, 8999, 30000]
Salary2 = [3000, 5000, 6000, 4300, 3000,
52000, 9999, 40000, 5000]
Set1 = set(Salary1)
print(Set1)
Set2 = set(Salary2)
print(Set2)
40000 in Set1
Set3 = Set1.union(Set2)
print(Set3)
Set4 = Set1 | Set2
print(Set4)
print(Set3.issuperset(Set1))
print(Set1.issubset(Set3))
Set5 = Set1.intersection(Set2)
print(Set5)
Set6 = Set1 & Set2
print(Set6)
print(Set5.issuperset(Set1))
print(Set1.issubset(Set6))
sorted(Set1)
sorted(Set1, reverse = True)
list(reversed(Salary1))
List1 = list(reversed('SURENDRA'))
print(List1)
Str3 = ':'.join(List1)
print(Str3)
List2 = Str3.split(':')
print(List2)
Str4 = ' '.join(List1)
print(Str4)
List3 = Str4.split()
print(List3)
Salary1
Salary2
list(zip(Salary1, Salary2))
dict(zip(Salary1, Salary2))
Que = ['name','age','city','state','country']
Ans = ['Surendra','42','Pune','Maharashtra','India']
for q, a in zip(Que, Ans):
print("What's your %s, it is %s"%(q,a))
[ num for num in range(2,25) if num % 2 !=0 and
num % 3 != 0 ]
[ (num, num**3) for num in range(2,25)
if num % 2 !=0 and num % 3 != 0 ]
Words = ['abc','def','ghi','jkl']
Expected List using List Comprehension
['a','b','c','d'....'l']
Words = ['abc','def','ghi','jkl']
[ ch for word in Words for ch in word ]
[ (ch, ord(ch)) for word in Words for ch in word ]
[ (ch, ord(ch), ch.upper()) for word in Words
for ch in word ]
Dict1 = dict(zip(Que,Ans))
print(Dict1)
{ key : value for key, value in Dict1.items() }
Dict2 = { value : key for key, value in Dict1.items() }
Dict2
{ key : value for key, value in Dict1.items()
if value == '42' }
# %load BooksApp.py
'''
Dictionary Application will consist of
Books Dictionay,
Authors Dictionary and
Publishers Dictionary. This will include relation of
Authors with Book ( Many to Many )
Relation of Publisher with Book ( One to One )
'''
# Step 1. Create Publisher Dictionary
print("*** Publishers Dictionary ****\n")
Publisher1 = { 'pid' : 1, 'pname' : 'Orielly Pub',
'pcity' : 'Chennai',
'pweb' : 'https://www.orielly.org' }
print(Publisher1)
Publisher2 = { 'pid' : 2, 'pname' : 'BPB Pub',
'pcity' : 'Delhi',
'pweb' : 'https://www.bpb.org' }
print(Publisher2)
Publisher3 = { 'pid' : 3, 'pname' : 'TataMcgraw Pub',
'pcity' : 'Mumbai',
'pweb' : 'https://www.tatamcgraw.org' }
print(Publisher3)
Publishers = {}
Publishers['P1'] = Publisher1
Publishers['P2'] = Publisher2
Publishers['P3'] = Publisher3
print("\n")
print("*** Publishers Dictionary ****\n")
print(Publishers)
# Step2 Create Authors Dictionary
print("\n")
print("*** Authors Dictionary ****\n")
Author1 = {'first_name' : 'Surendra',
'last_name' : 'Panpaliya',
'city' : 'Pune',
'email' : 'surendra@gktcs.com' }
print(Author1)
Author2 = {'first_name' : 'Narendra',
'last_name' : 'Panpaliya',
'city' : 'Pune',
'email' : 'narendra@gktcs.com' }
print(Author2)
Author3 = {'first_name' : 'Satish',
'last_name' : 'Panpaliya',
'city' : 'Chicago',
'email' : 'satish@gktcs.com' }
print(Author3)
Authors = { }
Authors['A1'] = Author1
Authors['A2'] = Author2
Authors['A3'] = Author3
print("\n")
print("*** Authors Dictionary ****\n")
print(Authors)
print("*** Books Dictionary ****\n")
print("\n")
Book1 = { 'title' : 'Bytes of Python',
'publisher' : Publishers['P1'],
'authors' : [ Authors['A1'], Authors['A2'] ],
'pub_date' : '21-09-2019' }
Book2 = { 'title' : 'Django',
'publisher' : Publishers['P1'],
'authors' : [ Authors['A2'], Authors['A3'] ],
'pub_date' : '20-09-2019' }
Book3 = { 'title' : 'Data Science',
'publisher' : Publishers['P1'],
'authors' : [ Authors['A1'], Authors['A3'] ],
'pub_date' : '23-09-2019' }
Books = { }
Books['B1'] = Book1
print(Books['B1'])
Books['B2'] = Book2
Books['B3'] = Book3
print("**** Books Library***")
Books
import random
random.randint(1,4)
#Feedback Link
#https://www.surveymonkey.com/r/CGH9NG8
import yaml
ymldata= '''
- eggs
- ham
- spam
- French basil salmon terrine
'''
with open('foo.yaml','w') as f:
f.write(ymldata)
import yaml
if __name__ == '__main__':
stream = open("foo.yaml", 'r')
data = yaml.load(stream, Loader=yaml.FullLoader)
for item in data:
print(item)
pip install pyyaml
%%writefile items.yaml
raincoat: 1
coins: 5
books: 23
spectacles: 2
chairs: 12
pens: 6
%%writefile data.yaml
cities:
- Bratislava
- Kosice
- Trnava
- Moldava
- Trencin
---
companies:
- Eset
- Slovnaft
- Duslo Sala
- Matador Puchov
import yaml
with open('items.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
print(data)
import yaml
with open('data.yaml') as f:
docs = yaml.load_all(f, Loader=yaml.FullLoader)
for doc in docs:
for k, v in doc.items():
print(k, "->", v)
import yaml
users = [{'name': 'John Doe', 'occupation': 'gardener'},
{'name': 'Lucy Black', 'occupation': 'teacher'}]
print(yaml.dump(users))
import yaml
with open('items.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
print(data)
sorted = yaml.dump(data, sort_keys=True)
print(sorted)
import yaml