Udemy課程:[100 Days Of Code(Dr. Angela Yu)](https://www.udemy.com/course/100-days-of-code/) # Day 4 - Beginner - Randomisation and Python Lists ###### tags: `python` `Udemy` `100 Days Of Code` 2021.01.22(Fri.) ## ● 前言 / 心得 開始慢慢覺得稍微有難度了,不過目前來說花點時間還是都能做出來,整體來說還是不算難,主要是語法方面還不太習慣,偶爾需要查一下。 像是python的random要導入模組才能使用!還有python中的length用法是「len(東西)」,而在JavaScript中是直接「東西.length」。 不過在做第四天的專題,有先自己把剪刀石頭布做出來,再看老師實作,有覺得老師的方法比較複雜了點。老師的做法是用0<1<2比大小,再加上特例,但是特例實在太多了,那還不如把所有輸的組合和平手的情形搭配出來(也才四組),其他就是贏了,感覺簡單很多。 ## ● 上課筆記 ## 0.code [day-4-start](https://www.udemy.com/course/100-days-of-code/learn/lecture/18011837#overview) [day-4-1-exercise](https://repl.it/@tina0915tw/day-4-1-exercise#main.py) [day-4-list-practice](https://repl.it/@tina0915tw/day-4-list-practice#main.py) [day-4-2-exercise](https://repl.it/@tina0915tw/day-4-2-exercise#main.py) [day-4-3-exercise](https://repl.it/@tina0915tw/day-4-3-exercise#main.py) [rock-paper-scissors](https://repl.it/@tina0915tw/rock-paper-scissors-start#main.py) ## 1.使用random函數第一步 ```python= import random ``` random()是不能直接使用的,得先導入random module(模組) 函式庫用法(官方):[Python random Module](https://www.askpython.com/python-modules/python-random-module-generate-random-numbers-sequences) * 一些常用函數: **** 1. **randint(a, b)**: Returns a random integer between a and b (both inclusive). This also raises a ValueError if a > b. ➤會得到a與b之間(包含)任意的整數,另外如果a>b,會產生ValueError。 **** 2. **random()**: Returns the next random floating point number between [ 0.0 to 1.0) ➤會得到0到1(不包含1)之間的任意小數。 **** ## 2. Module(模組) 開發大型應用程式時,模組(Module)化就顯得相當的重要,讓程式碼能夠透過引用的方式來重複使用,提升重用性(Reusable)。 (只要在主程式中import那個模組即可) ## 3.list的用法 **** * **取得list的元素:** 程式語言中的index都是從0開始的,所以要取得第一個元素,要輸入的位置值是0。 相反的,若是想要取得最後一個的元素,則可輸入位置值為「-1」(畢竟沒有「-0」),然後到數第二個就是輸入「-2」。 > 輸入的方式: ```python= name=["Bobby","Dora","Kevin","Mike","Vincent"] student01=name[0] student04=name[-1] print(student01) #"Bobby" print(student04) #"Vincent" ``` **** * **修改list中的元素:** 假如想要修改list中的某個元素,則直接用等號賦予新變數即可更改。 > 輸入的方式: ```python= name=["Bob","Dora","Kevin","Mike","Vincent"] name[0]="Bobby" print(name) #["Bobby","Dora","Kevin","Mike","Vincent"] ``` **** * **append()用法:** 想在list最後面新增新的元素,即可使用append,來增添新元素進去list中。 > 輸入的方式: ```python= name=["Bob","Dora","Kevin","Mike","Vincent"] name.append("Tina") print(name) #["Bobby","Dora","Kevin","Mike","Vincent","Tina"] ``` **** * **其他list的用法** 可直接在Python的官方找到:[官方網址](https://docs.python.org/3/tutorial/datastructures.html)