Introduction to Python Michael DiRamio District School Board of Niagara Michael'Diramiodsbn'edu'on'c PowerPoint PPT Presentation

presentation player overlay
1 / 35
About This Presentation
Transcript and Presenter's Notes

Title: Introduction to Python Michael DiRamio District School Board of Niagara Michael'Diramiodsbn'edu'on'c


1
Introduction to PythonMichael DiRamioDistrict
School Board of NiagaraMichael.Diramio_at_dsbn.edu.o
n.ca
  • Imperial Oil Summer Institute
  • 2007

2
Python
  • Python is a fairly young (1990) language that is
    gaining popularity in education and industry.
  • Used by YouTube, Google, Industrial Light and
    Magic.
  • University of Toronto is moving to Python as
    their first year programming language.

3
Why Python?
  • Python is becoming more popular as a first
    programming language because
  • It has a very simple, lightweight syntax.
  • The interpreter window makes it easy for students
    to try ideas.
  • It still has the power to cover all the high
    level concepts of computer science and has
    industry applications.

4
Why not Python?
  • Dynamic typing may cause problems for students.
  • The built in classes and functions are not well
    documented.
  • Because it is a young language, it is still a
    work in progress.

Conversely, students may find it more
straight forward to not have to declare the type
of a variable.
5
Overview
  • Python is an interpreted, dynamically typed,
    object oriented programming language.
  • We will explore each of these concepts in this
    presentation.

6
Interpreted Languages
  • An interpreted language is not run directly on
    the CPU.
  • There is another program running on the computer
    which then reads the lines of Python code and
    creates the appropriate output.

7
Python IDE IDLE
  • IDLE is a free Python integrated development
    environment.
  • There are two main components we will be using
  • Python Shell
  • File Window

8
Comments
  • Commented lines in Python start with a . All
    text beyond a will be ignored.

9
Output The print command
  • The print command is similar to put in Turing
    or System.out.println in Java.
  • It outputs what follows it and then prints a
    newline.
  • To suppress the newline, put a comma (,) at the
    end of the statement.
  • To concatenate output put a comma (,) between
    elements.
  • Python puts a space between concatenated elements.

10
Running a Program
  • While in the File Window, go to
  • Run ? Run Script
  • IDLE will make you save any changes before
    running the program.

11
Variables
  • Variables in Python are dynamically typed.
  • Variables are not declared,they are just assigned
    a value.
  • Type information is stored with the object, not
    with the variable.
  • Variables can change type by assigning it to an
    object of a different type.

12
Built in Variable Types
  • Integer
  • Integers are unbounded in Python
  • Floating Point
  • Boolean True and False
  • String

13
Arithmetic Operations
Note Dividing two integers will still result in
integer division with both / and //.
14
Strings
  • Strings are immutable (cannot be changed after
    creation).
  • Concatenation is done with the operator.
  • There is no char type in Python, just Strings of
    length one.

15
Substrings
  • Indexing for Strings starts at 0.
  • Examples below use the Strings 0123456789

16
Either Quotes
  • Strings in Python can use either single or double
    quotes.
  • This allows you to use the opposite quotes inside
    a String.
  • You can also use the escape character, \ (same
    as Java).

print He said Hello, I am a string."' print
"The programmer replied 'My program shouldn't be
talking!'" print "What's wrong with saying
\"Hello\"?"
17
Converting Strings to Other Types
18
Input
  • The command raw_input(prompt) is used to get
    input from the user.
  • Prompt is a message printed on the screen before
    waiting for input.
  • If you dont want a prompt, call raw_input() with
    no parameter.
  • Combine this method with the conversions when you
    want to read numbers or booleans.

s raw_input("Enter something ") print "You
said", s
19
Comparisons
Note You can also use ltgt for inequality, but
it is considered out of date.
20
Logical Operators
21
No Brackets
  • Python does not use brackets to denote the
    start/end of blocks of code.
  • Instead, indentation is used to specify blocks.
  • This forces programmers to properly indent their
    programs, otherwise the functionality changes.

22
Conditional Statements
  • The conditional statements are followed by colons
    ()
  • Each block is indented.
  • To end the block, go back out to the original
    level of indentation.

x 22 if x lt 15 print "first clause" elif
x lt 25 print "second clause" else
print "the else print "This is after the
statement"
23
Conditional Loops
  • Continue looping as long as the condition is true.

print from 1 to 10 x 1 while x lt 10
print x x x 1 print "Done"
24
Lists
  • Python has a built in List data structure that
    can hold any type.
  • Indexing starts at 0.
  • Unlike Strings, lists are mutable (can be
    modified).
  • Elements can be changed, added, and removed from
    the list.
  • List elements are not all forced to be the same
    type.

25
Modifying Lists
26
For Loops
  • Different than what we think of as a counting
    loop like in languages such as Java.
  • Loops go through each element of a set.

prints the three values for i in 10,20,14
print i
27
The range Function
  • The range function returns a list of the numbers
    in a given range.
  • It is often used with for loops.

28
For Loops and range
  • Putting these two concepts together gives us our
    usual counting loop.

print 0 - 9 for i in range(10) print i
calculate 4 factorial x 4 for i in range(3, 0,
-1) x x i print x
29
Functions
  • Functions must be defined before they are used.
  • This does not stop referencing of functions that
    have not yet been defined, but the functions can
    not be called until every part is defined.
  • All functions return something. If you do not
    specify a return, it is the constant None.

30
Declaring Functions
  • The keyword def indicates the start of a function
    definition.
  • The body of the function is indented under this
    header.
  • Parameters are contained in brackets after the
    function name.
  • There is no type listed with the parameters.

31
Function Examples
def foo() print "Hello" print "There"
print "World"
print s n times def bar(s, n) for i in
range(n) print s
def max(a, b) if a gt b return a
else return b
Calling Functions foo() max(2, 6) bar("Hello",
4)
32
Opening a File
  • The function to open a file is
  • open(name, mode)
  • Name and mode should both be Strings

33
Read Methods
34
Write Methods
  • write does not add a newline to output.
  • The parameter to write should be a String. Output
    needs to be converted to a String using the str
    method.

35
Contact and References
  • If you have any questions about this
    presentation, please dont hesitate to contact
    me
  • Michael.DiRamio_at_dsbn.edu.on.ca
  • I used the following references to help me
    prepare this presentation
  • www.python.org
  • Learning Python (OReilly)
  • Python in a Nutshell (OReilly)
Write a Comment
User Comments (0)
About PowerShow.com