SlideShare a Scribd company logo
1 of 17
Download to read offline
A Quick Python Tour


   Brought to you by
What is Python?
• Programming Language created by
  Guido van Rossum
• It has been around for over 20 years
• Dynamically typed, object-oriented
  language
• Runs on Win, Linux/Unix, Mac, OS/2 etc
• Versions: 2.x and 3.x
What can Python do?
•   Scripting
•   Rapid Prototyping
•   Text Processing
•   Web applications
•   GUI programs
•   Game Development
•   Database Applications
•   System Administrations
•   And many more.
A Sample Program
          function

def greetings(name=’’):
    ’’’Function that returns a message’’’
    if name==’’:                                       docstring
       msg = ”Hello Guest. Welcome!”
    else:
       msg = ”Hello %s. Welcome!” % name
    return msg
                           variable
indentation
>>> greetings(“John”)           #     name is ‘John’
‘Hello John. Welcome!’
                                        comment
>>> greetings()
‘Hello Guest. Welcome!’
Python Data Types
• Built-in types
   int, float, complex, long
• Sequences/iterables
      string
      dictionary
      list
     tuple
Built-in Types
• Integer
  >>> a = 5
• Floating-point number
  >>> b = 5.0
• Complex number
  >>> c = 1+2j
• Long integer
  >>> d = 12345678L
String
• Immutable sequence of characters enclosed in
  quotes
  >>> a = “Hello”
  >>> a.upper()    # change to uppercase
  ‘HELLO’
  >>> a[0:2]       # slicing
  ‘He’
List
• Container type that stores a sequence of items
• Data is enclosed within square brackets []
  >>> a = [“a”, “b”, “c”, “d”]
  >>> a.remove(“d”) # remove item “d”
  >>> a[0] = 1          # change 1st item to 1
  >>> a
  [ 1, “b”, “c” ]
Tuple
• Container type similar to list but is immutable
• More efficient in storage than list.
• Data is enclosed within braces ()
  >>> a = (‘a’, ‘b’, ‘c’)
  >>> a[1]
  ‘b’
  >>> a[0] = 1            # invalid
  >>> a += (1, 2, 3) # invalid
  >>> b = a+(1,2,3) # valid, create new tuple
Dictionary
• Container type to store data in key/value pairs
• Data is enclosed within curly braces {}
  >>> a = {“a”:1, “b”:2}
  >>> a.keys()
  [‘a’, ‘b’]
  >>> a.update({‘c’:3}) # add pair {‘c’:3}
  >>> a.items()
  [(‘a’, 1), (‘c’, 3), (‘b’, 2)]
Control Structures
• Conditional
   if, elif, else - branch into different paths
• Looping
   while        - iterate until condition is false
   for          - iterate over a defined range
• Additional control
   break       - terminate loop early
   continue    - skip current iteration
   pass        - empty statement that does nothing
if, else, elif
• Syntax:                    • Example:
  if condition1:               x=1
     statements                y=2
                               if x>y:
  [elif condition2:
                                  print “x is greater.”
     statements]               elif x<y:
  [else:                          print “y is greater.”
     statements]               else:
                                  print “x is equal to y.”
                             • Output:
                                y is greater.
while
• Syntax:               • Example:
  while condition:        x= 1
      statements          while x<4:
                             print x
                              x+=1
                        • Output:
                          1
                          2
                          3
for
• Syntax:                   • Example:
  for item in sequence:       for x in “abc”:
      statements                  print x
                            • Output:
                              a
                              b
                              c
Function
• A function or method is a group of statements
performing a specific task.
• Syntax:                     • Example:
   def fname(parameters):       def triangleArea(b, h):
       [‘’’ doc string ‘’’]       ‘’’Return triangle area‘’’
                                  area = 0.5 * b * h
        statements
                                  return area
       [return expression]
                              • Output:
                                  >>> triangleArea(5, 8)
                                  20.0
                                  >>> triangleArea.__doc__
                                  ‘Return triangle area’
class and object
• A class is a construct that represent a kind using
methods and variables. An object is an instance of a class.
 • Syntax:                   • Example:
  class ClassName:           class Person:
     [class documentation]      def __init__(self, name):
     class statements              self.name = name
                                def introduce(self):
                                   return “I am %s.” % self.name
                             • Output:
                             >>> a = Person(“John”). # object
                             >>> a.introduce()
                             ‘I am John.’
End of Tour

This is just a brief introduction.

What is next?
• Read PySchools Quick Reference
• Practice the online tutorial on PySchools


Have Fun!

More Related Content

What's hot

Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 

What's hot (18)

Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Python : Dictionaries
Python : DictionariesPython : Dictionaries
Python : Dictionaries
 
Iniciando com jquery
Iniciando com jqueryIniciando com jquery
Iniciando com jquery
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
Scala: A brief tutorial
Scala: A brief tutorialScala: A brief tutorial
Scala: A brief tutorial
 
Scala for Java Developers - Intro
Scala for Java Developers - IntroScala for Java Developers - Intro
Scala for Java Developers - Intro
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
Pharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntaxPharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntax
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021
 
Exploring type level programming in Scala
Exploring type level programming in ScalaExploring type level programming in Scala
Exploring type level programming in Scala
 

Viewers also liked

Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)
Jacob Kaplan-Moss
 

Viewers also liked (10)

Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)
 
Make It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version ControlMake It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version Control
 
Capybara
CapybaraCapybara
Capybara
 
Outside-In Development With Cucumber
Outside-In Development With CucumberOutside-In Development With Cucumber
Outside-In Development With Cucumber
 
Prepare for JDK 9
Prepare for JDK 9Prepare for JDK 9
Prepare for JDK 9
 
Introduction to Git for developers
Introduction to Git for developersIntroduction to Git for developers
Introduction to Git for developers
 
Two scoops of Django - Security Best Practices
Two scoops of Django - Security Best PracticesTwo scoops of Django - Security Best Practices
Two scoops of Django - Security Best Practices
 
The WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpecThe WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpec
 
Jenkins CI
Jenkins CIJenkins CI
Jenkins CI
 
Git Branching Model
Git Branching ModelGit Branching Model
Git Branching Model
 

Similar to A quick python_tour

Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
Yandex
 

Similar to A quick python_tour (20)

Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Python ppt
Python pptPython ppt
Python ppt
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless
 
Becoming a Pythonist
Becoming a PythonistBecoming a Pythonist
Becoming a Pythonist
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
 
Python assignment help
Python assignment helpPython assignment help
Python assignment help
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban KGetting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Python
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
Pharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellPharo: Syntax in a Nutshell
Pharo: Syntax in a Nutshell
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

A quick python_tour

  • 1. A Quick Python Tour Brought to you by
  • 2. What is Python? • Programming Language created by Guido van Rossum • It has been around for over 20 years • Dynamically typed, object-oriented language • Runs on Win, Linux/Unix, Mac, OS/2 etc • Versions: 2.x and 3.x
  • 3. What can Python do? • Scripting • Rapid Prototyping • Text Processing • Web applications • GUI programs • Game Development • Database Applications • System Administrations • And many more.
  • 4. A Sample Program function def greetings(name=’’): ’’’Function that returns a message’’’ if name==’’: docstring msg = ”Hello Guest. Welcome!” else: msg = ”Hello %s. Welcome!” % name return msg variable indentation >>> greetings(“John”) # name is ‘John’ ‘Hello John. Welcome!’ comment >>> greetings() ‘Hello Guest. Welcome!’
  • 5. Python Data Types • Built-in types  int, float, complex, long • Sequences/iterables  string  dictionary  list  tuple
  • 6. Built-in Types • Integer >>> a = 5 • Floating-point number >>> b = 5.0 • Complex number >>> c = 1+2j • Long integer >>> d = 12345678L
  • 7. String • Immutable sequence of characters enclosed in quotes >>> a = “Hello” >>> a.upper() # change to uppercase ‘HELLO’ >>> a[0:2] # slicing ‘He’
  • 8. List • Container type that stores a sequence of items • Data is enclosed within square brackets [] >>> a = [“a”, “b”, “c”, “d”] >>> a.remove(“d”) # remove item “d” >>> a[0] = 1 # change 1st item to 1 >>> a [ 1, “b”, “c” ]
  • 9. Tuple • Container type similar to list but is immutable • More efficient in storage than list. • Data is enclosed within braces () >>> a = (‘a’, ‘b’, ‘c’) >>> a[1] ‘b’ >>> a[0] = 1 # invalid >>> a += (1, 2, 3) # invalid >>> b = a+(1,2,3) # valid, create new tuple
  • 10. Dictionary • Container type to store data in key/value pairs • Data is enclosed within curly braces {} >>> a = {“a”:1, “b”:2} >>> a.keys() [‘a’, ‘b’] >>> a.update({‘c’:3}) # add pair {‘c’:3} >>> a.items() [(‘a’, 1), (‘c’, 3), (‘b’, 2)]
  • 11. Control Structures • Conditional  if, elif, else - branch into different paths • Looping  while - iterate until condition is false  for - iterate over a defined range • Additional control  break - terminate loop early  continue - skip current iteration  pass - empty statement that does nothing
  • 12. if, else, elif • Syntax: • Example: if condition1: x=1 statements y=2 if x>y: [elif condition2: print “x is greater.” statements] elif x<y: [else: print “y is greater.” statements] else: print “x is equal to y.” • Output: y is greater.
  • 13. while • Syntax: • Example: while condition: x= 1 statements while x<4: print x x+=1 • Output: 1 2 3
  • 14. for • Syntax: • Example: for item in sequence: for x in “abc”: statements print x • Output: a b c
  • 15. Function • A function or method is a group of statements performing a specific task. • Syntax: • Example: def fname(parameters): def triangleArea(b, h): [‘’’ doc string ‘’’] ‘’’Return triangle area‘’’ area = 0.5 * b * h statements return area [return expression] • Output: >>> triangleArea(5, 8) 20.0 >>> triangleArea.__doc__ ‘Return triangle area’
  • 16. class and object • A class is a construct that represent a kind using methods and variables. An object is an instance of a class. • Syntax: • Example: class ClassName: class Person: [class documentation] def __init__(self, name): class statements self.name = name def introduce(self): return “I am %s.” % self.name • Output: >>> a = Person(“John”). # object >>> a.introduce() ‘I am John.’
  • 17. End of Tour This is just a brief introduction. What is next? • Read PySchools Quick Reference • Practice the online tutorial on PySchools Have Fun!