[Python / PyQ] 4. list, for statement

Introduction

I am studying by personally using a service of Bee Proud called "PyQ" to strengthen Python. This can be programmed with a browser without creating an environment. Also, one of the features is that you can learn the technique by copying the basic grammar. In addition to grammar for beginners, there are also unit tests and web courses using Django that you might use in practice.

I will summarize not only the sutras but also the grammar learned in PyQ to output.

list The list is created as follows.

test = ['hoge','fuga','hege']

Element output

The value set in list is called an element. Elements start at 0.

test = ['hoge','fuga','hege']

print(test[0]) #hoge is output
print(test[1]) #fuga is output
print(test[2]) #hege is output

Add elements to list

If you want to add an element to the end, use append.

test = ['hoge','fuga','hege']
test.append('test')

print(test[3]) #test is output

If you want to add it in the middle, use insert.

test = ['hoge','fuga','hege']
test.insert(1,'test')

print(test[1]) #test is output

Remove element from list

Use pop () to remove an element from the end. If you want to delete an element in the middle, use pop (index).

test = ['hoge','fuga','hege']
test.pop()

for item in test:
    print(item) #hoge and fuga are output

Change the elements of list

Specify the element you want to change and enter it.

test = ['hoge','fuga','hege']
test(0) = 'test'

print(test[0]) #test is output

for statement

The structure of the for statement that combines list is as follows.

for variable in list:
    print(variable)

The values entered in list are output sequentially.

test = [1,2,3]

for item in test:
    print(item)
#The output looks like this
# 1
# 2
# 3

next time

Next time, I will output what I learned about handling dictionary-type data.

Recommended Posts

[Python / PyQ] 4. list, for statement
Python list, for statement, dictionary
Python basics ② for statement
[Python] for statement error
Python #list for super beginners
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for statement ~ What is iterable ~
[Python] Multiplication table using for statement
2016-10-30 else for Python3> for:
Python if statement
python [for myself]
For Else statement
Python exec statement
Python> Comprehension / Comprehension> List comprehension
[Python] if statement
PyQ ~ Python Beginner ~
Python assert statement
Python list manipulation
[Introduction to Udemy Python3 + Application] 43. for else statement
Don't use readlines () in your Python for statement!
Sorted list in Python
About Python for loops
Python Exercise 2 --List Comprehension
List of python modules
Python> list> extend () or + =
PyQ ~ Python First Steps ~
Python list comprehension speed
python unittest assertXXX list
About Python, for ~ (range)
python textbook for beginners
Python3 List / dictionary memo
Refactoring tools for Python
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
python for android Toolchain
Python exception class list
OpenCV for Python beginners
Python basic if statement
Install Python (for Windows)
Python environment for projects
Initialize list with python
List of Python libraries for data scientists and data engineers
List of sample program distribution sites for python books
Python: Get a list of methods for an object
A Java programmer studied Python. (for, if, while statement)
[Python] I want to use only index when looping a list with a for statement
Python hand play (two-dimensional list)
Python memo (for myself): Array
About Fabric's support for Python 3
Summary of Python3 list operations
Python for Data Analysis Chapter 4
[Python] Convert list to Pandas [Pandas]
Modern Python for intermediate users
Python 3.6 installation procedure [for Windows]
Search list for duplicate elements
List method argument information for classes and modules in Python
BigQuery integration for Python users
[Python] How to use list 1
Set Up for Mac (Python)
Create ToDo List [Python Django]