SlideShare ist ein Scribd-Unternehmen logo
1 von 109
Downloaden Sie, um offline zu lesen
Python – An Introduction
Arulalan.T
arulalant@gmail.com
Centre for Atmospheric Science 
Indian Institute of Technology Delhi
Python is a Programming Language
There are so many 
Programming Languages.
Why Python?
Python is simple and beautiful
Python is Easy to Learn
Python is Free Open Source Software
Can Do
● Text Handling
● System Administration
● GUI programming
● Web Applications
● Database Apps
● Scientific Applications
● Games
● NLP
● ...
  H i s t o r y
Guido van Rossum 
  Father of Python 
           1991
                 Perl  Java  Python   Ruby    PHP
            1987       1991           1993      1995
What is
Python?
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.
Quick and Easy
Intrepreted Scripting Language
Variable declarations are unnecessary
Variables are not typed
Syntax is simple and consistent
Memory management is automatic
     Object Oriented Programming
      
      Classes
         Methods
         Inheritance
         Modules
         etc.,
  
    Examples!
print    “Hello World”
         No Semicolons !
          Variables
  colored_index_cards
No Need to Declare Variable Types !
      Python Knows Everything !
value = 10
print value
value = 100.50
print value
value = “This is String ”
print   value * 3     # Oh !
Input
name = raw_input(“What is Your name?”)
print "Hello" , name , "Welcome"
         Indentation
You have to follow 
the Indentation 
Correctly.
Otherwise,
Python will beat 
you !
 Discipline 
   Makes  
    Good 
Flow
if score >= 5000 :
print “You win!”
elif score <= 0 :
print “You lose!”
print “Game over.”
else:
print “Current score:”,score
print “Donen”
  Loop
for  i   in   range(1, 5):
        print    i
else:
        print    'The for loop is over'
Q) Print Multiplication Table of user 
defined number upto N times.
Get both number & N from the User
Hint : Use may use For / While Loop
Soln) Print Multiplication Table of user 
defined number upto N times.
no = int(raw_input(“Enter number ”))
N = int(raw_input(“Enter N value ”)) 
for i in range(1, N + 1):
  print “%d x %d = %d” % (i, no, i*no)
number = 23
running = True
while running :
        guess = int(raw_input('Enter an integer : '))
        if  guess == number :
                print 'Congratulations, you guessed it.'
                running = False 
        elif  guess < number :
                print 'No, it is a little higher than that.'
        else:
                print 'No, it is a little lower than that.'
print  'Done'
Q) What is the core purpose of while 
loop ?
Q) What is the core purpose of while 
loop ?
Ans)  when the loop has to stop w.r.t 
certain condition/s. 
So the no of loops in “while loop” is 
dynamic / undefined one.
Lets have some break
Lets continue
Array
                List = Array
numbers = [ "zero", "one", "two", "three", 
"FOUR" ]  
                List = Array
numbers = [ "zero", "one", "two", "three", 
"FOUR" ]
numbers[0]
>>> zero 
numbers[4]                                 numbers[­1]
>>> FOUR                                  >>> FOUR
                         numbers[­2]
          >>> three
  Multi Dimension List
numbers = [ ["zero", "one"], ["two", "three", 
"FOUR" ] ]
numbers[0]
>>> ["zero", "one"] 
numbers[0][0]                       numbers[­1][­1]
>>> zero                                  >>> FOUR
                         len(numbers)
          >>> 2
                Sort List
primes = [ 11, 5, 7, 2, 13, 3 ]
                Sort List
primes = [ 11, 5, 7, 2, 13, 3 ]
primes.sort()
                Sort List
primes = [ 11, 5, 7, 2, 13, 3 ]
primes.sort()
>>> [2, 3, 5, 7, 11, 13]
                Sort List
names = [ "Shrini", "Bala", "Suresh",
"Arul"]
names.sort()
>>> ["Arul", "Bala","Shrini","Suresh"]
names.reverse()
>>> ["Suresh","Shrini","Bala","Arul"]
                Mixed List
names = [ "Shrini", 10, "Arul", 75.54]
names[1]+10
>>> 20
names[2].upper()
>>> ARUL
         Append on List
numbers = [ 1,3,5,7]
numbers.append(9)
>>> [1,3,5,7,9]
    Tuples
                                                             immutable
names = ('Arul','Dhastha','Raj')
name.append('Selva')
Error : Can not modify the tuple
Tuple is immutable type
    String
name = 'Arul'
name[0]
>>>'A'
myname = 'Arul' + 'alan'
>>> 'Arulalan'
name = 'This is python string'
name.split(' ')
>>> ['This', 'is', 'python', 'string']
comma = 'Shrini,Arul,Suresh'
comma.split(',')
>>> ['Shrini', 'Arul', 'Suresh']
split
li = ['a','b','c','d']
new = '­'.join(li)
>>> 'a­b­c­d'
new.split('­')
>>> ['a', 'b', 'c', 'd']
join
'small'.upper()
>>>'SMALL'
'BIG'.lower()
>>> 'big'
'mIxEd'.swapcase()
>>>'MiXwD'
Dictionary
menu = {
“idly” : 2.50,
“dosai” : 10.00,
“coffee” : 5.00,
“ice_cream” : 5.00,
100 : “Hundred”
}
>>> menu[“idly”]
2.50
>>> menu[100]
”Hundred”
>>> menu.get(“tea”, None)
None
uwind = {
“latitude” : (-90, 90),
“longitude” : (0, 360),
“level” : 850,
“time” : “2013-07-17”,
“units” : None
}
uwind.keys()
uwind.values()
for key, value in uwind.iteritems():
print key, ' = ', value
Q) So tell me now, 
     'what is the use of dictionary ?'
Q) So tell me now, 
     'what is the use of dictionary ?'
Do you know dictionary can take even a 
function as value in it.
      Function
def sayHello():
        print 'Hello World!' # block belonging of fn
# End of function
sayHello() # call the function
def printMax(a, b):
        if a > b:
                print a, 'is maximum'
        else:
                print b, 'is maximum'
printMax(3, 4) 
def getMax(a, b):
        if a > b:
                return a
  print “I will not be printed”    
 # end of if a > b:    
        return b
# end of def getMax(a, b):
mymax = getMax(3, 4) 
print mymax
Q) Write a function to print the passed 
argument number is even or odd... 
Q) Write a function to print the passed argument number 
is even or odd... 
def printEvenOrOdd(no):
print “The passed no “, no, 
if no % 2 == 0:  # condition
print “ is even”
else:
print “ is odd”
printEvenOrOdd(10)
Using in built Modules
#!/usr/bin/python
# Filename: using_sys.py
import time
print 'The sleep started'
time.sleep(3)
print 'The sleep finished'
#!/usr/bin/python
import os
os.listdir('/home/arulalan')
os.mkdir('/home/arulalan/Fun')
print dir(os)
Making Our Own Modules
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
        print “Hi, this is mymodule speaking.”
version = '0.1'
# End of mymodule.py
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, version
# Alternative:                 
# from mymodule import *
sayhi()
print 'Version', version
Class
class Person:
        pass # An empty block
p = Person()
print p
Classes
class Person:
        def sayHi(self):
                print 'Hello, how are you?'
p = Person()
p.sayHi()
Classes
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
                            
Inheritance
Classes
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
class A:
var = 10
        def  __init__(self):
self.public = 100
self._protected_ = 'protected'
self.__private__ = 'private'
Class B(A):
pass
p = B()
p.__protected__
Classes
File Handling
File Writing
poem = ''' Programming is fun
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() 
Q) How can we write CSV files ?
f = open('nos.csv', 'w') # open for 'w'riting
for no in range(10):
f.write(str(no) + ',' + str(no * no) + 'n')
f.close() 
File Reading
f = file('poem.txt','r') 
for line in f.readlines():
print line
f.close() 
THE END
                                                    of code :­)
How to learn ?
                                     
               
Python – Shell
                                     
               
● Interactive Python
● Instance Responce
● Learn as you type
bpython
ipython
                                     
               
} 
teach you very easily
Python can communicate 
                 With
                Other
            Languages
           C
           +
       Python
        Java
           +
       Python
     GUI
        With 
   Python
                 Glade
                    +
                Python
                    +
                 GTK
                    = 
             GUI APP
GLADE
Using Glade + Python
Web
Web
        Web Frame Work in Python
Python / CDAT Tips Blog Links
http://pyaos.johnny­lin.com/?page_id=10
http://pyaos.johnny­lin.com/?page_id=807
http://www.johnny­lin.com/cdat_tips/
http://pyaos.johnny­lin.com/
Python an-intro v2
Python an-intro v2

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Python ppt
Python pptPython ppt
Python ppt
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python introduction
Python introductionPython introduction
Python introduction
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Python basics
Python basicsPython basics
Python basics
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Python basic syntax
Python basic syntaxPython basic syntax
Python basic syntax
 
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
 

Andere mochten auch (6)

Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 

Ähnlich wie Python an-intro v2

Python Programming Introduction For Students
Python Programming Introduction For StudentsPython Programming Introduction For Students
Python Programming Introduction For Students
ShaunakBale1
 

Ähnlich wie Python an-intro v2 (20)

Python an-intro youtube-livestream-day1
Python an-intro youtube-livestream-day1Python an-intro youtube-livestream-day1
Python an-intro youtube-livestream-day1
 
INTRODUCTION TO PYTHON PROGRAMMING .pptx
INTRODUCTION TO PYTHON PROGRAMMING .pptxINTRODUCTION TO PYTHON PROGRAMMING .pptx
INTRODUCTION TO PYTHON PROGRAMMING .pptx
 
Why should you learn to python programming?
Why should you learn to python programming?Why should you learn to python programming?
Why should you learn to python programming?
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
Python basics
Python basicsPython basics
Python basics
 
Python Programming Introduction For Students
Python Programming Introduction For StudentsPython Programming Introduction For Students
Python Programming Introduction For Students
 
Features of Python.pdf
Features of Python.pdfFeatures of Python.pdf
Features of Python.pdf
 
Python
PythonPython
Python
 
637b4894085c4_ppt.pptx
637b4894085c4_ppt.pptx637b4894085c4_ppt.pptx
637b4894085c4_ppt.pptx
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Machine learning session 1
Machine learning session 1Machine learning session 1
Machine learning session 1
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptx
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Govind.ppt.pptx
Govind.ppt.pptxGovind.ppt.pptx
Govind.ppt.pptx
 
4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptx4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptx
 
Python Training in Chandigarh
Python Training in ChandigarhPython Training in Chandigarh
Python Training in Chandigarh
 
Beginning python programming
Beginning python programmingBeginning python programming
Beginning python programming
 

Mehr von Arulalan T

Python an-intro-python-month-2013
Python an-intro-python-month-2013Python an-intro-python-month-2013
Python an-intro-python-month-2013
Arulalan T
 
Pygrib documentation
Pygrib documentationPygrib documentation
Pygrib documentation
Arulalan T
 
Python An Intro
Python An IntroPython An Intro
Python An Intro
Arulalan T
 
Final review contour
Final review  contourFinal review  contour
Final review contour
Arulalan T
 
Contour Ilugc Demo Presentation
Contour Ilugc Demo Presentation Contour Ilugc Demo Presentation
Contour Ilugc Demo Presentation
Arulalan T
 
Contour Ilugc Demo Presentation
Contour Ilugc Demo PresentationContour Ilugc Demo Presentation
Contour Ilugc Demo Presentation
Arulalan T
 
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Arulalan T
 
matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
Arulalan T
 

Mehr von Arulalan T (20)

wgrib2
wgrib2wgrib2
wgrib2
 
Climate Data Operators (CDO)
Climate Data Operators (CDO)Climate Data Operators (CDO)
Climate Data Operators (CDO)
 
CDAT - graphics - vcs - xmgrace - Introduction
CDAT - graphics - vcs - xmgrace - Introduction CDAT - graphics - vcs - xmgrace - Introduction
CDAT - graphics - vcs - xmgrace - Introduction
 
CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction
CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction
CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction
 
CDAT - cdms numpy arrays - Introduction
CDAT - cdms numpy arrays - IntroductionCDAT - cdms numpy arrays - Introduction
CDAT - cdms numpy arrays - Introduction
 
Python an-intro-python-month-2013
Python an-intro-python-month-2013Python an-intro-python-month-2013
Python an-intro-python-month-2013
 
Thermohaline Circulation & Climate Change
Thermohaline Circulation & Climate ChangeThermohaline Circulation & Climate Change
Thermohaline Circulation & Climate Change
 
Python an-intro - odp
Python an-intro - odpPython an-intro - odp
Python an-intro - odp
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Pygrib documentation
Pygrib documentationPygrib documentation
Pygrib documentation
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introduction
 
Python An Intro
Python An IntroPython An Intro
Python An Intro
 
Final review contour
Final review  contourFinal review  contour
Final review contour
 
Contour Ilugc Demo Presentation
Contour Ilugc Demo Presentation Contour Ilugc Demo Presentation
Contour Ilugc Demo Presentation
 
Contour Ilugc Demo Presentation
Contour Ilugc Demo PresentationContour Ilugc Demo Presentation
Contour Ilugc Demo Presentation
 
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
 
Nomography
NomographyNomography
Nomography
 
matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
 
"contour.py" module
"contour.py" module"contour.py" module
"contour.py" module
 
contour analysis and visulaization documetation -1
contour analysis and visulaization documetation -1contour analysis and visulaization documetation -1
contour analysis and visulaization documetation -1
 

Kürzlich hochgeladen

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Kürzlich hochgeladen (20)

Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 

Python an-intro v2