-Memo # 1 for Python beginners to read "Detailed explanation of Python grammar" -Memo # 2 for Python beginners to read "Detailed Explanation of Python Grammar"
It's short today, but I'll do it little by little. Sutra-copying main and crisp.
Cumulative assignment statement
>>> spam = 10
>>> spam += 100
>>> spam
110
>>> spam -= 60
>>> spam
50
>>> spam *= 10
>>> spam
500
>>> spam /= 2
>>> spam
250.0
>>> spam //= 3
>>> spam
83.0
>>> spam %= 10
>>> spam
3.0
>>> spam **=4
>>> spam
81.0
#Bit operation
>>> spam = 1024
>>> spam >>= 2
>>> spam
256
>>> spam <<= 2
>>> spam
1024
>>> spam = 0b11110000
>>> spam
240
>>> spam &= 0b00001111 #Bit AND
>>> spam
0
>>> spam = 0b11110000
>>> spam ^= 0b00111100 #Bit exclusive OR
>>> spam
204 # 0b11001100
>>> spam = 0b11110000
>>> spam |= 0b00001111 #Bit OR
>>> spam
255 # 0b11111111
Variable names have a high degree of freedom
#You can do this (though I don't want to do it separately)
>>>spam= 10
>>>def ham(egg):
...return egg* 3
...
>>>Ham(spam)
30
#There is also this
>>> π = 3.14
>>> V = A * Ω
#Case sensitive
>>> spam = 10
>>> Spam = 20
>>> spam
10
>>> Spam
20
#The following variable names are all valid
>>> _spam = 1
>>>spam= 1
>>>egg= 1
>>> Spam = 1
#The following variable names are all invalid
>>> $pam = 1 #Symbol beginning
>>>1 Spam= 1 #Numerical beginning
>>> 1spam = 1 #Full-width numerical value start
>>>spam.= 1 #Punctuation is NG
#You can also judge whether it can be used as a variable name
>>> 'Spam'.isidentifier()
True
>>> '♪'.isidentifier()
False
#However, since the name is normalized to the Unicode standard normalization format KC, this function name is also the same.
#In short you shouldn't do it
>>>Spam= 'Half size'
>>>spam= 'Full-width'
>>> print(Spam)
Full-width
>>> print(spam)
Full-width
or
#or returns a "true" value
>>> 1 or 0
1
>>> 0 or 2
2
>>> 0 or 0.0
0.0 #If both are "false", the value on the right side is returned.
>>> 1 or 2
1 #If both are "true", the value on the left side is returned.
#At this time, "2" has not even been evaluated (this evaluation omission is called "short circuit").
#Practical example
>>> if x: #This if statement
>>> z = x
>>> else:
>>> z = y
>>> z = x or y #Can be written like this
and
>>> 1 and 2
2 #If both are "true", the value on the right side is returned.
>>> 0 and 1
0 #If either is "false", it returns a "false" value (again, 1 is a short circuit)
>>> 1 and 0.0
0.0 #Returns a "false" value if either is "false"
not
#not returns a bool type value
>>> not 1
False
>>> not 0
True
Recommended Posts