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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/17 python 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 轉義字符

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


1.6 原始字符串 raw Strings

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

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

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

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

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

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

乘法:可多倍輸出

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

運算符:
> \ < \ == \ >= \ <= \ != \ 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_变量字符串与随机数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。