Memo # 7 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" -Memo # 3 for Python beginners to read "Detailed explanation of Python grammar" -Memo # 4 for Python beginners to read "Detailed explanation of Python grammar" -Note that a Python beginner read "Detailed explanation of Python grammar" # 5 -Note that Python beginners read "Detailed explanation of Python grammar" # 6

Tuple

--Any object can be freely registered --Similar to a list, but cannot be updated (elements cannot be added / deleted after creation) --Elements are references to other objects -Of course, since it is a sequence object, it is possible to extract elements by subscripting or slicing.

Tuple


#The basic writing method is the same as the list.
t = (1, 2, 3)
t = ()
t = (1, 'Two', [3, 4])

#Tuple literal()Is optional (but required for empty tuples)
>>> t = (1, 2, 3)  #this is
>>> t
(1, 2, 3)
>>> t = None
>>> t
>>> t = 1, 2, 3  #Can be written like this
>>> t
(1, 2, 3)

>>> t = 1,  #Tuple consisting of one integer value
>>> t
(1,)

Swapping two variable values using tuples


#In C and Java, variables for temporary storage are required, but in Python it can be done like this
>>> a = 1
>>> b = 2
>>> b, a = a, b   # b, a = (a, b)
>>> a
2
>>> b
1

Tuple method


#Get the number of elements equal to the argument
>>> t = (1, 2, 1)
>>> t.count(1)
2

#Finds an element with a value equal to the argument and returns the index of the first matching element
>>> A = (3, 4, 5, 3, 4, 5)
>>> A.index(4)
1

Positioning and proper use of lists and tuples

--Non-updatable sequences (ie tuples) have the advantage that they can be used as dictionary keys or set elements. --The elements of the list are independent data (for example, "list of user names" and "list of sales amount"). It is common to create it as a fixed list of data. (If it is an RDB, is it an image to put a column?) --Tuples are used when the tuple as a whole has meaning as data (elements alone do not make sense). (If it is an RDB, is it an image to insert a line?) ――So, tuples are used more like structures than arrays. --Tuples are more memory efficient than lists because they are fixed-length sequences. Based on the above policy, it is not uncommon for the amount of data that you want to keep in tuples to be large due to its nature, so you can expect efficient resource use and reduction of garbage collection load by properly using tuples instead of lists. ..

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"
~ 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)
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
[For beginners] How to use say command in python!
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
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 #function 2 for super beginners
Python basic grammar (miscellaneous) Memo (2)
100 Pandas knocks for Python beginners
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]
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 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
[Pandas] I tried to analyze sales data with Python [For beginners]