
Alrighty everyone, this week I’m covering some useful operators for Python!
Range: return a sequence of numbers from start to stop by step.
syntax:
range(start, stop, step)list(range(10))
//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]list(range(0, 11, 2))
//[0, 4, 6, 8, 10]
Enumerate: adds a counter to an iterable (ie a list, dictionary, etc.)
syntax:
enumerate(iterable, start)weekdays = {'Mon', 'Tue', 'Wed', 'Thurs', 'Fri'}enum_days = enumerate(weekdays)
list(enum_days)
//[(0, 'Thurs'), (1, 'Fri'), (2, 'Tue'), (3, 'Mon'), (4, 'Wed')]enum_days = enumerate(weekdays, 5)
list(enum_days)
//[(5, 'Thurs'), (6, 'Fri'), (7, 'Tue'), (8, 'Mon'), (9, 'Wed')]
Zip: zips together two or more lists; however it will only zip lists together up to the shortest list length
syntax:
zip(list1, list2, ...)mylist1 = (1, 2, 3)
mylist2 = ('a', 'b', 'c')list(zip(mylist1, mylist2))
//[(1, 'a'), (2, 'b'), (3, 'c')]mylist3 = ('z', 'y')
list(zip(mylist1, mylist2, mylist3))
//[(1, 'a', 'z'), (2, 'b', 'y')]
In: a quick way to check if a value is included
'x' in [1,2,3]
// False'x' in ['x', 'y', 'z']
//True'x' in 'xyz'
//True'x' in ['abc', 'xyz']
//False
Min/Max: returns the minimum or maximum value of an iterable
mylist = [10,20,30,40,100]min(mylist)
//10max(mylist)
//100
I hope these operators become truly useful for you in your coding journey!