Section 16: Advanced Python Objects and Data Structures

  Python Bootcamp 0 to Hero

< Section 15 | Section 17 >

Advanced Numbers

https://www.udemy.com/complete-python-bootcamp/learn/lecture/3650896?start=0#questions

Hexadecimal hex()

Values are returned as strings

hex(246)

0xf6

hex(256)

0x200

Binary bin()

Values are returned as strings

bin(1234)

0b10011010010

pow()

Looks like PHP to me…

2**3 == pow(2,3)

abs()

abs(-4)

4

round()

Rounds to 0 places by default

round(4.75,1)

4.8

round(4.75)

5

Advanced Strings

https://www.udemy.com/complete-python-bootcamp/learn/lecture/3512768#questions

Case, Count and Find

.capitalize()

Capitalizes the first character in the string.

s = "hello world. how are you today?"
s.capitalize()

Hello world. how are you today?

.upper()

Converts all characters to UPPER case

s.upper()

HELLO WORLD. HOW ARE YOU TODAY?

.lower()

Converts all characters to lower case

s.lower()

hello world. how are you today?

.title()

s.title()

Hello World. How Are You Today?

.isalnum()

Check if all chars are alpha-numeric

'Hello'.isalnum()

True

'Hello!'.isalnum()

False

.isalpha()

Check if all chars are alpha-numeric

'Hello'.isalnum()

True

'Hello123'.isalnum()

False

.islower()

'hello'.islower()

True

'Hello'.islower()

False

.isspace()

'\t \t'.isspace()

True

'Hello'.isspace()

False

.istitle()

"Hello World. How Are You Today?".istitle()

True

"Hello world. How Are You Today?".istitle()

False

.isupper()

'HELLO".isuppoer()

True

'HELLo".isuppoer()

False

.islower()

'hello.islower()

True

'HELLo".islower()

False

.endswith()

s = 'Hello'
s.endswidth('o')

True
(This is the same as)

s[-1]=='o'

True

.count()

Count recurrence of a character in the string

s.count('o')

5

.find()

Find the first occurrence of a character in the string

s.find('o')

4

Formatting

.center(count, char)

s = 'Hello world'
s.center(20,'.')

….Hello world…..

.expandtabs()

print("hello/thi")
"hello/thi".expandtabs()
hello    hi
hello    hi

Regular Expressions Methods

.split()

s = "hello world. how are you today?"
s.split()

[‘hello’, ‘world.’, ‘how’, ‘are’, ‘you’, ‘today?’]

s.split('o')

[‘hell’, ‘ w’, ‘rld. h’, ‘w are y’, ‘u t’, ‘day?’]

.partition()

Splits only at the first occurrence. Returns the first part, the separator, and the trailing end.

s = "hello world. how are you today?"
s.partition('o')

(‘hell’, ‘o’, ‘ world. how are you today?’)

s.partition('!')

(‘hello world. how are you today?’, ”, ”)

Advanced Sets

https://www.udemy.com/complete-python-bootcamp/learn/lecture/3650898#questions

Sets will not take any duplicate items

.add()

s = set()
s.add(1)
s.add(2)
s.add(1)
s

{1, 2}

.clear()

s.clear()
s

set()

.copy

s = {1, 2, 3}
sc = s.copy()
s.add(4)
sc

{1, 2, 3}

.difference()

s.difference(sc)

{4}

.difference_update()

s1 = {1,2,3}
s2={1,4,5}
s1.difference_update(s2)
s1

{2, 3}

.discard()

Removes an item from the set if it exists

s = {1, 2, 3, 4}
s.discard(2)
s

{1, 3, 4}

.intersection()

Returns the common elements in 2 or more sets

s1 = {1, 2, 3, 4}
s2 = {2, 3, 4, 5, 6}
s3 = {2, 4, 6, 8}
s1.intersection(s2, s3)

{2, 4}

.intersection_update

Updates the current set to equal the intersection of all sets tested.

s1 = {1, 2, 3, 4}
s2 = {2, 3, 4, 5, 6}
s3 = {2, 4, 6, 8}
s1.intersection_update(s2, s3)
s1

{2, 4}

.isdisjoint()

Returns True if there are no matching elements

s1 = {1, 2, 3, 4}
s2 = {5, 6, 7, 8}
s3 = {3, 4, 5, 6}
s1.isdisjoint(s2)

True

s1.isdisjoint(s3)

False

.issubset()

s1 = {1, 2}
s2 = {1, 2, 3, 4, 5}
s3 = {3, 4, 5, 6}
s1.issubset(s2)

True

s1.issubset(s3)

False

.issuperset()

s1 = {1, 2}
s2 = {1, 2, 3, 4, 5}
s3 = {3, 4, 5, 6}
s2.issuperset(s1)

True

s3.issuperset(s1)

False

.symmetric_difference()

Returns a set of all items that are unique to both sets.
(Opposite of .intersection())

s1 = {0, 1, 2}
s2 = {1, 2, 3, 4, 5}
s1.symmetric_difference(s2)

{0, 3, 4, 5}

.symmetric_difference()

Returns a set of all items that are unique to both sets

s1 = {0, 1, 2}
s2 = {1, 2, 3, 4, 5}
s1.symmetric_difference_update(s2)
s1

{0, 3, 4, 5}

.union()

Returns all elements in all sets

s1 = {1, 2}
s2 = {1, 2, 3, 4, 5}
s3 = {3, 4, 5, 6}
s1.union(s2, s3)

{1, 2, 3, 4, 5, 6}

.update()

Sets the current set = all elements in all sets

s1 = {1, 2}
s2 = {1, 2, 3, 4, 5}
s3 = {3, 4, 5, 6}
s1.update(s2, s3)
s1

{1, 2, 3, 4, 5, 6}

Advanced Dictionaries

https://www.udemy.com/complete-python-bootcamp/learn/lecture/3512774#questions

Dictionary Comprehension

Not common. Difficult to get keys that are not based on the values
{ key: value }

mydict = {x: x**2 for x in range(10)}
mydict

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

.keys()

Returns the keys
This can also be used without the function. Keys are returned by default

d = {'k1':1, 'k2':2}
for k in d:
    print( k)

k1
k2

.items()

Returns (key, value) tuples

d = {'k1':1, 'k2':2}
for k in d:
    print( k)

(‘k1’, 1)
(‘k2’, 2)

.values()

Returns the values

d = {'k1':1, 'k2':2}
for k in d.values():
    print( k)

1
2

Advanced Lists

https://www.udemy.com/complete-python-bootcamp/learn/lecture/3512772#questions

.append()

mylist = [1, 2, 3]
mylist.append(4)
mylist

[1, 2, 3, 4]

.count()

Returns the number of times an element occurs in a list

mylist = [1, 2, 2, 3, 2]
mylist.count(2)

3

.extend()

Almost the same as append, but appends a list instead of adding it as an item.
.append() example:

mylist = [1, 2, 3]
mylist.append([4, 5])
print(mylist)

[1, 2, 3, [4, 5]]
.extend() example:

mylist = [1, 2, 3]
mylist.extend([4, 5])
print(mylist)

[1, 2, 3, 4, 5]

.index()

Returns the first index of a value in the list
Note: returns an error if the search value is not in the list.

mylist = [4, 5, 5, 8, 9]
mylist.index(5)

1

.insert(index, object)

Inserts the object at the index provided.

mylist = [4, 5, 5, 8, 9]
mylist.insert(2,'hello')
mylist

[4, 5, ‘hello’, 5, 8, 9]
Note: if the index provided is > the last index, the value will be appended to the list.

mylist = [4, 5, 5, 8, 9]
mylist.insert(20,'hello')
mylist

[4, 5, 5, 8, 9, ‘hello’]

.pop()

Pops off and returns the list item in the list

mylist = [4, 5, 5, 8, 9]
print(mylist.pop())
mylist

9
[4, 5, 5, 8]

You can also pop a specific item from the list by it’s index.

mylist = [4, 5, 5, 8, 9]
print(mylist.pop(3))
mylist

8
[4, 5, 5, 9]

.remove()

Removes the first occurrance of a value in the list

mylist = [4, 5, 5, 8, 9, 8]
mylist.remove(8)
mylist

[4, 5, 5, 9, 8]

.reverse()

Reverses the order of the items in place (index values are changed!)

mylist = [4, 5, 5, 8, 9, 8]
mylist.reverse()
mylist

[8, 9, 8, 5, 5, 4]

.sort()

Sorts ascending the items in place. (Index values are changed!)

mylist = [8, 9, 8, 5, 5, 4]
mylist.sort()
mylist

[4, 5, 5, 8, 8, 9]

 

 

 

LEAVE A COMMENT