[Python] How to use hash function and tuple.

[Python] How to use hash function and tuple

How to use the hash function that returns a hash value, which is built in by default.

Basic syntax

hash(tuple)

Arguments are ** tuple ** type numbers and variables. -The original value is returned for the integer. -List is an error.

tuple


hash((2,4,5))

#output
8794205387495702562

list is an error


hash([2,4,5])

#output
TypeError: unhashable type: 'list'

int returns the original number


hash(2)

#output
2

What is a tuple?

--A type that cannot add or remove elements. --Because it is iterable, it can be retrieved with a for statement. --Can be either a numerical value or a character string. -Enclosed in (). -* Non-iteral things cannot be tuples (int, float, etc.)

The appearance and properties are similar to list. The difference is that you cannot tamper with elements such as additions and deletions.

How to make tuples

There are some

--Multiple values -Enclose in () --Convert with tuple method: tuple ()

Multiple numbers


x=5,4.1

print(x)
print(type(x))

#output
(5, 4.1)
<class 'tuple'>

(Multiple numbers)


x=(5,4)

print(x)
print(type(x))

#output
(5, 4)
<class 'tuple'>

String


x="a","b"

print(x)
print(type(x))

#output
('a', 'b')
<class 'tuple'>

(String)


x="a"

print(x)
print(type(x))

#output
('a',)
<class 'tuple'>

tuple method


x=[1,2,3,4,5]
x=tuple(x)

print(x)
print(type(x))

#output
(1, 2, 3, 4, 5)
<class 'tuple'>

※important point

Non-iteral values such as int and float cannot be tuples.

int


x=(3)

print(x)
print(type(x))

#output
TypeError: 'int' object is not iterable

tuple(int)


x=3
x=tuple(x)

print(x)
print(type(x))

#output
TypeError: 'int' object is not iterable

## application A program that inputs two numbers and returns a hash value

python


if __name__ == '__main__':
    n = int(input())
    integer_list = map(int, input().split())
    t = tuple(integer_list)
    print(hash(t))

** ・ input (). split () ** Execute input () and then split ().

▼ input () is executed twice

n = int(input())
integer_list = map(int, input().split())

Recommended Posts

[Python] How to use hash function and tuple.
How to use python zip function
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
python: How to use locals () and globals ()
How to use Python zip and enumerate
How to use is and == in Python
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[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
[Python] How to use the enumerate function (extract the index number and element)
[Python] [Django] How to use ChoiceField and how to add options
Python: How to use async with
How to use the zip function
How to install and use Tesseract-OCR
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
How to use .bash_profile and .bashrc
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
[Python] How to use Typetalk API
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Summary of how to use pandas
How to package and distribute Python scripts
[Introduction to Python] How to use class in Python?
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
How to use __slots__ in Python class
How to use "deque" for Python data
[Python] How to calculate MAE and RMSE
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
[Blender x Python] How to use modifiers
How to use pandas Timestamp and date_range
[Question] How to use plot_surface of python
How to use functions in separate files Perl and Python versions
How to use Serverless Framework & Python environment variables and manage stages
[Python] Explains how to use the format function with an example
How to use Python with Jw_cad (Part 2 Command explanation and operation)
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
How to use xml.etree.ElementTree