#πŸ”’ HI i was wondering what this was

45 messages Β· Page 1 of 1 (latest)

ocean scarab
#

in this code:
dmap = dict(0='Mon', 1='Tue', 2='Wed', 3='Thu',
4='Fri', 5='Sat', 6='Sun'
why does one type out dict i havent seen this yet

forest flameBOT
#

@ocean scarab

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.

ocean scarab
#

are they converting something to something

autumn forge
#

It's the same as {}

#

You can do x = {key:value}

#

Or

#

x = dict(key=value)

ocean scarab
#

sorry im new it might take me a while to aborb this @autumn forge

warm nova
ocean scarab
#

no

#

?

warm nova
#

he's wrong

#

that code seems erroneous

autumn forge
#

Is it not the same?

ocean scarab
#

Can you help me out πŸ™‚

warm nova
forest flameBOT
#

@warm nova :x: Your 3.12 eval job has completed with return code 1.

001 |   File "/home/main.py", line 1
002 |     print(dict(0='abc'))
003 |                ^^
004 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
autumn forge
#

Ah.

ocean scarab
#

give me one sec to absorb this πŸ˜„

warm nova
ocean scarab
#

Is a string literal a

#

Is int a literal too ?

#

Nvm I got it

#

Still reading tho

ocean scarab
warm nova
#

possibly

ocean scarab
#

Ty for all your help buddy πŸ₯Ή

ocean scarab
warm nova
unique jasper
#

But you can do this. dict([(0, "red"), (1, "blue)])

#

dict is the class form of the {} literal.

#

The literal form can be used when all items are known at dev time (or in a generator). dict() can be used to convert other objects to a dict.

#

it also accepts kwargs

ocean scarab
unique jasper
#

stylistic choice probably

#

when you have string keys, you don't need to wrap the keys in quotes when using dict(x=y)

#

vs {"x": y}

ocean scarab
#

Is a string key a data type

unique jasper
#

if you have more than 3 or 4 keys, you're saving space.

#

it's a str

#

!d str

forest flameBOT
#
str

class str(object='')``````py

class str(object=b'', encoding='utf-8', errors='strict')```
Return a [string](https://docs.python.org/3/library/stdtypes.html#textseq) version of *object*. If *object* is not provided, returns the empty string. Otherwise, the behavior of `str()` depends on whether *encoding* or *errors* is given, as follows.

If neither *encoding* nor *errors* is given, `str(object)` returns [`type(object).__str__(object)`](https://docs.python.org/3/reference/datamodel.html#object.__str__), which is the β€œinformal” or nicely printable string representation of *object*. For string objects, this is the string itself. If *object* does not have a [`__str__()`](https://docs.python.org/3/reference/datamodel.html#object.__str__) method, then [`str()`](https://docs.python.org/3/library/stdtypes.html#str) falls back to returning [`repr(object)`](https://docs.python.org/3/library/functions.html#repr).
unique jasper
#

or ""

forest flameBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.