Docstoc

An Introduction to Python

Document Sample
An Introduction to Python Powered By Docstoc
					An Introduction to Python
             Michael DiRamio
    District School Board of Niagara




  Imperial Oil Summer Institute
               2006
               Summary
•   IDLE: a Python IDE
•   Output
•   Arithmetic
•   Variables
•   Input
•   Looping
•   Selection
 Most Important Feature in a
 Language: Documentation


• The symbol to start documentation in
  Python is ‘#’
   Interpreted Programming
          Languages
• Some programs are compiled, meaning
  that the code you write is translated to
  something the computer understands.
• Others are interpreted, meaning that
  your code is read, as is, by another
  program that then does what the
  program specifies.
• Python is an interpreted language.
      IDLE: A Python IDE
• Free IDE
  download at
  www.python.org
• Has both
  interactions
  window and
  program files
                   Interactions Window
         IDLE: Interaction
• Allows you to try out an expression with
  immediate results.
• Output is the result of the expression
       Activity: Exploration
• Play with the interactions window, see
  what the results are.
• Exploration is a good way to learn, and
  this window provides an easy
  opportunity to explore.
    IDLE: Writing Programs
• Programs can be written in files,
  saved, and run.
• The results are produced in the
  interactions window.
                     Output
• The output keyword in Python is “print”. print
  will output anything, Strings, numbers, etc.
• Note the difference between simply putting
  “Hello” in the interactions window, and
  putting print “Hello”

                                  >>>
     print 4 + 6                  10
     print "Hello"                Hello
       program                       output
      Activity: First Program
• Write a program (not in the interactions
  window, in a separate file) that prints
  the phrase “Hello World” on the screen
• Run this program (Run -> Run Module)
  and see the output produced in the
  interactions window
           Concatenation
• String concatenation is the “+” symbol


           >>> 'Hello ' + 'World'
           'Hello World'
                          “ or ‘ ?
• Python allows you to use either single
  or double quotes for Strings.
• This prevents needing special
  characters if you want to use a quote in
  your string.
 >>> print 'This contains "double quotes"'
 This contains "double quotes"
 >>> print "And here are 'single quotes' inside"
 And here are 'single quotes' inside
                  Math
• The standard arithmetic operations: +, -,
  /, *, % (remainder)
• Note: if you divide two integers, the
  result will be an integer
• More math functions like ceiling and
  floor are in the math module (see
  intermediate Python)
         Activity: Division
• Explore the difference between
5/2
and
5.0 / 2
               Variables
• Python variables      >>> x = 6
  don’t carry type.     >>> print x + 3
                        9
  You can assign a      >>> x = "Hello"
                        >>> print x
  variable to be a      Hello
  string, then assign   >>> print x + " world"
                        Hello world
  it to an integer
  later
                          Input
  • The method raw_input(string) returns a
    string the user has typed in
  • The parameter to raw_input is the
    prompt given to the user.
x = raw_input("<TYPE SOMETHING>")
print "The user typed: " + x
      Program
                      >>>
                      <TYPE SOMETHING>This is my input
                      The user typed: This is my input
                                                Output
                         Input
 • The string input can be converted to
   other types using conversion methods
   like int(string) and float(string)
x = raw_input("<TYPE A NUMBER> ")
y = int(x)
print y * 2
   Program
                                    >>>
                                    <TYPE A NUMBER> 11
                                    22
                                               Output
   Activity: Input Conversion
• Write a program that reads in a
  temperature in Fahrenheit and converts
  it to Celsius
• C = (F - 32) * 5 / 9
             Indentation
• Python doesn’t use brackets to define
  blocks of code
• Indentation tells the program where a
  section ends
• If a program isn’t properly indented
                 Loops
                                       >>>
                                       0
• The loop            i=0
                                       1
                                       2
  continues           while i < 10 :
                                       3
                        print i
  iterating as long     i=i+1
                                       4
                                       5
  as the condition    print "done!"
                                       6
  following “while”      Program
                                       7
                                       8
  remains true                         9
                                       done!
                                        Output
     Activity: Importance of
           Indentation
• Create a program that uses a loop, like
  the previous example.
• Now remove the indentation and try
  running the program
            Comparisions
• “==“ is used for equality testing
• Inequality is “!=“
• Inequalities are straight forward: <=, >=,
  <, >
              >>> print 8 < 15
              True
              >>> print 9 != 7
              True
                  Interactions Window
      Selection Statements
• if/elif/else
• Must have one if clause
• Followed by 0 or more elif (else if)
  clauses
• Followed by 0 or 1 else clause
              Selection Statements:
                     Example
                                  • Notice what
x = raw_input("[input please]")
x = int(x)                        happens when
if x < 10 :                       you remove the
    print "Small"
elif x < 20 :                     indentation.
    print "Medium"
elif x < 30 :
    print "Big"
else :                            >>>
    print "HUGE!"                 [input please]21
print "Done"                      Big
                                  Done
    Program
                                         Output
        Activity: Selection
• Write a program that reads in a number
  and determines if that number is even
  or odd.
          Use in Computer
           Programming
• Free IDE
• The interactions window allows students to
  quickly test out ideas.
• Lack of brackets keeps code uncluttered.
• Students *must* indent their code properly or
  it won’t function properly.
• Lightweight syntax prevents students from
  becoming lost in the picky details of a
  language.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:4/12/2013
language:English
pages:26