日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python学习day01_变量字符串与随机数

發(fā)布時(shí)間:2025/3/17 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python学习day01_变量字符串与随机数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)義字符

符號Notes
\\反斜杠(\)
\’單引號(’)
\"雙引號(")
\a響鈴(BEL)
\b退格符(BS)
\n換行符(LF)
\t水平制表符(TAB)
\v垂直制表符(VT)
\r回車符(CR)
\f換頁符(FF)
\oooooo 為八進(jìn)制數(shù)
\xhhhh 為十六進(jìn)制數(shù)


1.6 原始字符串 raw Strings

要輸出原始字符串,在反斜杠前再加一個(gè)反斜杠,類似此筆記:

若要輸出的東西被誤以為是轉(zhuǎn)義字符,只需在輸出前加 r 即可:

?? 反斜杠不可放在字符串末尾!反斜杠放在字符串的末尾,表示還沒結(jié)束!

使用三引號,中間可無限換行,替代使用末尾反斜杠。

1.7 字符串的加法和乘法 Concatenation and multiplication of Strings

與其他語言一樣,字符串的加法是拼接

乘法:可多倍輸出

print("Love u 3000 tims everyday\n" * 3000)

運(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。