ABS Functions list

Chapter 1 – Python Basics


str()

 int()

float()

len()

print() 

input()

Chapter 2 – Flow Control

True

False


Chapter 6 – Manipulating Strings

Double Quotes

>>> spam = "That is Alice's cat."

Escape Characters

>>> spam = 'Say hi to Bob\'s mother.'

Escape Characters.png
Escape Characters.png

Raw Strings

>>> print(r'That is Carol\'s cat.')
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''')

Multiline Comments

def spam():
    """This is a multiline comment to help
    explain what the spam() function does."""
    print('Hello!')

Indexing and Slicing Strings

>>> spam = 'Hello world!'

>>> spam[0:5]
'Hello'

The in and not in Operators with Strings

>>> 'Hello' in 'Hello World'
True

>>> 'HELLO' in 'Hello World'
False
>>> '' in 'spam'
True
>>> 'cats' not in 'cats and dogs'
False

Useful String Methods

The upper(), lower(), isupper(), and islower() String Methods

>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'
>>> spam = spam.lower()
>>> spam
'hello world!'

>>> spam.islower()
False
>>> spam.isupper()
False
>>> 'HELLO'.isupper()
True

>>> 'Hello'.upper().lower().upper()
'HELLO'

>>> 'HELLO'.lower().islower()
True

The isX String Methods

  • isalpha() returns True if the string consists only of letters and is not blank.

  • isalnum() returns True if the string consists only of letters and numbers and is not blank.

  • isdecimal() returns True if the string consists only of numeric characters and is not blank.

  • isspace() returns True if the string consists only of spaces, tabs, and new-lines and is not blank.

  • istitle() returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters.

>>> 'hello'.isalpha()
True

>>> 'This Is Title Case'.istitle()
True

Chapter 7 – Pattern Matching with Regular Expressions

>>> import re   # Regular expression module

re.compile(r'\d\d\d')

.search('My number is 415-555-4242.')
.group() tai (0), (1), (2)…

>>> mo = regex.search('000-11122')

regex = re.compile(r'((\d\d\d)-(\d\d\d))(\d\d)')

>>> mo.group(0)

'000-11122'

>>> mo.group(1)

'000-111'

>>> mo.group(2)

'000'

>>> mo.group(3)

'111'

>>> mo.group(4)

'22'

>>> mo.groups()

('000-111', '000', '111', '22')

>>> heroRegex = re.compile (r'Batman|Tina Fey')
>>> mo1 = heroRegex.search('Batman and Tina Fey.')
>>> mo1.group()
'Batman'

>>> batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
>>> mo = batRegex.search('Batmobile lost a wheel')
>>> mo.group()
'Batmobile'
>>> mo.group(1)
'Mobile'

Optional Matching with the Question Mark

>>> batRegex = re.compile(r'Bat(wo)?man')
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'

>>> mo2 = batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'

Matching Zero or More with the Star

>>> batRegex = re.compile(r'Bat(wo)*man')
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'

>>> mo2 = batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'

>>> mo3 = batRegex.search('The Adventures of Batwowowowoman')
>>> mo3.group()
'Batwowowowoman'

Matching One or More with the Plus

>>> batRegex = re.compile(r'Bat(wo)+man')
>>> mo1 = batRegex.search('The Adventures of Batwoman')
>>> mo1.group()
'Batwoman'

>>> mo2 = batRegex.search('The Adventures of Batwowowowoman')
>>> mo2.group()
'Batwowowowoman'

>>> mo3 = batRegex.search('The Adventures of Batman')
>>> mo3 == None
True

Matching Specific Repetitions with Curly Brackets

>>> haRegex = re.compile(r'(Ha){3}')
>>> mo1 = haRegex.search('HaHaHa')
>>> mo1.group()
'HaHaHa'

>>> mo2 = haRegex.search('Ha')
>>> mo2 == None
True

Greedy and Nongreedy Matching

Python’s regular expressions are greedy by default

>>> greedyHaRegex = re.compile(r'(Ha){3,5}')
>>> mo1 = greedyHaRegex.search('HaHaHaHaHa')
>>> mo1.group()
'HaHaHaHaHa'

>>> nongreedyHaRegex = re.compile(r'(Ha){3,5}?')
>>> mo2 = nongreedyHaRegex.search('HaHaHaHaHa')
>>> mo2.group()
'HaHaHa'

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']

Character Classes

Making Your Own Character Classes

>>> vowelRegex = re.compile(r'[aeiouAEIOU]')
>>> vowelRegex.findall('Robocop eats baby food. BABY FOOD.')
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']

>>> consonantRegex = re.compile(r'[^aeiouAEIOU]')
>>> consonantRegex.findall('Robocop eats baby food. BABY FOOD.')
['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
', 'B', 'B', 'Y', ' ', 'F', 'D', '.']

The Caret and Dollar Sign Characters

>>> 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

>>> endsWithNumber = re.compile(r'\d$')
>>> endsWithNumber.search('Your number is 42')
<_sre.SRE_Match object; span=(16, 17), match='2'>
>>> endsWithNumber.search('Your number is forty two.') == None
True

>>> wholeStringIsNum = re.compile(r'^\d+$')
>>> wholeStringIsNum.search('1234567890')
<_sre.SRE_Match object; span=(0, 10), match='1234567890'>
>>> wholeStringIsNum.search('12345xyz67890') == None
True
>>> wholeStringIsNum.search('12 34567890') == 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']

>>> nongreedyRegex = re.compile(r'<.*?>')
>>> mo = nongreedyRegex.search('<To serve man> for dinner.>')
>>> mo.group()
'<To serve man>'
>>> greedyRegex = re.compile(r'<.*>')
>>> mo = greedyRegex.search('<To serve man> for dinner.>')
>>> mo.group()
'<To serve man> for dinner.>'

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'
>>> mo.group(2)
'Sweigart'

Matching Newlines with the Dot Character

>>> noNewlineRegex = re.compile('.*')
>>> noNewlineRegex.search('Serve the public trust.\nProtect the innocent.
\nUphold the law.').group()
'Serve the public trust.'

>>> newlineRegex = re.compile('.*', re.DOTALL)
>>> newlineRegex.search('Serve the public trust.\nProtect the innocent.
\nUphold the law.').group()
'Serve the public trust.\nProtect the innocent.\nUphold the law.'

Substituting Strings with the sub() Method

>>> namesRegex = re.compile(r'Agent \w+')
>>> namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')
'CENSORED gave the secret documents to CENSORED.'

>>> agentNamesRegex = re.compile(r'Agent (\w)\w*')
>>> agentNamesRegex.sub(r'\1****', 'Agent Alice told Agent Carol that Agent
Eve knew Agent Bob was a double agent.')
A**** told C**** that E**** knew B**** was a double agent.'

re.compile() additionals arguments: re.compile(r’regex’, re.VERBOSE | re.IGNORECASE | re.DOTALL)

VERBOSE = enable comments in regex

IGNORECASE = ignore capitalization

DOTALL = makes . character match even newlines

Chapter 8 – Reading and Writing Files

os.path.join()

os.getcwd()

os.chdir()

os.makedirs()

os.path module

os.path.abspath(path)

os.path.isabs(path)

os.path.relpath(path, start)

os.path.dirname(path)

os.path.basename(path)

os.path.split()

>>> calcFilePath.split(os.path.sep)
['C:', 'Windows', 'System32', 'calc.exe']

os.path.getsize(path)

os.listdir(path)

os.path.exists(path)

os.path.isfile(path)

os.path.isdir(path)

open(path, argument) ‘r’ for read mode though it is the standard, ‘w’ write, ‘a’ for append

open().read()

write()

close() 

open().readlines()

open().write()

import shelve

type()

random.shuffle()

‘ ‘.format()

aList.index(‘item’) produces the number of the item

Sys.argv[0] (on Windows, other systems no [0]

Chapter 9 – Organizing Files

import shutil

shutil.copy(source, destination)

shutil.copytree(source, destination)

shutil.move(source, destination)

os.unlink(path)

os.rmdir(path)

shutil.rmtree(path)

import send2trash

send2trash.send2trash()

os.walk()

for folderName, subfolders, filenames in os.walk('C:\\delicious')

import zipfile

ZipFile()

getinfo()

namelist()

.file_size

.compress_size

extractall()

extract()

Leave a Comment

Your email address will not be published. Required fields are marked *

/* add by OCEANUS */