Dictionary Calculator

def operations(operator, x, y):
    return {
        'add': lambda: x+y,
        'sub': lambda: x-y,
        'mul': lambda: x*y,
        'div': lambda: x/y}.get(operator, lambda: 'Not a valid operation')()
operations("mul", 2,8)

What is the difference between sort() and sorted()?

Both sort() and sorted() can be used to sort a list. What is the difference between them and when would I want to use one versus the other?

Answer

The primary difference between the list sort() function and the sorted() function is that the sort() function will modify the list it is called on. The sorted() function will create a new list containing a sorted version of the list it is given. The sorted() function will not modify the list passed as a parameter. If you want to sort a list but still have the original unsorted version, then you would use the sorted() function. If maintaining the original order of the list is unimportant, then you can call the sort() function on the list.

A second important difference is that the sorted() function will return a list so you must assign the returned data to a new variable. The sort() function modifies the list in-place and has no return value.

The example below shows the difference in behavior between sort() and sorted(). After being passed to sorted(), the vegetables list remains unchanged. Once the sort() function is called on it, the list is updated.

vegetables = ['squash', 'pea', 'carrot', 'potato']

new_list = sorted(vegetables)
print(new_list)
# new_list = ['carrot', 'pea', 'potato', 'squash']

vegetables.sort()
print(vegetables)
# vegetables = ['carrot', 'pea', 'potato', 'squash']


The isX() Methods

isalpha() Returns True if the string consists only of letters and isn’t 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 newlines 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
>>> 'hello123'.isalpha()
False
>>> 'hello123'.isalnum()
True
>>> 'hello'.isalnum()
True
>>> '123'.isdecimal()
True
>>> '
'.isspace()
True
>>> 'This Is Title Case'.istitle()
True
>>> 'This Is Title Case 123'.istitle()
True
>>> 'This Is not Title Case'.istitle()
False
>>> 'This Is NOT Title Case Either'.istitle()
False

The startswith() and endswith() Methods

>>> 'Hello, world!'.startswith('Hello')
True
>>> 'Hello, world!'.endswith('world!')
True
>>> 'abc123'.startswith('abcdef')
False
>>> 'abc123'.endswith('12')
False
>>> 'Hello, world!'.startswith('Hello, world!')
True
>>> 'Hello, world!'.endswith('Hello, world!')
True

The join() and split() Methods

>>> ', '.join(['cats', 'rats', 'bats'])
'cats, rats, bats'
>>> ' '.join(['My', 'name', 'is', 'Simon'])
'My name is Simon'
>>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
'MyABCnameABCisABCSimon'

>>> 'MyABCnameABCisABCSimon'.split('ABC')
['My', 'name', 'is', 'Simon']
>>> 'My name is Simon'.split('m')
['My na', 'e is Si', 'on']