A Caffeinated Crash Course in Python
A Caffeinated Crash Course in Python
Python is not.
Java C Perl
dir()
Syntax Errors
Python Errors show the line number of the error Check the line above if your error makes no sense
White Space
String Basics
Not a mutable data type
More Strings
Concatenation uses the +
Output
Indexing
To index into a string, specify the position inside square brackets
You can index into a string from the end of the string.
Slicing
A Substring of a string is a slice
More Slicing
You dont need to specify the beginning and end of the string
Example
Lists
Lists in python are made of any data type delimited by commas and surrounded by brackets.
More on Lists
You can index into lists
Modifying Lists
You can add lists
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
String Formatting
The % operator substitutes values into a string %s and %d are placeholders for the values (%d makes sure its a number) %s has %d letters % (colorless, len(colorless)) becomes the string colorless has 9 letters
For and If
If statements
For Statements
List Comprehensions
Applies a function to every element of a list
Dictionaries
Hash - maps things to things!
Classes
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()
Resources
Python.org NLTK Python Tutorial
http://nltk.org/doc/en/programming.html