Catherine Devlin
APCUG 2013 Regional Conference
Sep 15, 2013
http://tinyurl.com/apcug-2013-python-intro
target_letter = "r"
for animal in ["horse", "cat", "racoon", "duck"]:
if target_letter in animal:
print(animal + " contains the letter " + target_letter)
else:
print("sorry, no " + target_letter + " in " + animal)
horse contains the letter r sorry, no r in cat racoon contains the letter r sorry, no r in duck
for
, if
, else
, etc.
def lots_of(x):
return 3 * x
stuff = "milk "
print(type(stuff))
print(lots_of(stuff))
stuff = 3
print(type(stuff))
print(lots_of(stuff))
<class 'str'> milk milk milk <class 'int'> 9
import datetime
print(lots_of(datetime.datetime.now()))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-38-61e691416648> in <module>() 1 import datetime 2 ----> 3 print(lots_of(datetime.datetime.now())) <ipython-input-37-3b437fd333c5> in lots_of(x) 1 def lots_of(x): ----> 2 return 3 * x 3 4 print(lots_of("milk")) 5 print(lots_of(3)) TypeError: unsupported operand type(s) for *: 'int' and 'datetime.datetime'
target_letter = "r"
for animal in ["horse", "cat", "racoon", "duck"]:
if target_letter in animal:
print(animal + " contains the letter " + target_letter)
else:
print("sorry, no " + target_letter + " in " + animal)
horse contains the letter r sorry, no r in cat racoon contains the letter r sorry, no r in duck
target_letter = "r"
for animal in ["horse", "cat", "racoon", "duck"]:
if target_letter in animal:
print(animal + " contains the letter " + target_letter)
else:
print("sorry, no " + target_letter + " in " + animal)
horse contains the letter r sorry, no r in cat racoon contains the letter r sorry, no r in duck
animal = "moose"
animal.upper()
'MOOSE'
class Animal(object):
def eat(self, food):
return "What yummy " + food + "!"
fido = Animal()
fido.eat('dog food')
'What yummy dog food!'
class Cat(Animal):
def purr(self):
return 'rrrrrrrrrrrrrrrr'
agamemnon = Cat()
agamemnon.eat('hotdish')
'What yummy hotdish!'
agamemnon.purr()
'rrrrrrrrrrrrrrrr'
fido.purr()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-6-32f565adf250> in <module>() ----> 1 fido.purr() AttributeError: 'Animal' object has no attribute 'purr'
help("x".upper)
Help on built-in function upper: upper(...) S.upper() -> str Return a copy of S converted to uppercase.
dir("x")
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
hotdish_ingredients = ["noodles", "tuna", "mayo", "celery"]
shopping_list = ["apple", "bread", hotdish_ingredients, "spinach"]
for buy_me in shopping_list:
print(buy_me)
apple bread ['noodles', 'tuna', 'mayo', 'celery'] spinach
german = {"cat": "Katze", dog: "Hund"}
german["cat"]
'Katze'
name = "American of Personal Computer User Groups"
initials = [letter for letter in name if letter.isupper()]
print(initials)
abbrev = "".join(initials)
print(abbrev)
['A', 'P', 'C', 'U', 'G'] APCUG
csv
, json
, xml.*
...zlib
, zipfile
, tarfile
, bz2
...dbm
, shelve
, pickle
...ftplib
, urllib2
, smtp
, SimpleHTTPServer
...hashlib
cmd
, curses
...34,000 + freely available modules
import requests
response = requests.get('http://dma1.org')
response.status_code
200
response.content[:100]
b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n<HTML>\n<HEAD>\n <LINK REL="SHORTCUT '
import re
re.search(">(APCUG.*?)<", str(response.content)).group(1)
'APCUG 2013 Regional Computer Conference'