#๐ understanding some code
93 messages ยท Page 1 of 1 (latest)
@red helm
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.
@rough perch
hey
print({}.get("key", "default"))
tell me what you wonder about
i dont understand code with two argument
do you understand functions with one argument?
example?
you should show me an example you understand so i know
.
no, an example with one, you understand
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x)
i understand this
explain what you understand here please
x=car.keys() grab "brand", "model", "year" , print just outputs those things
and also add dictkey before printing
yes, true
so here, keys() is a function without any arguments
and you said you did not understand functions with two arguments
do you understand functions with one argument?
alright, so lets look at what that is
do you know how to write your own function?
yes
can you write me a short simple function?
def x(name):
print(f"Hello {name}")
x("cope")
!e
def x(name):
print(f"Hello {name}")
x("cope")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
Hello cope
alright.. the name argument here is how you get something inside the function
do you agree about that?
yes
so what if you want two things? like name and age?
add one more argument
try it
def x(name, age):
print(f"Hello {name}, your age is {age}")
x("cope", 14)
!e
def x(name, age):
print(f"Hello {name}, your age is {age}")
x("cope", 14)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
Hello cope, your age is 14
alright
so do you agree that it is you the developer of this code, that decides what you want to use your arguments for?
i think so
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
doesnt ".get" searches for a key
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).
no not key, the item in the key
the get function returns the value of the key your looking for
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
what is the difference between .value() and .get()
car['colour'] would give an error in your example
do you know what .get does now?
whats the difference between .value() and .get()
yes, and do you know what get does now?
yes
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...
oh so get only returns an indivudual value while value returns all the value in a dictionary
yes, but mostly, get does not give you an erroor if the key is not there
it prints None
instead of an error
!e
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car['colour'])
:x: Your 3.13 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m7[0m, in [35m<module>[0m
003 | print([31mcar[0m[1;31m['colour'][0m)
004 | [31m~~~[0m[1;31m^^^^^^^^^^[0m
005 | [1;35mKeyError[0m: [35m'colour'[0m
correct, or your default value if you give it one
ok i understand the difference between get and value now
back to the problem
!e
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.get('colour', 'black'))
:white_check_mark: Your 3.13 eval job has completed with return code 0.
black
there is no "colour"
what is the problem? you said that you understand it
i know, thats why it prints the default
the default is black now because i set it to black
so .get() add the value if the value doesnt exist
is it like that?
it does not change the dict, it just gives you the default back
so it goes like
if you cannot find "colour", print the default
basically yah
yeah
a dict is a collection of key value pairs
and you use the key to get the value
oh
i understand now,
thanks for your time
my pleasure
no probs (defo did not see this rn and send one msg)
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.