#๐Ÿ”’ understanding some code

93 messages ยท Page 1 of 1 (latest)

red helm
#

print({}.get("key", "default"))

sudden boltBOT
#

@red helm

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

red helm
#

@rough perch

rough perch
#

hey

red helm
#

print({}.get("key", "default"))

rough perch
#

tell me what you wonder about

red helm
#

i dont understand code with two argument

rough perch
#

do you understand functions with one argument?

red helm
rough perch
#

you should show me an example you understand so i know

red helm
rough perch
#

no, an example with one, you understand

red helm
#

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

x = car.keys()

print(x)

i understand this

rough perch
#

explain what you understand here please

red helm
#

x=car.keys() grab "brand", "model", "year" , print just outputs those things

and also add dictkey before printing

rough perch
#

yes, true

#

so here, keys() is a function without any arguments

#

and you said you did not understand functions with two arguments

rough perch
#

do you understand functions with one argument?

#

alright, so lets look at what that is

rough perch
#

do you know how to write your own function?

rough perch
#

can you write me a short simple function?

red helm
rough perch
#

!e

def x(name):
    print(f"Hello {name}")
x("cope")
sudden boltBOT
rough perch
#

alright.. the name argument here is how you get something inside the function

#

do you agree about that?

rough perch
#

so what if you want two things? like name and age?

red helm
rough perch
#

try it

red helm
#
def x(name, age):
    print(f"Hello {name}, your age is {age}")
x("cope", 14)
rough perch
#

!e

def x(name, age):
    print(f"Hello {name}, your age is {age}")
x("cope", 14)
sudden boltBOT
rough perch
#

alright

#

so do you agree that it is you the developer of this code, that decides what you want to use your arguments for?

rough perch
#

i think so as well

#

so how would you tell people about your function, how would you let them know that they need to use two arguments?

#

name and age in your example is not hard to tell, but what when you have key and default?

#

you dont know the python guy that wrote the dict get function

red helm
rough perch
#

you can check if it does

#

!docs dict.get

sudden boltBOT
#

get(key, default=None, /)```
Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to `None`, so that this method never raises a [`KeyError`](https://docs.python.org/3/library/exceptions.html#KeyError).
red helm
#

no not key, the item in the key

rough perch
#

the get function returns the value of the key your looking for

rough perch
#

if it does not find one, you will get the default answer instead

#

the normal way to get a key gives you an error if the key is not there

red helm
#

what is the difference between .value() and .get()

rough perch
#

car['colour'] would give an error in your example

rough perch
red helm
rough perch
#

yes, and do you know what get does now?

red helm
rough perch
#

values gives you all the values in your dict

#

!docs dict.values

sudden boltBOT
#

values()```
Return a new view of the dictionaryโ€™s values. See the [documentation of view objects](https://docs.python.org/3/library/stdtypes.html#dict-views).

An equality comparison between one `dict.values()` view and another will always return `False`. This also applies when comparing `dict.values()` to itself...
rough perch
#

in your example, the values are

#

['Ford', 'Mustang', 1964]

red helm
#

oh so get only returns an indivudual value while value returns all the value in a dictionary

rough perch
#

yes, but mostly, get does not give you an erroor if the key is not there

red helm
#

instead of an error

rough perch
#

!e

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

print(car['colour'])
sudden boltBOT
rough perch
red helm
#

ok i understand the difference between get and value now

back to the problem

rough perch
#

!e

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

print(car.get('colour', 'black'))
sudden boltBOT
rough perch
rough perch
#

the default is black now because i set it to black

red helm
#

is it like that?

rough perch
red helm
true sable
#

basically yah

rough perch
#

yeah

#

a dict is a collection of key value pairs

#

and you use the key to get the value

red helm
#

oh

red helm
rough perch
#

my pleasure

true sable
#

no probs (defo did not see this rn and send one msg)

sudden boltBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.