Skip to content

Instantly share code, notes, and snippets.

@duan-li
Last active January 4, 2017 04:02
Show Gist options
  • Save duan-li/77f0179fc4ecd6e3faa7a5d0625700d8 to your computer and use it in GitHub Desktop.
Save duan-li/77f0179fc4ecd6e3faa7a5d0625700d8 to your computer and use it in GitHub Desktop.
Automate the Boring Stuff with Python
# Exception Handling
def spam(divideBy):
return 42 / divideBy
try:
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
except ZeroDivisionError:
print('Error: Invalid argument.')
# Random Number
import random
secretNumber = random.randint(1, 20)
# The in and not in Operators for List
spam = ['hello', 'hi', 'howdy', 'heyas']
'cat' in spam
'howdy' not in spam
# The Multiple Assignment Trick
cat = ['fat', 'orange', 'loud']
size, color, disposition = cat
# Raw Strings
print(r'That is Carol\'s cat.')
# Multiline Strings with Triple Quotes
print('''Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob''')
# The in and not in Operators with Strings
'Hello' in 'Hello World'
# The isX String Methods
'hello'.isalpha()
'hello123'.isalpha()
'hello123'.isalnum()
'hello'.isalnum()
'123'.isdecimal()
' '.isspace()
'This Is Title Case'.istitle()
# The startswith() and endswith() String Methods
'Hello world!'.startswith('Hello')
'Hello world!'.endswith('world!')
# The join() and split() String Methods
>>> ', '.join(['cats', 'rats', 'bats'])
'cats, rats, bats'
>>> 'My name is Simon'.split()
['My', 'name', 'is', 'Simon']
# Install
# >sudo apt-get install xsel xclip
# >pip3 install gtk
# >pip3 install PyQt4
import pyperclip
pyperclip.copy('Hello world!')
pyperclip.paste()
# Grouping with Parentheses
import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My number is 415-555-4242.')
mo.group(1)
# with the Pipe
>>> heroRegex = re.compile (r'Batman|Tina Fey')
>>> mo1 = heroRegex.search('Batman and Tina Fey.')
>>> mo1.group()
'Batman'
>>> mo2 = heroRegex.search('Tina Fey and Batman.')
>>> mo2.group()
'Tina Fey'
# Matching Zero or More
>>> batRegex = re.compile(r'Bat(wo)*man')
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
# Matching One or More
>>> batRegex = re.compile(r'Bat(wo)+man')
>>> mo2 = batRegex.search('The Adventures of Batwowowowoman')
>>> mo2.group()
'Batwowowowoman'
# The findall() Method
>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
>>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
['415-555-9999', '212-555-0000']
# The Caret and Dollar Sign Characters (Beging and End)
>>> beginsWithHello = re.compile(r'^Hello')
>>> beginsWithHello.search('Hello world!')
<_sre.SRE_Match object; span=(0, 5), match='Hello'>
>>> beginsWithHello.search('He said hello.') == None
True
# The Wildcard Character
>>> atRegex = re.compile(r'.at')
>>> atRegex.findall('The cat in the hat sat on the flat mat.')
['cat', 'hat', 'sat', 'lat', 'mat']
# Matching Everything with Dot-Star
>>> nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)')
>>> mo = nameRegex.search('First Name: Al Last Name: Sweigart')
>>> mo.group(1)
'Al'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment