How tuples work | Python

Swap of values often seen in Python

Some of the most common value swapping methods are: For example C / C ++

swapSample.cpp


#include <iostream>
using namespace std;
int main(void){
//Initial setting
int x = 10;
int y = 25;
int tmp = 0;

cout << "x:"<< x << ", " << "y:"<< y << '\n';

//Exchange process here
tmp = x;
x = y;
y = tmp;

cout << "x:"<< x << ", " << "y:"<< y << '\n';
return 0;
}
//Output result:
// x:10, y:25
// x:25, y:10

In Python, this kind of swap processing is done as follows.

swapPython.py


x, y = 10, 25
print("x:{0}, y:{1}".format(x,y))
x,y = y, x #This alone swap!
print("x:{0}, y:{1}".format(x,y))
#Output result:
# x:10, y:25
# x:25, y:10

I often use this method conveniently, but this is what Python has. ** Tuple ** It is a function called.

This is different from the list t = (1, 2, 3, 4, 5) It is expressed as. Then >>> t [ENTER] > (1,2,3,4,5) >>> The value is stored as follows. There are various ways to use it, but this time about swap.

Tuple unpacking

The parentheses () can be omitted for tuples. The following expressions

swapPython.py


x, y = y, x #This alone swap!

Actually

swapPython.py


(x, y) = (y, x) #This alone swap!

This means that the tuple processing has been executed.

Summary

It was a little thing The process I'm always doing actually meant something like this You often notice that.

Recommended Posts

How tuples work | Python
How to work with BigQuery in Python
How to clear tuples in a list (Python)
[Introduction to Udemy Python3 + Application] 23. How to use tuples
How to install Python
Python3 | Lists, Tuples, Dictionaries
How to install python
Python> Tuples versus Lists
[2020.8 latest] How to install Python
How Kaggle ranking points work
[Python] Why pserve doesn't work
How Python module import works
python3: How to use bottle (2)
How Python __dict__ is used
[Python] How to use list 1
How to update Python Tkinter to 8.6
Python Basic Course (5 List Tuples)
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
Python list and tuples and commas
How to change Python version
Search engine work with python
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use machine learning for work? 03_Python coding procedure
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
Python
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
5 Easy-to-Use Python Tools | Increase Work Efficiency
How to get the Python version
How to get started with Python
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
[Python] How to swap array values
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to speed up Python calculations
How to calculate date with python
How to access wikipedia from python
How to use python zip function