DOC PREVIEW
Brandeis CS 101A - A Caffeinated Crash Course in Python

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A Caffeinated Crash Course inPythonPython is not….• Java• C• PerlThe Python Interpreter• Type “python” at the command prompt• In windows, find the python icon on thestart menuDir and Helphelp()dir()Syntax Errors• Python Errors show the line number ofthe error• Check the line above if your error makesno senseWhite SpaceString Basics• Not a mutable data type• String can be delimited with either the “or ‘More Strings• Concatenation uses the +• You can do math with strings!OutputIndexing• To index into a string, specify the positioninside square brackets• You can index into a string from the “end” ofthe string.Slicing• A Substring of a string is a slice• Your head or tail can be a negativeindexMore Slicing• You don’t need to specify the beginningand end of the string• Find the length of a string with len()ExampleLists• Lists in python are made of any datatype delimited by commas andsurrounded by brackets.• Lists are mutableMore on Lists• You can index into lists• You can slice listsModifying Lists• You can add lists• And append to themList Methods• sort - sorts the list in place, returns nothing• sorted - does not modify the list, returns newsorted list• reverse - reverses the list in place, returnsnothingString 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”Converting fromStrings to Lists• Join a list to make a string• Split a string to make a listFor and If• If statements• For StatementsList Comprehensions• Applies a function to every element of alistDictionaries• Hash - maps things to things!Even More DictionariesExample: Letter FrequenciesClassesImporting and the Python path• Import using the import command• You can import everything from amodule using the syntax “from<module> import *”FilesFilename = “/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• IDLE (Windows Development Env.)–


View Full Document

Brandeis CS 101A - A Caffeinated Crash Course in Python

Download A Caffeinated Crash Course in Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Caffeinated Crash Course in Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Caffeinated Crash Course in Python 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?