How to use the hash function that returns a hash value, which is built in by default.
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
--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.
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'>
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
python
if __name__ == '__main__':
n = int(input())
integer_list = map(int, input().split())
t = tuple(integer_list)
print(hash(t))
▼ input () is executed twice
n = int(input())
integer_list = map(int, input().split())
Recommended Posts