Python学习day01_变量字符串与随机数
Python學(xué)習(xí)
- Python學(xué)習(xí)_day01
- 1.1 一個(gè)猜數(shù)字的小游戲
- 1.2 Python內(nèi)置函數(shù)
- 1.3變量 Variable
- 1.4 字符串 String
- 1.5 轉(zhuǎn)義字符
- 1.6 原始字符串 raw Strings
- 1.7 字符串的加法和乘法 Concatenation and multiplication of Strings
- 1.8 循環(huán)結(jié)構(gòu)
- 1.9 改進(jìn)小游戲
Python學(xué)習(xí)_day01
初學(xué)建議使用IDLE: Integrated Development and Learing Evrionment 快樂碼字
永遠(yuǎn)的 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 一個(gè)猜數(shù)字的小游戲
"""用python設(shè)計(jì)一個(gè)小游戲""" temp = input("猜一下心里想的一個(gè)數(shù)字:") guess = int(temp)if guess == 0:print("yes, 你猜對了!") else:print("no, 你猜錯(cuò)了,正確的數(shù)字是0哦!") print("game over!")
input() 函數(shù)用于接收用戶的輸入及返回。
tips:
Java用戶記得不要寫 " ; " 哦!
正確使用縮進(jìn)!
正確拼寫函數(shù)!
1.2 Python內(nèi)置函數(shù)
輸入dir(__builtins__), 查看Python的內(nèi)置函數(shù),兩個(gè)下劃線!
1.3變量 Variable
變量不可直接數(shù)字開頭!
變量可以使用中文!
變量取最新值!
若要交換兩個(gè)變量的值,可以不使用臨時(shí)變量(第三變量),直接 x,y = y,x 即可交換!
1.4 字符串 String
Single quotes ’ ’
Double quotes " "
Triple quoted 三個(gè)單引號 ‘’’ ‘’’ 或 三個(gè)雙引號 “”" “”"
引號必須成雙成對!
1.5 轉(zhuǎn)義字符
| \\ | 反斜杠(\) |
| \’ | 單引號(’) |
| \" | 雙引號(") |
| \a | 響鈴(BEL) |
| \b | 退格符(BS) |
| \n | 換行符(LF) |
| \t | 水平制表符(TAB) |
| \v | 垂直制表符(VT) |
| \r | 回車符(CR) |
| \f | 換頁符(FF) |
| \ooo | ooo 為八進(jìn)制數(shù) |
| \xhh | hh 為十六進(jìn)制數(shù) |
1.6 原始字符串 raw Strings
要輸出原始字符串,在反斜杠前再加一個(gè)反斜杠,類似此筆記:
若要輸出的東西被誤以為是轉(zhuǎn)義字符,只需在輸出前加 r 即可:
?? 反斜杠不可放在字符串末尾!反斜杠放在字符串的末尾,表示還沒結(jié)束!
使用三引號,中間可無限換行,替代使用末尾反斜杠。
1.7 字符串的加法和乘法 Concatenation and multiplication of Strings
與其他語言一樣,字符串的加法是拼接
乘法:可多倍輸出
運(yùn)算符:
> \ < \ == \ >= \ <= \ != \ is \ is not
1.8 循環(huán)結(jié)構(gòu)
While 條件:
如果條件為真 (true) 執(zhí)行這里的語句
1.9 改進(jìn)小游戲
"""用python設(shè)計(jì)一個(gè)小游戲""" """用python設(shè)計(jì)一個(gè)小游戲"""import randomcounts =3 answer = random.randint(0,10) while counts > 0:temp = input("猜一下心里想的一個(gè)數(shù)字:")guess = int(temp)if guess == answer:print("yes, 你猜對了!")breakelse:if guess < answer:print("猜小啦")else:print("猜大啦")counts = counts -1 print("game over!")
1.使用break語句組織部輸入正確后的無效循環(huán)。
2.random函數(shù)
使用 import 導(dǎo)入函數(shù)
random.randint(m,n) 隨機(jī)打印 m 到 n 的一個(gè)數(shù)。
3.random 隨機(jī)數(shù)重現(xiàn)
使用 random.setstate() 來存儲隨機(jī)數(shù),使用 random.getstate() 來獲取隨機(jī)數(shù)。
random.getstate() 只表示取出隨機(jī)數(shù):
若先將第一次讀取出來的隨機(jī)數(shù)使用 random.setstate()保存起來再讀取,便可復(fù)現(xiàn)上次的隨機(jī)數(shù)。
抽獎黑幕系統(tǒng)原理大概如此!
Mac快捷鍵
Control + P:上一行
學(xué)會使用help-- Python Docs進(jìn)行查找文檔
總結(jié)
以上是生活随笔為你收集整理的Python学习day01_变量字符串与随机数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java虚拟机的垃圾收集算法】
- 下一篇: python数据类型和循环控制