Python commentary whose parents were killed literally

@ Raclett3's Python programming whose parents were killed by literals of strings, numbers and truth values I thought it was really interesting: raised_hands :, but at the same time I couldn't understand what was happening, so I looked it up. It's a commentary, or my own study.

I mainly write "small numbers" items. If you make a mistake, please throw Masakari.

0

int()  # 0

This is also as shown in @ Raclett3's link.

or return 0 if no arguments are given

It returns 0 if no arguments are specified. This is easy.

1

len([()])  # 1

This is because the list has one tuple for that element, so len just returns a length of 1. In other words, is it easy to understand what is happening in the example below?

len([(), ()])  # 2
len([1, 2, 3])  # 3

2

len(((),()))  # 2

It's all nasty and tuples, but all we're doing is tuples on the list outside of 1. It is easy to understand if you add a little space.

len( ( () , () ) )  # 2
len( ( () , () , () ) )  # 3

3

len(hex(not()))  # 3

Not () is the original article @ Raclett3's commentary is very easy to understand, so please use that. I will transform it little by little.

len(hex(not()))

# ↓ not() == True

len(hex(True))

# ↓ True == 1

len(hex(1))

# ↓ hex()Converts the argument to a hexadecimal string.

len('0x1')  #That is 3!!

4

len(str(not()))  # 4

This is easy if you can understand 3! The 4-character True is made into a str type, and the length is acquired by len.

len(str(not()))

# ↓ not() == True

len(str(True))

#↓ True is converted to a character string.

len('True')  # 4!!

5

len(str(not[()]))  # 5

This is the False version of 4.

len(str(not[()]))

# ↓ not[()] == False

len(str(False))

#↓ False is converted to a character string.

len('False')  # 5

6

len(str(complex(not(),int())))  # 6

I get a headache from here: scream_cat: complex takes a real part as the first argument and an imaginary part as the second argument, and returns a complex type complex number. I think it's easier to understand if you take a closer look.

len(str(complex(not(),int())))

# ↓ not()Is True, int()is 0

len(str(complex(True, 0)))

#↓ True is 1

len(str(complex(1, 0)))

#↓ Complex type complex number with real part 1 and imaginary part 1,(1+0j)Is returned.

len(str((1+0j)))  # len( str( (1+0j) ) )

# ↓ (1+0j)To str type.

len('(1+0j)')  # ()Since it is 6 characters including, 6 is returned.

7

len(str(bytes(len(((),)))))  # 7

((((; ゚ Д ゚)))) It trembles for a moment, but if you look closely, you can see that 1 is just a bytes type. For bytes type string conversion, there is also sample code in here as described in @ Raclett3's article.

len(str(bytes(len(((),)))))  #The contents of bytes are len( ( (), ) )

#↓ In other words, the length of the tuple with 1 element becomes the argument of bytes.

len(str(bytes(1)))

#↓ Make 1 a bytes type.

len(str(b'\\x00')  # \\Is an escape sequence, so one character.

# ↓ b'\\x00'To get its length as a string.

len("b'\\x00")  # 7!

8

len(str(((),())))  # 8

In the second half, there are many () and the appearance is complicated ...

len(str(((),())))  # len( str( ((),()) ) )

#↓ "Tuple with two empty tuples" is str()Arguments of.

len('((),())')  #A tuple with 2 elements is converted to a character string as it is, and a character string length of 8 is returned.

9

len(str(([int()],[])))  # 9
len(str(((int()),())))  #It's hard to see if all are tuples

It's finally the end. This is the same as 8: honeybee:

len(str(([int()],[])))

# ↓ int() == 0

len(str(([0],[])))

#↓ As with 8, the argument of str is([0],[])So...

len('([0],[])')  # 9!!!

It's been a long time, but was it in demand? These basic guys are a good opportunity to study because I don't usually read documents. Thanks to @ Raclett3.

Recommended Posts

Python commentary whose parents were killed literally
Today's python error: killed
Memorize Python commentary 5 --Lists
Memorize Python commentary 4 --Input
Memorize Python commentary 2 --Strings