29
loading...
This website collects cookies to deliver better user experience
null
in JavaScript is None
in Python. There is no equivalent of undefined
in Python.String
is called str
in Python.Boolean
is called bool
in Python.else if
is called elif
in Python. 🤯else if
. It's a strange quirk, but you will adapt fast if you know to look out for it.person = {"name": "Sherlock", "address": "Baker Street"}
print(person["name"]) # This works
print(person.name) # This doesn't work
undefined
in JavaScript. This means that if you try to access an undefined property, you will get an exception.person = {"name": "John", "title": "Doctor"}
print(person["address"]) # Throws KeyError: 'address'
.get()
. .get()
is a method that returns the value of a key if it exists in a dictionary. If it can't be found .get()
returns None
. You can also give .get()
an optional default parameter that will be returned instead of None
if the key is undefined.person = {"name": "Mycroft", "occupation": "Government official"}
print(person.get('name')) # prints Mycroft
print(person.get('age')) # prints None
print(person.get('age', 35)) # prints 35
clues = ["chair", "safe", "saucer of milk"]
for clue in clues:
print(f"{clue} is a helpful clue")
def get_nemesis():
return ("James", "Moriarty", 1835) # Return a tuple
# User is a tuple here
nemesis = get_nemesis()
print(nemesis[0], nemesis[1]) # Prints James Moriarty
nemesis[0] = "John" # This throws an Exception
# You can destructure a tuple
(first_name, last_name, born) = get_nemesis()
# The parenthesis are optional when destructuring
first_name, last_name, born = get_nemesis()
.map()
, .sort()
, and .filter()
. Python has a couple of those array functions, but they are a bit ugly to use. Here is an example of doubling only even numbers from a list.const numbers = [1, 2, 3, 4]
const result = numbers
.filter(num => num % 2 == 0)
.map(num => num + num)
numbers = [1, 2, 3, 4]
result = map(
lambda num: num + num,
filter(lambda num: num % 2 == 0, numbers),
)
numbers = [1, 2, 3, 4]
result = [
num + num # new value (map)
for num in numbers # list to iterate
if num % 2 == 0 # filter
]
numbers = [1, 2, 3, 4]
result = {
num: num % 2 == 0
for num in numbers
}
# Results in {1: False, 2: True, 3: False, 4: True}
time
, datetime
, calendar
).