Memorandum of python beginners About inclusion notation

Make a note of what you thought was important in learning python.

Comprehension notation

List comprehension

number_list=[]
for number in range(1,6):
	number_list.append(number)

You can make a list of numbers from 1 to 5 with this code. However, you can also write like this

number_list=[number for number in range(1,6)]

This one is smarter

You can also add conditional expressions

a_list=[number for number in range(1,6) if number % 2 == 1 ]

You can make an odd list from 1 to 5 with this code Let's compare it with the writing style that does not use comprehension

a_list=[]
for number in range(1,6):
	if number % 2 ==1:
		a_list.append(number)

The comprehension is much more compact

Multiple loops can also use comprehensions

for i in range(1,4):
	for k in range(1,6):
    	print(i,k)

List comprehension ver

S =[[i,k] for i in range(2,4) for k in range(1,5)]
print(S)

Dictionary comprehensive notation

Dictionaries also have comprehensions. There was a comprehensive notation in the book Basically the same writing style as list comprehension.

di={key:key**2 for key in range(1,5)}

You can create a dictionary like this {1: 1, 2: 4, 3: 9, 4: 16}

Generator comprehension

Tuples have no inclusions

num=(i for i in range(1,4))

You can create a generator object like this. ** I don't know what the generator is now. ** **

Recommended Posts

Memorandum of python beginners About inclusion notation
About python beginner's memorandum function
Memorandum of beginners Python "isdigit" movement
About the ease of Python
Python basic course (10 inclusion notation)
About various encodings of Python 3
A memorandum about correlation [Python]
A memorandum about Python mock
About the features of Python
[Python] A memorandum of beautiful soup4
About the basics list of Python basics
Learn the basics of Python ① Beginners
Python memorandum
Python Memorandum 2
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
About building GUI using TKinter of Python
This and that of the inclusion notation.
[Python] Minutes of study meeting for beginners (7/15)
About the virtual environment of python version 3.7
A memorandum of understanding about django's QueryDict
Python Basic Memorandum Part 3-About Object Orientation-
Memo of troubles about coexistence of Python 2/3 system
[Python] Chapter 02-04 Basics of Python Program (About Comments)
Basic grammar of Python3 system (included notation)
A memorandum of python string deletion process
About python slices
About python comprehension
Introduction of Python
Python basics memorandum
Python pathlib memorandum
About Python tqdm.
About python yield
About python, class
A memorandum of calling Python from Common Lisp
Python hand play (one line notation of if)
A memorandum of extraction by python bs4 request
About python inheritance
Basics of Python ①
Basics of python ①
A note about the python version of python virtualenv
About python, range ()
Easy understanding of Python for & arrays (for super beginners)
Copy of python
About python decorators
A memorandum about the Python tesseract wrapper library
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Python memorandum [links]
Beginners practice Python
About python reference
About Python decorators
[Python] About multi-process
About the * (asterisk) argument of python (and itertools.starmap)
[Answer example (python3)] ABS (AtCoder Beginners Selection) of atcoder
Python beginner's note
About shallow and deep copies of Python / Ruby