2019-04-21第一天學習成果

學會添加作者及其相關信息

注釋

#單行注釋
'''
多行注釋
'''
"""
多行注釋
"""

交換兩個變量的值

a = 1
b = 2
temp = b
b = a
a = temp
print( 'a的值是:',a,'b的值是:',b)
#python中一句話搞定
a,b = b,a
print( 'a的值是:',a,'b的值是:',b)

變量以及類型

標識符

格式化輸出

hero_name=魯班七號
level = 15
print('您選擇的英雄是%s當前等級為%d'(hero_name,level))
print('您選擇的英雄是{hero_name}當前等級為{level}'.format(hero_name=hero_name,level=level)

多變量賦值操作

name,age,sex='陳智越' , 20 , 'f'
print(name,age,sex)

數(shù)據(jù)類型轉(zhuǎn)換

name=input('請輸入')
print(type(name))
name=int(name)
print(type(name))
str1="1+19'
res=eval(str1)
print(res)
image.png

判斷和循環(huán)

if else elif

#if 要判斷的條件:
#        條件成立時,要做的事情
#        if的嵌套
age=input('請輸入你的年齡')
age=int(age)
if age >=18:
        print('你已經(jīng)成年,可以去網(wǎng)吧了')
        if age ==20:
                print('網(wǎng)吧沖會員減半')
else:
        print('你還是個弟弟')
if xxx1:
        xxxx1
elif xxxx2:
        xxxx2
elif xxxx3:
        xxxx3
else:
 #       上面的條件都不滿足執(zhí)行的事情
score=77
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('你不及格')
#elif 必須和 if 一起使用,否則出錯
#else 在最后出現(xiàn)

score = '你的分數(shù)是77' if score == 77 else score
print(score)

猜拳游戲

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)到天亮 ' )

循環(huán) while循環(huán)

#while 條件: 
#        條件滿足時執(zhí)行的事情
while True:
        print(' 老婆 , 我錯了 ' )  
i = 0
while i < 10:
        print( ' 老婆, 我錯了,只是我第{ }次向您道歉'.format ( i ) )
        i += 1
#計算 1~100 之間偶數(shù)的累加和包含(1和100)
i = 1
while i <= 100:
        if i % 2 == 0:
                sum += i
        i += 1
print ( sum )

循環(huán)的嵌套

#*
#* *
#* * *
#* * * *
#* * * * *
#循環(huán)的嵌套
i = 1
while i <= 5 :
        j = 1
        while j <= i:
                print( " * " , end = ' ' )
                j += 1
        print ( ' \n ' )
        i += 1
#九九乘法表
i = 1
while i <= 9 :
        j = 1
        while j <= i:
                  print ( " %d * %d = %d     " %(j , i , i*j), end ='')
                  j += 1
print ( '\n' )
i += 1

for 循環(huán)

# for 臨時變量 in 可迭代的對象:
#循環(huán)滿足時要執(zhí)行的事情
company = 'neusoft '
for x in company:
        print (x)
        if x == 'S':
                print( " 這個S要大寫 ")
        for ( int i = 0 ; i < 100 ; i ++){
}
#range( 起始值,終值,步長 )
for i in range (1,101,2): 
        print( i )

break和continue

#break的作用:立刻結(jié)束break所在的循環(huán)
#continue的作用:用來結(jié)束本次循環(huán),緊接著執(zhí)行下一次的循環(huán)
company = 'neusoft'
for x in company:
    print( '---------')
    if x == 's':
        break
    print(x)
else:
    print('for循環(huán)沒有執(zhí)行break,則執(zhí)行本語句')
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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