投稿

Pythonのおもな組み込み型の分類表

イメージ
Pythonのおもな組み込み型の分類表 変更不可(イミュータブル、immutable) 値を変更するには、箱ごと変えなければいけない(中身を変えられない箱) 変更可(ミュータブル、mutable) 箱はそのままで、中身の値だけを変えることができる(中身を変えられる箱) 反復可(イテラブル、iterable) 要素を1つずつ返すことができるオブジェクト」です。 わかりやすく言うと、for文のinのうしろに置いて、 1つずつデータを取り出せるオブジェクトです。 シーケンス(sequence) 整数のインデックスを指定して、要素にアクセスできる」データ型です。 組み込み関数len()で要素数(長さ)を取得できます。 文字列(str)もシーケンスです。 マッピング(mapping) 任意のキーで要素を検索できる」データ型です。 シーケンスが整数のインデックスを指定して要素にアクセスするのに対して、 マッピングは任意のキーを使用できます。 辞書(dict)が代表的なマッピング型です。

help("yield")

>>> help("yield") The "yield" statement ********************* yield_stmt ::= yield_expression A "yield" statement is semantically equivalent to a yield expression. The yield statement can be used to omit the parentheses that would otherwise be required in the equivalent yield expression statement. For example, the yield statements yield yield from are equivalent to the yield expression statements (yield ) (yield from ) Yield expressions and statements are only used when defining a *generator* function, and are only used in the body of the generator function. Using yield in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. For full details of "yield" semantics, refer to the Yield expressions section. >>>

yield sample progaram

def myfunc(): yield 'one' yield 'two' yield 'three' yield 12345 print("test myfunc") for s in myfunc(): print(s) print("test myfunc by next") gene = myfunc() print(next(gene)) print(next(gene)) print(next(gene)) def myfunc2(x:int): # 0からxまでの値を2倍して返す for z in range(x): yield z*2 print("test myfunc2") for i in myfunc2(5): print(i) ===== RESTART: C:/python/yieldTest20210224.py === test myfunc one two three 12345 test myfunc by next one two three test myfunc2 0 2 4 6 8 >>>

Why do we use yield in Python?

We should use yield when we want to iterate over a sequence but don't want to store the entire sequence in memory. yield is used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return.

generate cubes of numbers

#solution : generate cubes of numbers def cubicvar(): i=1 while True: yield i*i*i i +=1 print("cubes of number") for num in cubicvar(): if num> 1000: break print(num) ======= RESTART: C:/python/generateCubesOfNUmber.py ==== cubes of number 1 8 27 64 125 216 343 512 729 1000 >>>

calculate BMI by numpy array

Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with, and give users the opportunity to perform calculations across entire arrays. In the following example, you will first create two Python lists. Then, you will import the numpy package and create numpy arrays out of the newly created lists. # Create 2 new lists height and weight height = [1.6, 1.87, 1.87, 1.82, 1.91] weight = [55.2,81.65, 97.52, 95.25, 92.98] print("height") print(height) print("weight") print(weight) # Import the numpy package as np import numpy as np # Create 2 numpy arrays from height and weight np_height = np.array(height) np_weight = np.array(weight) print("type") print(type(np_height)) # Calculate bmi bmi = np_weight / np_height ** 2 # Print the result print("BMI") print(bmi) # For a boolean response print("bmi > 23") print(bmi > 23) print("bmi 23]...

Generators

import random def lottery(): # returns 6 numbers between 1 and 40 for i in range(6): yield random.randint(1, 40) # returns a 7th number between 1 and 15 yield random.randint(1,15) # returns a 8th number between 100 and 120 yield random.randint(100,120) for random_number in lottery(): print("And the next number is... %d!" %(random_number)) ========= RESTART: C:/python/lottery20210222.py ======== And the next number is... 25! And the next number is... 25! And the next number is... 16! And the next number is... 38! And the next number is... 22! And the next number is... 4! And the next number is... 9! And the next number is... 115! >>>