python之路day01 變數

2022-04-10 20:57:41 字數 3377 閱讀 6537

變數就是將一些運算的中間結果暫存到記憶體中,以便後續**塊呼叫。

規範:1、必須由數字、字母、下劃線任意組合,且不能數字開頭。

2、不能是python中的關鍵字。如:『print』 'and' 'or' 'break' 

3、變數具有可描述性

4、不能是中文(看著很撈)

一直不變的量。 如 派 π

在python裡面所有的變數都是可變的,所以用全部大寫的變數名來代表次變

量為常量

如 bir_of_china =1949

1、整形:int

2、字串:str,python中凡是用引號引起來的都是字串

可相加:字串的拼接

可相乘:字串*整形   srt*int

字串的轉換:

ps:type()

字串轉換為數字:int(str) 條件:str必須為數字組成的 如a=int(『222』)  此時的a就是整形  

數字轉換為字串:str(int)

3、bool:布林值  true false  判斷對與錯使用

name =input('

請輸入你的名字:')

age = input('

請輸入你的年齡:')

print

(name,age)

print('

my name is

',name,'

,i am

',age,'

year old

')

請輸入你的名字:zhangyang

請輸入你的年齡:22zhangyang 22my name

is zhangyang ,i am 22 year old

1、if 條件:

結果

:(冒號是為了識別條件和結果)

python是解釋形語言,逐行解釋**,從上到下執行。

上面執行結果如下:

2、多項選擇:

3、巢狀

1、while迴圈

while true:

print(『666』)

print(『777』)

終止迴圈的方法:

①改變迴圈條件使迴圈不滿足

②break關鍵字終止迴圈

③continue 在迴圈體中結束本次迴圈,繼續下一次迴圈

結果都是1,不會執行到count=count+1

ps:加法

count =1sum =0

while count <= 10:

sum = sum +count

count = count + 1

print(sum)

結果sum=0+1+2+3+4+5+6+7+8+9+10=55

使用while迴圈輸出1 2 3 4 5 6 8 9 10
count =0

while count<10:

count+=1

if count == 7:

print('')

else

:

print(count)

求1到100所有數的和
count=1sum=0

while count<=100:

sum =sum +count

count=count+1

print(sum)

輸出1-100內所有的奇數
1)方法一

count =1

while count<=100:

print(count)

count+=2

2)方法二取餘數

count=1

while count<=100:

if count % 2 ==1:

print(count)

count+=1

輸出1-100內所有的偶數
count =0

while count<=100:

print

(count)

count+=2

求1-2+3-4+5-6 ...-98+99 的和
sum =0

count =1

while count<100:

if count % 2 == 1:

sum +=count

else

: count % 2 ==0

sum -=count

count += 1

print(sum)

使用者登入(三次機會重試)
i=1

while i <=3:

name = input('

請輸入你的使用者名稱:')

password = input('

請輸入你的密碼:')

if name =='zy'

and password == '

password':

print('

----登入成功!----')

break

else

:

print('

使用者名稱或密碼錯誤')

i+=1

else

:

print('

使用者名稱密碼錯誤已超過3次,請稍後再試

')

python入門學習day01

基礎爬蟲知識入門 01 匯入模組 import urllib.request 爬取的目標網頁 file urllib.request.urlopen 讀取全部網頁,file.readline讀取行 data file read print data 開啟路徑,wb表示以二進位制方式寫入 write方...

python學習筆記 Day 01

回憶並複述是加強記憶的好方式!python3的所有常用語法 物件導向程式設計思維 運用模組進行程式設計 遊戲程式設計 計算機 是乙個 python shell,shell 的意思就是 外殼 乙個通過鍵入文字與程式互動的途徑!像我們 windows 那個 cmd 視窗,像 linux 那個黑乎乎的命令...

day01 關鍵字 變數 2

目錄 一 關鍵字 二 變數 1.變數宣告 2.批量宣告 三 變數的初始化 1.宣告並初始化變數 2.型別推導 3.短變數宣告 四 匿名變數 practise go語言中有25個關鍵字 break default func inte ce select case defer go map struct...