Featured Post

Set up machine learning and deep learning on AWS

Here is the simple instructions to set up a EC2 instance to run machine learning and deep learning on AWS 1.  Run an EC2 instance from ...

Showing posts with label Python tips. Show all posts
Showing posts with label Python tips. Show all posts

Apr 25, 2020

Python methods cheat sheet

Python Tips and Tricks, You Haven't Already Seen, Part 2

Python list methods

append(item)
>>> lst = [1, 2, 2]
>>> lst.append(4)
[1, 2, 2, 4]
count(item)
>>> lst.count(2)
2
extend(list)
>>> lst.extend([5, 6])
[1, 2, 2, 4, 5, 6]
index(item)
>>> lst.index(5)
4
insert(position, item)
>>>lst.insert(1, 9)
[1, 9, 2, 2, 3, 5, 6]
sort()
>>> lst.sort()
[1, 2, 2, 3, 5, 6, 9]
pop(index)
>>> lst.pop()
9
reverse()
>>> lst.reverse()
[6, 5, 3, 2, 1, 1]

Python string methods
>>> s = 'Hello2Python'
count(sub, start, end)
>>> s.count('l', 1, 5)
2
find(sub, start, end)
>>> s.find('l', 0, 5)
2
index(sub, start, end)
>>> s.index('t', 0, 9)
8
isalnum()
>>> s.isalnum()
True


isalpha()
>>> s.isalpha()
False
isdigit()
>>> s.isdigit()
False
islower()
>>> s.islower()
False
isupper()
>>> s.isupper()
False
isspace()
>>> s.isspace()
False
join()
>>> s.join('___')
'_Hello2Python_Hello2Python_Hello2Python_'
lower()
>>> s.lower()
hello2python
partition(sep)
>>> s.partition('2')
('Hello', '2', 'Python')
split(sep)
>>> s.split('2')
['Hello',  'Python']
strip()
>>> s.strip('n')
'Hello2Pytho'
upper()
>>> s.upper()
'HELLO2PYTHON'