一、类型转换
1,int代表整数数据类型
2、str代表字符串数据类型
3、float代表小数数据类型
两个之间的转换,想要转换成什么数据类型,就给要转换的数据,前面加转换的数据类型,就是:如图
str1="123" age=17 #把字符串的数据类型转换成int数据类型 print(int(str1)) print(type(int(str1))) score=int(input('请输入学生分数:\n')) print(type(score))
图解:
二、for循环
对一个数值(名称)覆盖,循环
str2="积分活动" for itme in str2: print(itme)
三、while循环(1)
持续循环(尽量不要单独使用)
while True: score=int(input("输入年龄:\n")) if score<60: print("重新输入:\n") continue elif score>=60 and score<70: print("重新输入:\n") continue elif score>=70 and score<90: print("重新输入:\n") continue elif score>=90 and score<100: print("找到了") break
图解:
四、while循环(2)
对逻辑进行判断,得到想要的结果
while True: score=int(input('请输入学生分数:\n')) if score>50 and score<60: print(' 你没及格') elif score>=60 and score<65: print('刚及格,还得努力') elif score>=65 and score<80: print('成绩良好') elif score>=80 and score<90: print('成绩优秀') elif score>=90 and score<100: print('非常优秀') elif score==100: print('恭喜你,满分') else: print('未知错误') break
图解:
五、逻辑判断
score=60 if score>=50 and score<60: print('你没及格') elif score>=60 and score<65: print('刚及格,还得努力') elif score>=65 and score<80: print('成绩良好') elif score>=80 and score<90: print('成绩优秀') elif score>=90 and score<100: print('非常优秀') elif score==100: print('恭喜满分') else: print('未知错误')
六、字符串
常用的字符串
(查看对象有哪些方法:
str1="this is a pyhton language" print(dir(str1))
图解:
)
(1)判断以什么开始(startswith)
如:
str1="whate are you doing" print('判断字符串以什么开始:',str1.startswith('wh'))
图解:
(2)判断以什么结束(endswith)
str1="whate are you doing" print('判断字符串以什么结束:',str1.endswith('ing'))
图解:
(3)判断是不是数字(isdigit)
str1="whate are you doing" print('判断字符串是不是数字:',str1.isdigit())
图解:
(4)判断是否大写(isupper)
str1="whate are you doing" print('判断字符串是否大写:',str1.isupper())
图解:
(5)判断是否小写(islower)
str1="whate are you doing" print('判断字符串是否小写:',str1.islower())
图解:
(6)对字符串进行分割(split),分割完的数据了类型是(list)
str1="whate are you doing" print('对字符串进行分割:',str1.split())
图解:
(7)大写字母转小写(lower)
str2='JAJV' print('大写字母转小写:',str2.lower())
图解:
(8)取消空格键(strip)
str3=' are ' print('没有取消空格键:',str3) print('取消空格键:',str3.strip())
图解:
(9)使用jojn数据类型转换str数据类型
list1=['python','jajv','go'] print('list转换str类型:',' '.join(list1))
图解:
(10)一个数据类型,想要的某个字在第几位(index)
如:
str3="holle" print('o他的索引为:',str3.index('o'))
图解:
(11)对象在字符串里面有几个(count)
str4='fsddsd' print('对象在字符串里面存在多少个:',str4.count('d'))
图解: