Essential Python Functions and Sequence Operations
Common Python Functions
Mathematical Functions
pow(x, y) computes x raised to the power y.
abs(val) returns the absolute value of val.
round(num) returns the nearest integer to num.
sqrt(n) calculates the square root of n.
input(prompt) reads a line from terminal input.
max(seq) returns the largest item in a sequence.
min(seq) returns the smallest item in a sequence.
len(seq) returns the number of items in a sequence.
Python Sequences
Indexing
Access the first element with seq[0]:
>>> greeting = 'hi,today is Sunday!'
>>> greeting[0]
'h'
Access the last element with seq[-1]:
>>> greeting[-1]
'!'
Access the second-to-last element with seq[-2]:
>>> greeting[-2]
'y'
Slicing
seq[start:end] extracts elements where start ≤ index < end:
>>> items = ['a','b','c','1','2','3']
>>> items[2:4]
['c', '1']
Step Value
The default step is 1; specify a different step as the third parameter:
>>> digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> digits[0:9:1]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> digits[0:9:2]
[0, 2, 4, 6, 8]
>>> digits[0:9:4]
[0, 4, 8]
Sequence Concatenation
Combine two sequences with +:
>>> list_a = [0,2,4]
>>> list_b = [1,3,5]
>>> list_a + list_b
[0, 2, 4, 1, 3, 5]
Sequence Repetition
Repeat a sequence with *:
>>> list_a = [0, 2, 4]
>>> list_a * 3
[0, 2, 4, 0, 2, 4, 0, 2, 4]
Element Deletion
Remove elements using del:
>>> values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del values[0]
>>> values
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del values[0:3]
>>> values
[4, 5, 6, 7, 8, 9]
List Methods
count
Count occurrences of an element:
>>> chars = ['a', 'b', 'c', 'ac', 'bc', 'cd']
>>> chars.count('a')
1
>>> chars.count('ac')
1
append
Add an element to the end:
>>> chars.append('end')
>>> chars
['a', 'b', 'c', 'ac', 'bc', 'cd', 'end']
extend
Append multiple elements from another list:
>>> extra_list = ['extra']
>>> chars.extend(extra_list)
>>> chars
['a', 'b', 'c', 'ac', 'bc', 'cd', 'end', 'extra']
insert
Insert an element at a specific position:
>>> chars.insert(0, 'first')
>>> chars
['first', 'a', 'b', 'c', 'ac', 'bc', 'cd', 'end', 'extra']
pop
Remove and return the last element:
>>> chars.pop()
'extra'
>>> chars
['first', 'a', 'b', 'c', 'ac', 'bc', 'cd', 'end']
remove
Remove the first occurrence of a value:
>>> chars.remove('a')
>>> chars
['first', 'b', 'c', 'ac', 'bc', 'cd', 'end']
reverse
Reverse the list in place:
>>> chars.reverse()
>>> chars
['end', 'cd', 'bc', 'ac', 'c', 'b', 'first']
sort
Sort the list in ascending order:
>>> nums = [1, 5, 9, 2, 6, 8, 0]
>>> nums.sort()
>>> nums
[0, 1, 2, 5, 6, 8, 9]
Strings
String Formatting
Format specifiers control output alignment and precisino:
%starts the format specifier-left-aligns the output+shows the sign for numbers- Width before
.sets minimum field width - Precision after
.sets decimal places
Examples:
pi = 3.141592653589793
print("number is %-10f" % pi)
print("number is %10f" % pi)
print("number is %+10f" % pi)
print("number is %010f" % pi)
print("number is %.3f" % pi)
Output:
number is 3.141593
number is 3.141593
number is +3.141593
number is 003.141593
number is 3.142
Common format codes:
%ddecimal integer%ooctal integer%xhexadecimal integer%ffloating-point number%sstring
Example:
print("%s and %s are all in [a~z]" % ('a', 'b'))
print("price is %d" % 10)
print("pi is %f" % 3.1415926)
print("number 100 equals %d / %o / %x " % (100, 100, 100))
String Methods
find
Locate a substring, returning the lowest index or -1:
>>> text = 'hello,my name is a'
>>> text.find('no')
-1
>>> text.find('name')
9
join
Combine sequence elements with a separator:
>>> letters = ['a', 'b', 'c', 'd']
>>> '+'.join(letters)
'a+b+c+d'
>>> ''.join(letters)
'abcd'
lower
Convert to lowercase:
>>> phrase = 'Hello, My Name Is John'
>>> phrase.lower()
'hello, my name is john'
replace
Replace occurrences of a substring:
>>> phrase.replace('e', 'xxx')
'Hxxxllo, My Namxxx Is John'
split
Divide a string into a list using a delimiter:
>>> phrase.split(' ')
['Hello,', 'My', 'Name', 'Is', 'John']
strip
Remove leading and trailing characters (whitespace by default):
>>> s = ' hi,abc '
>>> s.strip()
'hi,abc'
>>> t = 'hooooooh'
>>> t.strip('h')
'oooooo'