[Python] How to use list 1

Introduction

We are advancing Python learning according to Learning Roadmap for Python Beginners on Tommy's blog. This time, I will deal with the first half of [Introduction to Python] Summary of how to use list (Part 1).

Target person

--Those who are studying on Tomi-san's blog --For those who want to get an overview of how to use list

environment

Google Colaboratory

content of study

  1. How to make a list and how to refer to it Lists are ** sequenced and mutable **

  2. Tuple and range are immutable. You can enclose a tuple variable in list () to make it a new variable

  3. list can also be put in list

1

list_num = [1,2,3,4]  # 1,2,3,Make a list with 4
print(list_num)       # list_In num display

Execution result


[1, 2, 3, 4]

2 In addition to lists, sequence types include tuples, ranges, etc., but these are immutable and data cannot be added. Enclose the tuple variable in list () to make it a new variable (the value assigned at the time of declaration cannot be changed) Conversion of tuples to list type

tuple_samp = (1,2,3,4)
print(tuple_samp)
print(type(tuple_samp))  #Type confirmation

list_samp = list(tuple_samp)
print(list_samp)
print(type(list_samp))

Execution result


(1, 2, 3, 4)
<class 'tuple'>
[1, 2, 3, 4]
<class 'list'>

Same for range

range_samp = range(4)
print(range_samp)
print(type(range_samp))
list_samp = list(range_samp)
print(list_samp)
print(type(list_samp))

Execution result


range(0, 4)
<class 'range'>
[1, 2, 3, 4]
<class 'list'>

3

list_0 = [1,2,3]
list_1 = [4,5,6]
list_2 = [7,8,9]
list_two_dim = [list_0,list_1,list_2]  #Put the list in the list
print(list_two_dim)

Execution result


[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

reference

[Introduction to Python] Summary of how to use list (Part 1)

Recommended Posts

[Python] How to use list 1
[Python] How to use list 3 Added
How to use list []
Summary of how to use Python list
python3: How to use bottle (2)
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
[Algorithm x Python] How to use the list
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
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
How to use python zip function
[Python] How to use Typetalk API
How to use xml.etree.ElementTree
How to use Python-shell
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to install and use pandas_datareader [Python]
How to use Virtualenv
[Python] How to convert a 2D list to a 1D list
How to use numpy.vectorize
How to use pytest_report_header
[python] How to use __command__, function explanation
How to install python
How to use partial
How to use Bio.Phylo
How to use SymPy
[Python] How to use import sys sys.argv
How to use x-means
How to use WikiExtractor.py
How to use IPython
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
How to use virtualenv
[Python2.7] Summary of how to use unittest
How to use Matplotlib
How to use iptables
python: How to use locals () and globals ()
How to use numpy
How to use __slots__ in Python class
How to use TokyoTechFes2015
How to use venv