lesson1_python an introduction
TRANSCRIPT
-
8/6/2019 Lesson1_Python An Introduction
1/99
Python An Introduction
Arulalan.T
Centre for Atmospheric Sciences
Indian Institute of Technology Delhi
mailto:[email protected]:[email protected] -
8/6/2019 Lesson1_Python An Introduction
2/99
Python is a Programming Language
-
8/6/2019 Lesson1_Python An Introduction
3/99
There areso many
Programming Languages.
Why Python?
-
8/6/2019 Lesson1_Python An Introduction
4/99
-
8/6/2019 Lesson1_Python An Introduction
5/99
-
8/6/2019 Lesson1_Python An Introduction
6/99
Python is simple and beautiful
-
8/6/2019 Lesson1_Python An Introduction
7/99
Python is Easy to Learn
-
8/6/2019 Lesson1_Python An Introduction
8/99
Python is Free Open Source Software
-
8/6/2019 Lesson1_Python An Introduction
9/99
Can Do
Text Handling
System Administration
GUI programming Web Applications
Database Apps
Scientific Applications
Games
NLP
...
-
8/6/2019 Lesson1_Python An Introduction
10/99
H i s t o r y
-
8/6/2019 Lesson1_Python An Introduction
11/99
Guido van Rossum
Father of Python1991
-
8/6/2019 Lesson1_Python An Introduction
12/99
Perl Java Python Ruby PHP1987 1991 1993 1995
-
8/6/2019 Lesson1_Python An Introduction
13/99
What is
Python?
-
8/6/2019 Lesson1_Python An Introduction
14/99
Python is...
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.
-
8/6/2019 Lesson1_Python An Introduction
15/99
Quick and Easy
Intrepreted Scripting Language
Variable declarations are unnecessary
Variables are not typed
Syntax is simple and consistent
Memory management is automatic
-
8/6/2019 Lesson1_Python An Introduction
16/99
Object Oriented Programming
Classes
Methods
Inheritance
Modules
etc.,
-
8/6/2019 Lesson1_Python An Introduction
17/99
Examples!
-
8/6/2019 Lesson1_Python An Introduction
18/99
-
8/6/2019 Lesson1_Python An Introduction
19/99
print Hello World
-
8/6/2019 Lesson1_Python An Introduction
20/99
No Semicolons !
-
8/6/2019 Lesson1_Python An Introduction
21/99
Indentation
-
8/6/2019 Lesson1_Python An Introduction
22/99
You have to follow
the Indentation
Correctly.
Otherwise,
Python will beat
you !
-
8/6/2019 Lesson1_Python An Introduction
23/99
-
8/6/2019 Lesson1_Python An Introduction
24/99
Discipline
Makes
Good
-
8/6/2019 Lesson1_Python An Introduction
25/99
Variables
colored_index_cards
-
8/6/2019 Lesson1_Python An Introduction
26/99
No Need to Declare Variable Types !
Python Knows Everything !
-
8/6/2019 Lesson1_Python An Introduction
27/99
value = 10
print value
value = 100.50
print value
value = This is String
print value * 3
-
8/6/2019 Lesson1_Python An Introduction
28/99
Input
-
8/6/2019 Lesson1_Python An Introduction
29/99
name = raw_input(What is Your name?)
print "Hello" , name , "Welcome"
-
8/6/2019 Lesson1_Python An Introduction
30/99
Flow
-
8/6/2019 Lesson1_Python An Introduction
31/99
if score >= 5000 :print You win!
elif score
-
8/6/2019 Lesson1_Python An Introduction
32/99
Loop
-
8/6/2019 Lesson1_Python An Introduction
33/99
for i in range(1, 5):
print i
else:print 'The for loop is over'
b
-
8/6/2019 Lesson1_Python An Introduction
34/99
number = 23
while True :
guess = int(raw_input('Enter an integer : '))
if guess == number :
print 'Congratulations, you guessed it.'
running = Falseelif guess < number :
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'
print 'Done'
-
8/6/2019 Lesson1_Python An Introduction
35/99
Array
-
8/6/2019 Lesson1_Python An Introduction
36/99
List = Array
numbers = [ "zero", "one", "two", "three",
"FOUR" ]
-
8/6/2019 Lesson1_Python An Introduction
37/99
List = Array
numbers = [ "zero", "one", "two", "three","FOUR" ]
numbers[0]>>> zero
numbers[4] numbers[ 1]
>>> FOUR >>> FOUR
numbers[ 2]
>>> three
Multi Dimension List
-
8/6/2019 Lesson1_Python An Introduction
38/99
Multi Dimension List
numbers = [ ["zero", "one"],["two", "three","FOUR" ]]
numbers[0]>>> ["zero", "one"]
numbers[0][0] numbers[ 1][ 1]
>>> zero >>> FOUR
len(numbers)
>>> 2
S Li
-
8/6/2019 Lesson1_Python An Introduction
39/99
Sort List
primes = [ 11, 5, 7, 2, 13, 3 ]
S t Li t
-
8/6/2019 Lesson1_Python An Introduction
40/99
Sort List
primes = [ 11, 5, 7, 2, 13, 3 ]
primes.sort()
S t Li t
-
8/6/2019 Lesson1_Python An Introduction
41/99
Sort List
primes = [ 11, 5, 7, 2, 13, 3 ]
primes.sort()
>>> [2, 3, 5, 7, 11, 13]
S t Li t
-
8/6/2019 Lesson1_Python An Introduction
42/99
Sort List
names = [ "Shrini", "Bala", "Suresh","Arul"]
names.sort()
>>> ["Arul", "Bala","Shrini","Suresh"]
names.reverse()
>>> ["Suresh","Shrini","Bala","Arul"]
Mi d Li t
-
8/6/2019 Lesson1_Python An Introduction
43/99
Mixed List
names = [ "Shrini", 10, "Arul", 75.54]
names[1]+10
>>> 20
names[2].upper()
>>> ARUL
Mixed List
-
8/6/2019 Lesson1_Python An Introduction
44/99
Mixed List
names = [ "Shrini", 10, "Arul", 75.54]
names[1]+10
>>> 20
names[2].upper()
>>> ARUL
Append on List
-
8/6/2019 Lesson1_Python An Introduction
45/99
Append on List
numbers = [ 1,3,5,7]
numbers.append(9)
>>> [1,3,5,7,9]
-
8/6/2019 Lesson1_Python An Introduction
46/99
Tuplesimmutable
-
8/6/2019 Lesson1_Python An Introduction
47/99
names = ('Arul','Dhastha','Raj')
name.append('Selva')
Error : Can not modify the tuple
Tuple is immutable type
-
8/6/2019 Lesson1_Python An Introduction
48/99
String
-
8/6/2019 Lesson1_Python An Introduction
49/99
name = 'Arul'
name[0]
>>>'A'
myname = 'Arul' + 'alan'>>> 'Arulalan'
split
-
8/6/2019 Lesson1_Python An Introduction
50/99
name = 'This is python string'
name.split(' ')
>>>['This','is','python','string']
comma = 'Shrini,Arul,Suresh'
comma.split(',')
>>> ['Shrini','Arul','Suresh']
join
-
8/6/2019 Lesson1_Python An Introduction
51/99
li = ['a','b','c','d']
s = ' '
new = s.join(li)
>>> a b c d
new.split(' ')>>>['a','b','c','d']
-
8/6/2019 Lesson1_Python An Introduction
52/99
'small'.upper()
>>>'SMALL'
'BIG'.lower()
>>> 'big'
'mIxEd'.swapcase()>>>'MiXwD'
-
8/6/2019 Lesson1_Python An Introduction
53/99
{
-
8/6/2019 Lesson1_Python An Introduction
54/99
menu = {idly : 2.50,
dosai : 10.00,coffee : 5.00,ice_cream : 5.00,
100 : Hundred}
menu[idly]
2.50
menu[100]Hundred
-
8/6/2019 Lesson1_Python An Introduction
55/99
Function
-
8/6/2019 Lesson1_Python An Introduction
56/99
def sayHello():
print 'Hello World!' # block belonging of fn
# End of function
sayHello() # call the function
-
8/6/2019 Lesson1_Python An Introduction
57/99
def printMax(a, b):if a > b:
print a, 'is maximum'
else: print b, 'is maximum'
printMax(3, 4)
-
8/6/2019 Lesson1_Python An Introduction
58/99
Using in built Modules
-
8/6/2019 Lesson1_Python An Introduction
59/99
#!/usr/bin/python
# Filename: using_sys.py
import time
print 'The sleep started'
time.sleep(3)
print 'The sleep finished'
-
8/6/2019 Lesson1_Python An Introduction
60/99
#!/usr/bin/python
import os
os.listdir('/home/arulalan')
os.walk('/home/arulalan')
-
8/6/2019 Lesson1_Python An Introduction
61/99
Making Our Own Modules
#!/usr/bin/python
-
8/6/2019 Lesson1_Python An Introduction
62/99
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print Hi, this is mymodule speaking.
version = '0.1'
# End of mymodule.py
#!/ /bi / th
-
8/6/2019 Lesson1_Python An Introduction
63/99
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
#!/usr/bin/python
-
8/6/2019 Lesson1_Python An Introduction
64/99
# Filename: mymodule_demo2.py
from mymodule import sayhi, version# Alternative:
# from mymodule import *
sayhi()
print 'Version', version
-
8/6/2019 Lesson1_Python An Introduction
65/99
Class
Classes
-
8/6/2019 Lesson1_Python An Introduction
66/99
class Person:
pass # An empty block
p = Person()
print p
Classes
-
8/6/2019 Lesson1_Python An Introduction
67/99
class Person:
def sayHi(self):
print 'Hello, how are you?'
p = Person()
p.sayHi()
Classes
-
8/6/2019 Lesson1_Python An Introduction
68/99
class Person:
def __init__(self, name):
#like contstructor
self.name = name
def sayHi(self):print 'Hello, my name is', self.name
p = Person('Arulalan.T')
p.sayHi()
Classes
-
8/6/2019 Lesson1_Python An Introduction
69/99
Inheritance
Classes
-
8/6/2019 Lesson1_Python An Introduction
70/99
class A:
def hello(self):
print ' I am super class '
class B(A):
def bye(self):print ' I am sub class '
p = B()
p.hello()
p.bye()
Classes
-
8/6/2019 Lesson1_Python An Introduction
71/99
class A:
Var = 10def __init__(self):
self.public = 100
self._protected_ = 'protected'self.__private__ = 'private'
Class B(A):
pass
p = B()
p.__protected__
-
8/6/2019 Lesson1_Python An Introduction
72/99
File Handling
-
8/6/2019 Lesson1_Python An Introduction
73/99
File Writing
poem = ''' Programming is fun
-
8/6/2019 Lesson1_Python An Introduction
74/99
When the work is done
if you wanna make your work also fun:use Python!
'''
f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close()
File Reading
-
8/6/2019 Lesson1_Python An Introduction
75/99
-
8/6/2019 Lesson1_Python An Introduction
76/99
f= file('poem.txt','r')for line in f.readlines():
print line
f.close()
Database Intergration
-
8/6/2019 Lesson1_Python An Introduction
77/99
import psycopg2
-
8/6/2019 Lesson1_Python An Introduction
78/99
conn = psycopg2.connect(" dbname='pg_database'user='dbuser'host='localhost'password='dbpass' ")
cur = conn.cursor()
cur.execute("""SELECT * from pg_table""")rows = cur.fetchall()
print rows
cur.close()
conn.close()
import psycopg2
-
8/6/2019 Lesson1_Python An Introduction
79/99
conn = psycopg2.connect(" dbname='pg_database'user='dbuser'host='localhost'password='dbpass' ")
cur = conn.cursor()
cur.execute("'insert into pg_table values(1,'python')"')conn.commit()
cur.close()
conn.close()
-
8/6/2019 Lesson1_Python An Introduction
80/99
THE END of code : )
-
8/6/2019 Lesson1_Python An Introduction
81/99
How to learn ?
Python Shell
-
8/6/2019 Lesson1_Python An Introduction
82/99
Interactive Python
Instance Responce
Learn as you type
-
8/6/2019 Lesson1_Python An Introduction
83/99
bpython
ipython
}
teach you very easily
-
8/6/2019 Lesson1_Python An Introduction
84/99
Python can communicate
With
OtherLanguages
-
8/6/2019 Lesson1_Python An Introduction
85/99
C+
Python
-
8/6/2019 Lesson1_Python An Introduction
86/99
-
8/6/2019 Lesson1_Python An Introduction
87/99
Java+
Python
-
8/6/2019 Lesson1_Python An Introduction
88/99
-
8/6/2019 Lesson1_Python An Introduction
89/99
GUI
With
Python
-
8/6/2019 Lesson1_Python An Introduction
90/99
Glade
-
8/6/2019 Lesson1_Python An Introduction
91/99
Glade
+
Python
+GTK
=
GUI APP
GLADE
-
8/6/2019 Lesson1_Python An Introduction
92/99
-
8/6/2019 Lesson1_Python An Introduction
93/99
Using Glade + Python
Web
Web
-
8/6/2019 Lesson1_Python An Introduction
94/99
-
8/6/2019 Lesson1_Python An Introduction
95/99
Web Frame Work in Python
-
8/6/2019 Lesson1_Python An Introduction
96/99
-
8/6/2019 Lesson1_Python An Introduction
97/99
-
8/6/2019 Lesson1_Python An Introduction
98/99
-
8/6/2019 Lesson1_Python An Introduction
99/99