6月26日 火曜日
さて、もう 23時半で時間ないけど、Pythonの学習する。
プログラム言語も外国語も、毎日のように使っていないと忘れちゃうのでなるべく続ける。といいながら、木曜日夜から月曜日朝まで海外に友人の結婚式に参加するのでその間はさすがにやらないと思うけど。。
Courseraの2週目の課題を早めに終えられるように勧めていく。
Week2は List形式の勉強のようだ。
lis = ["a", "b", "c", "d", "e", "f"] の形ね。
あとは Datatypes
x = 17 #integer
y = 3.14 #float
z = "The Walrus" #string
z1 = "30" #string
vowels = {'a', 'e', 'i', 'o', 'u'] # list of strings
r = True # boolean
s = False #boolean
Datatype変換
numstr1 = input("Enter a number: ")
で、
num1 = float(numstr1)
で、変換とか。
この辺は言語でちょっと違うだけで超基本だな。
飛ばし気味に流す。。
あとは、sublist
newEngland = [["Massachusetts",6692824],["Connecticut",3596080],
["Maine",1328302],["New Hampshire",1323459],
["Rhode Island",1051511],["Vermont",626630]]
こんなの
newEngland[0] でとれば、["Massachusetts", 6692824] が取れる。
newEngland[1][0]でとれば、'Conneticut' がとれる。
newEngland[1][1]でとれば、3596080 がとれる。
あとは Random Library
import random で Random Libraryをimport
これで、 print (random.random()) とかで 0 - 1 までの乱数を出せる。
print (random.randint(3,8)) とかすると、 3 - 8 までの乱数を出せる。
リストからのランダム選択は
articles=["a", "the", "that", "this"]
article = random.choice(articles)
のような形でできると。。
最後に、Loops and Appendで、
break命令で while Loopから抜ける例とか
def add_up():
sum_ = 0
while True: # will loop forever
num = int(input("Enter a number, input 0 to quit: "))
if num == 0:
break # breaks out of while loop
sum_ = sum_ + num
print(sum_)
List への 追加の append命令とか
num_lis.append(nextnum) で数字を追加。
2週目は大体こんなところか。
------------------
課題2:案外わからなかった(w)
最後の1個とるのはmy_list[-1]
途中から最後までは my_list[n:]
とか。
0) print the whole list (this doesn't require a while or for loop)
1) print the item with index 0
2) print the last item in the list
3) print the items with indexes 3 through 5 but not including 5
4) print the items up to the one with index 3 but not including item 3
5) print the items starting at index 3 and going through the end.
6) print the length of the list ( use len() )
7) Use the append() method of a list to append the letter "z" onto a list.
Print the list with z appended.
def problem2_2(my_list):
print(my_list)
print(my_list[0])
print(my_list[-1])
print(my_list[3:5])
print(my_list[0:3])
print(my_list[3:])
print(len(my_list))
my_list.append("z")
print(my_list)
------------------------------------------
もう1時か:寝よう。
Problem 2_2 まで。
problem 2_8まである。
明日どこかで策っと終わらせて提出しとかないと提出する日がない。
コメント
コメントを投稿