Python学习day01_变量字符串与随机数
Python學習
- Python學習_day01
- 1.1 一個猜數字的小游戲
- 1.2 Python內置函數
- 1.3變量 Variable
- 1.4 字符串 String
- 1.5 轉義字符
- 1.6 原始字符串 raw Strings
- 1.7 字符串的加法和乘法 Concatenation and multiplication of Strings
- 1.8 循環結構
- 1.9 改進小游戲
Python學習_day01
初學建議使用IDLE: Integrated Development and Learing Evrionment 快樂碼字
永遠的 hello world
>>>print("hello world") hello world >>>print('hello world') hello world >>>print('''hello world''') hello world輸入 import this,一起欣賞一下這首Python之詩~
>>>import this The Zen of Python, by Tim PetersBeautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!1.1 一個猜數字的小游戲
"""用python設計一個小游戲""" temp = input("猜一下心里想的一個數字:") guess = int(temp)if guess == 0:print("yes, 你猜對了!") else:print("no, 你猜錯了,正確的數字是0哦!") print("game over!")
input() 函數用于接收用戶的輸入及返回。
tips:
Java用戶記得不要寫 " ; " 哦!
正確使用縮進!
正確拼寫函數!
1.2 Python內置函數
輸入dir(__builtins__), 查看Python的內置函數,兩個下劃線!
1.3變量 Variable
變量不可直接數字開頭!
變量可以使用中文!
變量取最新值!
若要交換兩個變量的值,可以不使用臨時變量(第三變量),直接 x,y = y,x 即可交換!
1.4 字符串 String
Single quotes ’ ’
Double quotes " "
Triple quoted 三個單引號 ‘’’ ‘’’ 或 三個雙引號 “”" “”"
引號必須成雙成對!
1.5 轉義字符
| \\ | 反斜杠(\) |
| \’ | 單引號(’) |
| \" | 雙引號(") |
| \a | 響鈴(BEL) |
| \b | 退格符(BS) |
| \n | 換行符(LF) |
| \t | 水平制表符(TAB) |
| \v | 垂直制表符(VT) |
| \r | 回車符(CR) |
| \f | 換頁符(FF) |
| \ooo | ooo 為八進制數 |
| \xhh | hh 為十六進制數 |
1.6 原始字符串 raw Strings
要輸出原始字符串,在反斜杠前再加一個反斜杠,類似此筆記:
若要輸出的東西被誤以為是轉義字符,只需在輸出前加 r 即可:
?? 反斜杠不可放在字符串末尾!反斜杠放在字符串的末尾,表示還沒結束!
使用三引號,中間可無限換行,替代使用末尾反斜杠。
1.7 字符串的加法和乘法 Concatenation and multiplication of Strings
與其他語言一樣,字符串的加法是拼接
乘法:可多倍輸出
運算符:
> \ < \ == \ >= \ <= \ != \ is \ is not
1.8 循環結構
While 條件:
如果條件為真 (true) 執行這里的語句
1.9 改進小游戲
"""用python設計一個小游戲""" """用python設計一個小游戲"""import randomcounts =3 answer = random.randint(0,10) while counts > 0:temp = input("猜一下心里想的一個數字:")guess = int(temp)if guess == answer:print("yes, 你猜對了!")breakelse:if guess < answer:print("猜小啦")else:print("猜大啦")counts = counts -1 print("game over!")
1.使用break語句組織部輸入正確后的無效循環。
2.random函數
使用 import 導入函數
random.randint(m,n) 隨機打印 m 到 n 的一個數。
3.random 隨機數重現
使用 random.setstate() 來存儲隨機數,使用 random.getstate() 來獲取隨機數。
random.getstate() 只表示取出隨機數:
若先將第一次讀取出來的隨機數使用 random.setstate()保存起來再讀取,便可復現上次的隨機數。
抽獎黑幕系統原理大概如此!
Mac快捷鍵
Control + P:上一行
學會使用help-- Python Docs進行查找文檔
總結
以上是生活随笔為你收集整理的Python学习day01_变量字符串与随机数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java虚拟机的垃圾收集算法】
- 下一篇: linux 常用命令及操作