/sfpcPythonWorkshop

Small Python Workshop for SFPC!

Primary LanguagePython

SFPC Python Workshop

Hey everyone, this is a small workshop to get everyone started in Python. Here's what we'll be doing:

Installation

All Macs will come with Python 2 already installed, but we're going to install Anaconda for SFPC. Anaconda provides access to a bunch of tools and libraries to use with python. We will be using Python 2 for this course, not Python 3. So please use the Python 2.7 installer.

Get Anaconda Here

Running the Python Interpreter from the Terminal

Print / Variables

thingToSay = 'Hello World'
print thingToSay

Strings - Playing with words

  • `'hello' + 'friend' + 'hello'*3

Basic Terminal Use

  • cd (change directory)
  • ls (show contents of directory)

Running Python files from Terminal

  • python filename.py

More String Methods and Lists (( (upper/lower, slice, split/join, append/prepend, del)

  • `string.upper() + string.lower()
  • index notation string[2] list[4]
  • slice: string[2:5] list[0:2]
  • split/join string.split(separator) ' '.join(list)
  • list.append(item) list.prepend(item)
  • del del list[0]

Control Flow (If/Else)

#indents are meaningful, but DONT USE TABS
if (expression):
    #your code here
elif (expression2):
    #this code will only run if expression2 is true and expression1 is false
else:
    #this code runs if all other expressions are false
if var in ['is', 'it', 'one', 'of', 'these']:
    #runs if the variable is part of the list

Loops

for item in list:
    #code runs for each item in list

for num in range(0,10):
    #runs like for loops in other languages, goes from 0,10
while (endCondition):
    #do this code until end condition is true

Functions

def cheer(word):
    return word + " " + word.upper() + " " + "hey " * 2 + word.upper()

print cheer('Todd')

Input/Output

name = raw_input('What\'s your name?')
print name
file = open("testfile.txt","w") 
 
file.write("Hello World") 
file.write("This is our new text file") 
file.write("and this is another line.") 
file.write("Why? Because we can.") 
 
file.close() 
file = open("testfile.txt", "r") 
print file.read() 
print file.readlines()
print file.readline(2)
file = open("testfile.txt", "r")
for line in file.readlines():
   if 'this' in line:
       print line