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

Back number

-Memo # 1 for Python beginners to read "Detailed explanation of Python grammar"

Chapter 3 Expressions and Data Types

--The bool type that represents the result of a logical operation is also a numeric type object. --Each Python data has its own type, but all are derived from the object type. --Any object is an instance of object type, and all types are objects of type type.

Built-in functions related to object attributes


>>> setattr(obj, 'ham', 100)  # obj.ham =Same as 100

>>> getattr(obj, 'ham', 999)  # try:
100                           #     return obj.ham
                              # except AttributeError:
                              #     return 999
                              #Same as (default value can be omitted)

>>> delattr(obj, 'ham')  # del obj.Same as ham
>>> hasattr(obj, 'ham')  #Returns True if attribute name exists in obj
False

None object

--Used to indicate "no value specified" --Something like NULL in C or null in Java --A singleton object of type NoneType (the only object of this type)

Singleton object check process



# ==Is is faster than is, and moreover==Operators can be implemented independently on the object to be compared (judgment result cannot be guaranteed)
#(I'm not sure "I can implement it independently with the object to be compared")
>>> spam = 1
>>> spam == None  #Not like this
False
>>> spam is None  #like this
False

Ellipsis object

--Singleton object of type ellipsis. -"ellipsis" = It seems to mean omission. (I didn't know so I looked it up) --Originally assumed to be used in third-party libraries that handle multidimensional arrays. ――The usage method and purpose of use have not been decided.

Example of using Ellipsis object


>>> ...  #From Python3 it is possible to write with this literal
Ellipsis

#Represents the omitted part of the sample code
def spam():
    ...

#It may be good to indicate that the value is not specified explicitly
ham = ...
egg = (1, 2, ...)

Function call

Various ways to pass arguments


spam(0, 1, 2, 3, 4)     #this is
args = [0, 1, 2, 3, 4]  #Store it in an iterable object
spam(*args)             #Can be written like this


spam(a = bacon, b = sausage)  #This is
kwargs = {
    "a":bacon,
    "b":sausage
}               #If you store it in a mapping object
spam(**kwargs)  #Can be written like this

--In Python, functions, methods, classes, etc. are just a kind of objects that are the same as integers and strings. --() is also just an operator, so function call = just "apply the () operator to an object" --The only difference between a function object and an integer object is __ whether it defines the behavior of the () operator or not __ --The object that defines the behavior of the () operator is called __callable object __ --One of the callable objects that creates an object of that type when a type object is also called

py:Because the method of the object is also a callable object...


>>> spam.ham(egg)      #ham method call on spam object

>>> method = spam.ham  #Get the ham attribute of the spam object
>>> method(egg)        #If you write like this, you can call the method (wow)

Variables and references

Assigning to a variable means making it possible to refer to an object by variable name.


>>> spam = [1, 2, 3, 4]  #Set the variable spam to reference a list object
>>> ham = spam           #When assigned to another variable ham, ham also references the same object
>>> ham
[1, 2, 3, 4]

>>> spam.append('five')  #When you add an element to a list object
>>> spam                 #Since both spam and ham refer to the same object,
[1, 2, 3, 4, 'five']
>>> ham                  #The displayed result is the same
[1, 2, 3, 4, 'five']

This is when a new object is created


>>> egg = bacon = 1
>>> egg = egg + 1   # egg +1 returns a new object
>>> egg
2
>>> bacon
1

Decomposition of elements

Substitution all at once


>>> spam = [1, 2, 3]
>>> first, second, third = spam
>>> first, second, third  #Decomposes and assigns elements of iterable objects
(1, 2, 3)

>>> (a, [b, c], d) = (1, (2, 3), 4)  #The left side is[]Or()Can be further disassembled by surrounding with
>>> a, b, c, d
(1, 2, 3, 4)

When there are more elements on the right side than variables on the left side


# *If there is a variable with, it will be assigned to the list object in a nice way
>>> spam, ham, *egg = 1, 2, 3, 4, 5
>>> spam, ham, egg
(1, 2, [3, 4, 5])

>>> spam, *ham, egg = 1, 2, 3, 4, 5
>>> spam, ham, egg
(1, [2, 3, 4], 5)

>>> *spam, ham, egg = 1, 2, 3, 4, 5
>>> spam, ham, egg
([1, 2, 3], 4, 5)

When the number of elements on the right side is smaller than the variable on the left side


# *Variable with is an empty list with 0 elements
>>> spam, ham, *egg = 1, 2
>>> spam, ham, egg
(1, 2, [])

Recommended Posts

Memo # 4 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
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
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
Python for super beginners Python # dictionary type 2 for super beginners
An introduction to Python for non-engineers
python beginners tried to find out
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy