Memo # 3 for Python beginners to read "Detailed Python Grammar"

Back number

-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

Memo # 4 for Python beginners to read "Detailed Python Grammar"
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Memo # 5 for Python beginners to read "Detailed Python Grammar"
Basic Python grammar for beginners
~ Tips for beginners to Python ③ ~
Memo to ask for KPI with python
Beginners read "Introduction to TensorFlow 2.0 for Experts"
[Python] Read images with OpenCV (for beginners)
The fastest way for beginners to master Python
For beginners to build an Anaconda environment. (Memo)
Python for super beginners Python for super beginners # Easy to get angry
python textbook for beginners
Try to calculate RPN in Python (for beginners)
Python basic grammar memo
Introduction to Programming (Python) TA Tendency for beginners
How to make Python faster for beginners [numpy]
[For beginners] How to study programming Private memo
OpenCV for Python beginners
How to convert Python # type for Python super beginners: str
[For beginners] How to study Python3 data analysis exam
[For beginners] Learn basic Python grammar for free in 5 hours!
Python # How to check type and type for super beginners
Python memo (for myself): Array
Learning flow for Python beginners
Python Basic Grammar Memo (Part 1)
Python code memo for yourself
Python3 environment construction (for beginners)
3 Reasons Beginners to Start Python
Python basic grammar (miscellaneous) Memo (3)
Python #function 2 for super beginners
Python basic grammar (miscellaneous) Memo (2)
100 Pandas knocks for Python beginners
[Python] Sample code for Python grammar
Python for super beginners Python #functions 1
Python #list for super beginners
Python basic grammar (miscellaneous) Memo (4)
Introduction to Python For, While
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
[For beginners] How to read Numerai's HP + Submit + Convenient links
[R] [Python] Memo to read multiple csv files in multiple zip files
How to learn TensorFlow for liberal arts and Python beginners
Tips for coding short and easy to read in Python
How to convert Python # type for Python super beginners: int, float
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)
Tips for Python beginners to use the Scikit-image example for themselves
[Python] Introduction to graph creation using coronavirus data [For beginners]
Python Exercise for Beginners # 2 [for Statement / While Statement]
Minimum grammar notes for writing Python
Python for super beginners Python # dictionary type 1 for super beginners
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Python #len function for super beginners
Beginners use Python for web scraping (1)
[Nanonets] How to post Memo [Python]
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners