28
A Caffeinated Crash Course in Python

A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

A Caffeinated Crash Course inPython

Page 2: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Python is not….

• Java• C• Perl

Page 3: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

The Python Interpreter

• Type “python” at the command prompt• In windows, find the python icon on the

start menu

Page 4: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Dir and Help

help()

dir()

Page 5: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Syntax Errors

• Python Errors show the line number ofthe error

• Check the line above if your error makesno sense

Page 6: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

White Space

Page 7: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

String Basics

• Not a mutable data type

• String can be delimited with either the “or ‘

Page 8: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

More Strings

• Concatenation uses the +

• You can do math with strings!

Page 9: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Output

Page 10: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Indexing• To index into a string, specify the position

inside square brackets

• You can index into a string from the “end” ofthe string.

Page 11: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Slicing• A Substring of a string is a slice

• Your head or tail can be a negativeindex

Page 12: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

More Slicing

• You don’t need to specify the beginningand end of the string

• Find the length of a string with len()

Page 13: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Example

Page 14: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Lists

• Lists in python are made of any datatype delimited by commas andsurrounded by brackets.

• Lists are mutable

Page 15: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

More on Lists

• You can index into lists

• You can slice lists

Page 16: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Modifying Lists

• You can add lists

• And append to them

Page 17: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

List Methods

• sort - sorts the list in place, returns nothing• sorted - does not modify the list, returns new

sorted list• reverse - reverses the list in place, returns

nothing

Page 18: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

String Formatting

• The % operator substitutes values into astring

• %s and %d are placeholders for the values(%d makes sure it’s a number)

• “%s has %d letters” %(“colorless”, len(“colorless”))becomes the string “colorless has 9letters”

Page 19: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Converting fromStrings to Lists

• Join a list to make a string

• Split a string to make a list

Page 20: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

For and If

• If statements

• For Statements

Page 21: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

List Comprehensions

• Applies a function to every element of alist

Page 22: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Dictionaries

• Hash - maps things to things!

Page 23: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Even More Dictionaries

Page 24: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Example: Letter Frequencies

Page 25: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Classes

Page 26: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Importing and the Python path

• Import using the import command• You can import everything from a

module using the syntax “from<module> import *”

Page 27: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Files

Filename = “/home/havasi/input.txt”input = open(Filename, ‘r’)output = open(Filename + ‘.out’, ‘w’)for line in input.readlines():

input.write(‘Cows! \n’)input.close()output.close()

Page 28: A Caffeinated Crash Course in Python · 2008-07-22 · python tutorial.ppt Author: Catherine Havasi Created Date: 9/6/2007 6:28:07 PM

Resources

• Python.org• NLTK Python Tutorial

– http://nltk.org/doc/en/programming.html• IDLE (Windows Development Env.)

– http://www.python.org/idle/