Skip to content

Instantly share code, notes, and snippets.

@mizushou
Created September 20, 2019 17:01
Show Gist options
  • Save mizushou/fe9a68a70d4db3aa9f63a1c3db53a6c5 to your computer and use it in GitHub Desktop.
Save mizushou/fe9a68a70d4db3aa9f63a1c3db53a6c5 to your computer and use it in GitHub Desktop.
Python listの使い方
"""
list: A sequence of items (elements)
- A list is mutable
- A list is not array in Python.
- A list can contain a variety of types.
"""
# 1. create a list
fruit = []
print(fruit)
# >>> []
fruit = ["Apple", "Orange", "Pear"]
print(fruit)
# >>> ['Apple', 'Orange', 'Pear']
squares = [1, 4, 9, 16, 25, 36, 49]
print(squares)
# >>> [1, 4, 9, 16, 25, 36, 49]
chaos = [1, 2.5, "Cat", [1, 3], (5, "apple")]
# >>> [1, 2.5, 'Cat', [1, 3], (5, 'apple')]
# 2. list operations
animals = ["Eagle", "Snake", "Panda", "Lion", "Tiger", "Bear"]
# add
# 注意!!!! listに追加しても、新しいlistは返ってこない。これに気づかないで10分考えた。ここらへんにmutableらしさを感じる。
print(animals.append("Cat")) # add "Cat" item to the list
# >>> None
print(animals)
# >>> ['Eagle', 'Snake', 'Panda', 'Lion', 'Tiger', 'Bear', 'Cat']
animals.insert(0, "Monkey") # insert "Monkey" at 0 index
print(animals)
# >>> ['Monkey', 'Monkey', 'Eagle', 'Snake', 'Panda', 'Lion', 'Tiger', 'Bear', 'Cat']
# 注意!!!! insertも同じ。
print(animals.insert(0, "Dog"))
# >>> None
# delete
# count
# retrieve element or index
# sum
# sort
# search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment