[PYTHON] Sequence and mapping

I thought it was probably an important concept to get to know Python I would like to describe and share ^ ^ A description of the built-in object.

There is an idea.

Sequences literally represent serial numbers. The map is "photo-like" in my own words, To put it a little more, it refers to something that cannot be separated (it becomes meaningless if separated).

Python strings and lists are sequences.

In other words, you can do the following:

String 【result】 print u'Pillow soup' [2: 4] And others

However, it cannot be used in the following ways. × str = 'testing' str[0] = 'a' Because it is an immutable object.

list 【result】 print [1, 2, 3, 4, 5, 6][5] 6

Since it is a serial number, you can slice it in a sequence. Since this is a mutable object, you can perform the following operations. array = [1, 2, 3, 4, 5, 6] array[0] = 8 print array [8, 2, 3, 4, 5, 6]

for, The Python dictionary type is a map.

It is a key-value, a hash in Perl etc. I think it corresponds to the associative array of PHP.

heights = {"john":182, "mike":173, "kevinn":190, "George":181, "Randy":184} print heights["Randy"] 184

You can use it like this.

However, it cannot be taken out sequentially. × print heights["john":"kevinn"]

However, because it is variable, you can change or add elements. heights ["mike"] = 175 # 2cm stretched!

Today is this strange ^^

Recommended Posts

Sequence and mapping
Python 3 indexer and sequence unpacking (unpacking substitution)
[Personal memo] Python sequence type / mapping type
Escape sequence
Calculate Fibonacci sequence with generator and iterator
About _ and __
Zero-based code wars kata yield and Fibonacci sequence