Python全栈课程004

python学习网 2020-08-24 22:13:02

程序结构

  • 顺序
  • 选择
  • 循环

选择(分支)语句

  • if语句
  • if 表达式:
    语句1
    语句2
    else:
    语句
a = input("请输入数字:") #字符串
a = int(a)
if a < 10:
    print("Python全栈课程")
else:
    print("Linux从入门到放弃")
请输入数字:100
Linux从入门到放弃
#输入成绩,100-90分为优秀,89-80分为良好,79-60分为及格,60分以下为不及格
score = input("input your score:")
score = int(score)
if score <= 100 and score >= 90:
    print("优秀")
else:
    if score <= 89 and score >= 80:
        print("良好")
    else:
        if score <= 79 and score >=60:
            print("及格")
        else:
            print("不及格")       
input your score:99
优秀
score = input("input your score:")
score = int(score)
if score <= 100 and score >= 90:
    print("优秀")
elif score <= 89 and score >= 80:
    print("良好")
elif score <= 79 and score >=60:
    print("及格")
else:
    print("不及格")
input your score:99
优秀

pass关键字

  • 占位符
a = 1
if a < 2:
  File "<ipython-input-12-504e793a112e>", line 2
    if a < 2:
             ^
SyntaxError: unexpected EOF while parsing
a = 1
if a < 2:
    pass
a = 1
if a < 2:
    pass #跳过此语句,而不是终止
    print("pass")
pass

循环

  • for循环
  • while循环
list = [1,2,3,4]
for i in list:
    print(i)
1
2
3
4
list = [1,2,3,4]
for i in list:
    print("**********************")
    if i % 2 == 0:
        print("Python全栈课程")
    else:
        print("Linux从入门到放弃")
    print("**********************")
**********************
Linux从入门到放弃
**********************
**********************
Python全栈课程
**********************
**********************
Linux从入门到放弃
**********************
**********************
Python全栈课程
**********************
print(1)
print(2)#print()自动换行
1
2
print(1, end = "") #end的默认值为\n(换行)
print(2, end = "")
12
for i in range(1,101,2):#range:从1开始到100结束,步长为2
    print(i, end = " ")
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 
list = [1,2,3,4]
for i in list:
    print(i)
else:
    print("python全栈课程")
    print("Linux从入门到放弃")
1
2
3
4
python全栈课程
Linux从入门到放弃
阅读(2404) 评论(0)