Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Functions: Miscellaneous

Give Feedback

kenyonej on April 13, 2020

Hi, I heard you say don’t put [] at the end of the code, what is the purpose of brackets, or may i ask, when or when not to use brackets? I know they’re used to create a list, but what other scenarios do we need to use them? Sorry if i missed your explanation of this…

Darren Jones RP Team on April 14, 2020

Hi. Can you point me to the time that you’re referring to for some context? [] will typically be used to either create a list (whether in a comprehension or direct assignment, but also for indexing elements or slices. They’re often used to access dictionary items:

my_dict["one"] = "Ein"

but using the .get() method of the dictionary is generally a safer way to work.

kenyonej on April 16, 2020

Hi Darren,

I should’ve made note of the reference to which video at which time when you indicated it, but you are correct, you could’ve been simply referring to the context in which you used the []. I didn’t make note of it, so I’d at least have to go through the module again and listen. But, your explanation actually answers what I was asking. Thanks!!

Adam on May 3, 2020

it is very clear explanation.Thanks

eleven2 on May 18, 2020

at time 5:30 it was. What Darren meant was not to use pow() as a dict value, rather pow without (). print(eval(calc, {‘pow’:pow()})) would not allow to use the function itself.

jckichuk on July 4, 2020

Hi Darren, when checking if too strings are the same, why bother hashing them first? Does python not allow direct comparisons between the strings themselves? Could you not print(moby_1 == moby_2) ?

Üllar on Sept. 18, 2020

@jckichuk, Python does allow direct comparisons, but in his example the usage of a direct comparison on a book’s contents would not have made sense, I believe, when it comes to performance. It should be much quicker to hash and then compare whether they are identical.

pamizu2 on Oct. 25, 2020

Well then what’s the difference of using between id() and hash()?

Bartosz Zaczyński RP Team on Oct. 26, 2020

@pamizu2 It’s a question of identity versus value. You can have two distinct string objects at different memory addresses but having the same value, for example:

>>> a = "Hello world"
>>> b = "Hello world"
>>> id(a) == id(b)  # Same as "a is b"
>>> hash(a) == hash(b)  # Same as "a == b"

The id() tells you the object’s identity, which is guaranteed to be unique among simultaneously existing objects. In CPython, it’s the memory address of the object.

The hash() function, on the other hand, calculates a number based on the object’s value. In more complex data types, hashing may sometimes consider only a few attributes of the object to calculate that number.

Become a Member to join the conversation.