python for循环连续输入五个成绩判断等级_Python基础(1)——输入输出/循环/条件判断/基本数据类型...
一、編程語言
1、 編譯型語言:先編譯,再執行 (先編譯成二進制)
舉例:英文書翻譯成中文再看
C、C++、C#
2、解釋型語言:一邊執行一邊編譯
舉例:英文書找個翻譯,它翻譯一行你聽一行
php、js、python、java、go
二、輸入輸出
1、基礎字符類型
name = '小黑'#字符串 str
age = 18 #整數類型 int
score = 97.5 #浮點型 float
2、輸入輸出
your_name = input('請輸入你的名字:')#python2中用raw_input
print('你的名字是:',your_name)
備注:
1) input接收到的都是string類型,需要強制轉換
2)單引號和雙引號
使用時無區別,依據具體情況使用
word1 = "let's go"word2= 'tom is very "shy"'word3= '''let's go ,tom is very "shy"'''
print(word1)print(word2)print(word3)
3、注釋
對選中代碼進行注釋:注釋快捷鍵ctrl+/
多行注釋 :三個引號(單引號/雙引號)
print('hello world!')#單行注釋
"""雙引號注釋
多行注釋"""
'''單引號注釋
多行注釋'''
print('hello world!')
三、條件判斷
1、運算符
>、<、 >=、 <=、 !=、==
2、if ... elif ... else
score = input('請輸入你的分數:')
score=int(score) #input接收到的都是string類型,需要強制轉換if score >= 90:print("優秀")elif score<90 and score>=75:print("良好")elif score>=60 and score<75:print("及格")else:print("不及格")if score == 90:print("猜對了")if score != 83:print("不等于83")
三、循環、遍歷、迭代
while: 需要定義計數器,每次循環計數器增加,否則就會變成死循環
break:循環里面遇到break循環立即結束
continiue:循環里面遇到continue,結束本次循環
type(val) 查看變量的類型
循環對應的else:當循環正常結束后(不是通過break結束),會執行else中代碼
1、python中的while循環
1)break
count = 0 #計數器
rate = 140
while count<10:if rate >160:break
print("跑圈", count)
count= count+1rate+=5
2)continue
count = 0 #計數器
rate = 140
while count<10:
count= count+1
if rate >160:
rate-= 10
continue
print("跑圈", count)
rate+= 5
count =1 跑圈1 rate=145
count =2 跑圈2 rate=150
count =3 跑圈3 rate=155
count =4 跑圈4 rate=160
count =5 跑圈5 rate=165
count =6 rate=165 進if rate=155 遇到continue不打印跑圈直接進入下一次循環
count =7 跑圈7 rate=160
count =8 跑圈8 rate=165
count =9 rate=165 進if rate=155 遇到continue不打印跑圈直接進入下一次循環
count =10 跑圈10 rate=160
2、python中的for循環
1)for循環
rate=130,跑到第7圈后休息
rate = 130
for i in range(10):print("跑圈", i)if rate > 160:print("休息")breakrate+=5
2)for...else
循環對應的else:
當循環正常結束后,會執行else中的代碼,不是通過break結束的。
例如 本來循環10次,循環十次后正常結束,會執行else。
for i in range(10):print(i)else:print('什么時候執行')
非正常結束:
for i in range(10):print(i)if i== 8:break
else:print('什么時候執行')
備注:while... else同樣
count=0while count<3:print(count)
count+=1
else:print("執行else")
練習:
寫一個小游戲:猜數字
# 最多輸入5次,如果猜的數字不對,提示大了或小了,猜對了游戲結束,如果次數用盡沒猜對,提示已經到達次數
分析:
importrandom
number= random.randint(1,100)for i in range(5):
guess= input("請輸入你猜測的數字:")
guess=int(guess)if guess==number:print("恭喜你猜對了,游戲結束")break
elif guess>number:print("猜的數字大了")continue
else:print("猜的數字小了")continue
else:print("錯誤次數過多")
方法二:使用continue實現
importrandom
number= random.randint(1,100)for i in range(5):
guess= input("請輸入你猜測的數字:")
guess=int(guess)if guess==number:print("恭喜你猜對了,游戲結束")break
elif guess>number:print("猜的數字大了")continue
else:print("猜的數字小了")continue
else:print("錯誤次數過多")
四、運算
1、常用運算符號:
+ - * / // %
+= -+ *= /= //=
2、地板除:
print(5//2) #地板除,不要小數部分
#pyhon2 里面默認 /就是地板觸發 5/2=2 5/2.0=2.5
3、取模:
print(5%2) #取余數,取模
4、a+=1#a=a+1
print(5//2)print(5%2)
a=1a+=1 #a=a+1
print(a)print(1.5+2)
5、字符串中的+ 和 *
a='my name is'b='Marry'
print(a+' '+b)print(b*3)
五、字符串格式化
1、加號連接
importdatetime
name='小明'welcome= '小明,歡迎登陸'today=datetime.datetime.today()print(name + ',歡迎登陸.'+'今天的日期是:'+str(today))#不推薦會在內存開創3個空間
2、占位符 %s字符串 %d整數型 %f浮點型
importdatetime
name='小明'today=datetime.datetime.today()
welcome = '%s,歡迎登陸' %nameprint(welcome) welcome= '%s,歡迎登陸,今天的日期是 %s' %(name,today)print(welcome)
age = 18age_str= '你的年齡是:%d' %ageprint(age_str)
score= 73.985score_str= '你的成績是:%f' %score
score_str2= '你的成績是:%.2f' %score #保留2位小數
print(score_str)print(score_str2)
3、大括號方式(適合參數較多的情況)
.format(大括號中定義的變量=賦值的變量)
sub_name = 'Lily'today= datetime.datetime.today()score= 98.133addr="北京"welcome= '{name1},歡迎登陸,今天的日期是{today1}'.format(today1=today,name1=sub_name)print(welcome)
welcome2= '{},歡迎登陸,今天的日期是{}'.format(sub_name,today)print(welcome2)
六、列表
Python內置的一種數據類型是列表:list。
list是一種有序的集合,可以隨時添加和刪除其中的元素。
1)增加
append('xxxxx') 向列表末尾增加元素
insert(位置腳標,'xxxxxx') 向指定位置增加元素
student_new = ['張三','李四','王五']print("增加")
student_new.append('Marry') #向列表末尾增加元素
student_new.insert(0,'Tom') #向指定位置增加元素
print(student_new[0])print(student_new)print(student_new[-1])
2)修改
直接賦值
#修改#['Tom', '張三', '李四', '王五', 'Marry']
print("修改")
student_new[2]='Lisi'
print(student_new)
3)刪除
4種方式刪除
#['Tom', '張三', 'Lisi', '王五', 'Marry']
print("刪除")print("1、傳入指定下標,刪除元素")#1、傳入指定下標,刪除元素
student_new.pop(1)print(student_new)#2、刪除指定的元素(刪除第一個符合的)
print("2、刪除指定的元素")
student_new.remove('Lisi')print(student_new)#3
print("3 del")del student_new[-1]print(student_new)#4清空
print("4清空")
student_new.clear()print(student_new)
4)查詢
student_new = ['張三','李四','王五']
z_count= student_new.count('張三')#統計list中某元素出現的次數
print(z_count)print(student_new.index('張三')) #找某元素下標
5)把一個列表中的元素加入到另一個列表中 extend()
student_new = ['張三','李四','王五']
student_new2= ['MARRY','TOM','JACK']
student_new.extend(student_new2)print(student_new)
6)反轉reverse()
#['張三', '李四', '王五', 'MARRY', 'TOM', 'JACK']
print("反轉list")
student_new.reverse();print('reverse反轉之后:',student_new)
7)排序
升序:sort()
降序.sort(reverse=True)
print("排序")
student_new= ['MARRY','TOM','JACK']
student_new.sort()print(student_new)
numbers= [323,455,98,11,556,899,6]
numbers.sort()#默認升序排列
print(numbers)
numbers.sort(reverse=True)#降序排列
print(numbers)
8)多維列表
student_info =[
[1,'tom','beijing'],
[2,'mary','shanghai'],
[3,'lily','tianjin']
]
student_info2=[
[1,'tom','beijing',['bmz','benz','audi']],
[2,'mary','shanghai',['bmz','benz','audi']],
[3,'lily','tianjin']
]
student_info[0][-1]='山東'
print(student_info)
student_info2[0][-1].append('特斯拉')print(student_info2)
student_info2[0].pop(2)print(student_info2) #beijing刪除了
student_info2[2].append(['五菱宏光','英菲尼迪'])print(student_info2)
練習:
錄入學生信息判斷學生是否存在,如果已經存在提示并繼續添加
直到輸入over,結束
分析:
student = ['lhy','hzy','fd']while 1>0:
a= input('請輸入學生:')if a == 'over':break
#elif student.count(a)>0:
elif a in student: #用in判斷是否在list中 ; elif a not in student:不在list中
print("已經存在")else:
student.append(a)print("已添加%s"%a)print('錄入成功')print(student)
練習2:打印多維數組中元素
備注:len() 列表長度
student_info2 =[
[1,'lhy','beijing'],
[2,'hzy','beijing'],
[3,'ljj','beijing']
]
index=0
student_len=len(student_info2) #list長度
while index
stu= student_info2[index]
print(stu)
stuid= stu[0]
name = stu[1]
addr = stu[2]
sql = 'insert into student value ({id},"{name}","{addr}");'.format(
id=stuid,name=name,addr=addr
)
print(sql)
用拆包方式
student_info2 =[
[1,'lhy','beijing'],
[2,'hzy','beijing'],
[3,'ljj','beijing']
]
index=0
student_len=len(student_info2) #list長度
while index
stu= student_info2[index]
stuid,name,addr=stu#拆包代替以上代碼(變量名稱必須與數組中數量一致) sql= 'insert into student value ({id},"{name}","{addr}");'.format(id=stuid,name=name,addr=addr)print(sql) index+=1
用for循環:
for stu instudent_info2:
stuid2,name2,addr2=stu
sql= 'insert into student value ({id},"{name}","{addr}");'.format(
id=stuid2,name=name2,addr=addr2
)print(sql)
9)延伸學習——元祖tuple
另一種有序列表叫元組:tuple,tuple和list非常類似,但是tuple一旦初始化就不能修改,它沒有append(),insert()這樣的方法。
student= ('張三', '李四', '王五')
獲取元素:print(student[0]),但不能賦值成另外的元素
tuple的意義:因為tuple不可變,所以代碼更安全。
七、布爾類型
#bool 布爾類型#true 真#false 假
l= [1,2,3,4]print(1>2)print(4 inl)print(1 not inl)print(len(l)>3)
總結
以上是生活随笔為你收集整理的python for循环连续输入五个成绩判断等级_Python基础(1)——输入输出/循环/条件判断/基本数据类型...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java音频采样_音频重采样的坑
- 下一篇: websocket python爬虫_p