python第一周學習筆記

# ? Python第一天學習筆記

-要點 0 添加作者信息


-要點1 交換變量的值

```

這是代碼區(qū)域

a =1

b =2

a, b = b, a

print('a的值是:',a,'b的值是:',b)

print(type(a))

```

-要點2多個變量的賦值

```

name, age, sex ='雷小壞',18,'m'

print(name,age,sex)

```

-要點3數(shù)據(jù)類型的轉(zhuǎn)換

```

input ('請輸入')

print(type(name))

name =int(name)

print(type(name))

```

-要點4判斷語句(if,else)

# 判斷和循環(huán)

# if? else? elif

# if 要判斷的條件:

#? ? 條件成立時,要做的事情

# age = input('請輸入您的年齡')

# age = int (age)

# age = 11

#if xxx1:

#? ? xxxx2

#elif xxxx2:

#? ? xxxx2

#elif xxx3:

#? ? xxxx3

#else:

#? ? 上面的條件都不滿足執(zhí)行的事情

```

score =input('請輸入您的分數(shù)')

score =int(score)

if score>=90 and score <=100:

print('考試等級是A')

elif score >=80 and score <=90:

print('考試等級是B')

elif score >=70 and score <=80:

print('考試等級是C')

elif score >=60 and score <=70:

print('考試等級是D')

else:print('成績不及格')

```

-要點5 猜拳游戲

# 猜拳游戲

import random

player= input('請輸入:剪刀(0),石頭(1),布(2)')

player= int(player)

# 生成【0,2】隨機整數(shù)

computer=random.randint(0,2)

# 獲勝的情況

if (player== 0 and computer==2)or(player== 1 and computer== 0)or(player==2 and computer== 1):

? ? print('玩家獲勝')

# 平局的情況

elif (player== computer):

? ? print('平局,要不要再來一局')

#輸了

else:

? ? print('你輸了,不要走,決戰(zhàn)到天亮')

-要點6 循環(huán)語句while


```

# while循環(huán)

# while 條件:

#? ? 條件滿足時執(zhí)行的事情

# while? True:

#? ? print('老婆我錯了')

# i = 0

# while i < 10:

#? ? print('老婆我錯了,這是我第{}次向您道歉'.format(i))

#? ? i += 1

```

-要點7 循環(huán)的嵌套

# print(sum)

# *

# **

# ***

# ****

# *****

# 循環(huán)的嵌套

?i = 1

?while i <= 5:

??? j = 1

? ? while j <= i:

? ? ? ? print("* ",end='')

? ? ? ? j += 1

? ? print ('\n')

? ? i += 1

-要點8 for循環(huán),break和continue語句的使用

# for 循環(huán)

# for 臨時變量? in 可迭代對象:

# 循環(huán)滿足時要執(zhí)行的事情

company= 'neusoft'

for xin company:

? ? print (x)

? ? if x=='s':

? ? ? ? print("這個s要大寫")

# break 立刻結束break所在的循環(huán)

# continue 結束當前循環(huán),緊接著執(zhí)行下一次循環(huán)

# break

company='neusoft'

for xin company:

? ? print('--------')

? ? if x== 's':

? ? ? ? break

? ? print(x)

else:

? ? print('for 循環(huán)沒有執(zhí)行break,則執(zhí)行本語句')

# continue

company='neusoft'

for xin company:

? ? print('--------')

? ? if x== 's':

? ? ? ? continue

? ? print(x)

else:

? ? print('for 循環(huán)沒有執(zhí)行break,則執(zhí)行本語句')

-要點9 字符串,切片


# 字符串

word= "hello , 'c'"

print(word)

# 切片:截取一部分的操作

# 對象[起始:終止:步長]? 不包括結束位,左閉右開

name= 'abcdef'

print(name[4:2:-1])

print(name[3:5])

print(name[2:])

print(name[1:-1])

print(name[:])

print(name[::2])

print(name[5:1:-2])

# fedcba

print(name[::-1])

-要點10 字符串常規(guī)操作

my_str='hello world neuedu and neueducpp'

count= my_str.count('n',0,20)

print(count)

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容