Upload
rolando-merino-velez
View
242
Download
0
Embed Size (px)
Citation preview
7/27/2019 Python Tutorial Descafeinado
1/28
A Caffeinated Crash Course inPython
7/27/2019 Python Tutorial Descafeinado
2/28
Python is not.
Java
C Perl
7/27/2019 Python Tutorial Descafeinado
3/28
The Python Interpreter
Type python at the command prompt
In windows, find the python icon on thestart menu
7/27/2019 Python Tutorial Descafeinado
4/28
Dir and Help
help()
dir()
7/27/2019 Python Tutorial Descafeinado
5/28
Syntax Errors
Python Errors show the line number of
the error
Check the line above if your error makes
no sense
7/27/2019 Python Tutorial Descafeinado
6/28
White Space
7/27/2019 Python Tutorial Descafeinado
7/28
String Basics
Not a mutable data type
String can be delimited with either the
or
7/27/2019 Python Tutorial Descafeinado
8/28
More Strings
Concatenation uses the +
You can do math with strings!
7/27/2019 Python Tutorial Descafeinado
9/28
Output
7/27/2019 Python Tutorial Descafeinado
10/28
Indexing
To index into a string, specify the positioninside square brackets
You can index into a string from the end of
the string.
7/27/2019 Python Tutorial Descafeinado
11/28
Slicing A Substring of a string is a slice
Your head or tail can be a negative
index
7/27/2019 Python Tutorial Descafeinado
12/28
More Slicing
You dont need to specify the beginning
and end of the string
Find the length of a string with len()
7/27/2019 Python Tutorial Descafeinado
13/28
Example
7/27/2019 Python Tutorial Descafeinado
14/28
Lists
Lists in python are made of any data
type delimited by commas and
surrounded by brackets.
Lists are mutable
7/27/2019 Python Tutorial Descafeinado
15/28
More on Lists
You can index into lists
You can slice lists
7/27/2019 Python Tutorial Descafeinado
16/28
Modifying Lists
You can add lists
And append to them
7/27/2019 Python Tutorial Descafeinado
17/28
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, returnsnothing
7/27/2019 Python Tutorial Descafeinado
18/28
String Formatting
The % operator substitutes values into astring
%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
7/27/2019 Python Tutorial Descafeinado
19/28
Converting from
Strings to Lists
Join a list to make a string
Split a string to make a list
7/27/2019 Python Tutorial Descafeinado
20/28
For and If
If statements
For Statements
7/27/2019 Python Tutorial Descafeinado
21/28
List Comprehensions
Applies a function to every element of a
list
7/27/2019 Python Tutorial Descafeinado
22/28
Dictionaries Hash - maps things to things!
7/27/2019 Python Tutorial Descafeinado
23/28
Even More Dictionaries
7/27/2019 Python Tutorial Descafeinado
24/28
Example: Letter Frequencies
7/27/2019 Python Tutorial Descafeinado
25/28
Classes
7/27/2019 Python Tutorial Descafeinado
26/28
Importing and the Python path Import using the import command
You can import everything from a
module using the syntax from
import *
7/27/2019 Python Tutorial Descafeinado
27/28
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()
7/27/2019 Python Tutorial Descafeinado
28/28
Resources Python.org
NLTK Python Tutorial
http://nltk.org/doc/en/programming.html
IDLE (Windows Development Env.)
http://www.python.org/idle/