1、Python常用的数据类型有哪些?
1、int(代表整数数据类型,如:56、21、5) (int:整数 如年龄:36、57、21)
2、str(代表字符串数据类型,需要加双引号,如:"das211") (str:字符串,需加双引号,如:str=“123”)
3、bool(真:turn,假:false)
4、float(代表金额上的数据类型) (有小数位数:1555.65、434.45)
2、把str转为int,举例说明
str1='123'
print(int(str1))
print(type(int(str1)))
3、把int转为str,举例说明
age=18
strAge=str(age)
print(strAge)
4、把float转为int
score=23.33 scoreInt=int(score) print(scoreInt) print(type(scoreInt))
5、把salary=1990.98转为int,再接着转为str
salary=1990.98 #fioat-->int intSalary=int(salary) print(intSalary) print(type(intSalary)) #int-->str strSalary=str(salary) print(strSalary) print(type(strSalary))
6、什么是解码,举例说明
解码是:是把str数据类型转换成bytes(二进制)的过程,使用到encode方法
如:
str0="dongfang" str_byte=str.encode('utf-8') print(str_byte) print(type(str_byte)) byte_str=str_byte.decode('utf-8') type(byte_str) print(byte_str)
7,什么是编码,举例说明
编码:就是把byte转换成str的过程,会用到decode方法
如:
str0 = "dongfang" str_byte = str.encode('utf-8') print(str_byte) print(type(str_byte)) byte_str = str_byte.decode('utf-8') type(byte_str) print(byte_str)
8、变量定义的规则,举例说明规范的变量命名和不规范的变量命名
变量就是对任何一个事物的称谓,可以代表任何东西(一切皆对象)
(参考数据类型)
错误:
正确:
9、请描述变量的生命周期
变量在进行调用的时候分配内存地址,调用结束后会释放内存信息
10、字符串str1="无涯课堂为您服务!",使用for进行循环输出
str1="无涯课堂为您服务!" while True: for name in str1: print(name)
11、break怎么理解?举例说明
break:就是找到程序的正确结果,停止循环
如:
while True: score=int(input("输入年龄:\n")) if score<30: print("重新输入:\n") elif score>=30 and score<50: print("少年:\n") elif score>=50 and score<60: print("中年:\n") elif score>=60 and score<70: print("老人") break
12、continue怎么理解?举例说明
continue:继续,可以继续输入
while True: score=int(input("输入年龄:\n")) if score<30: print("重新输入:\n") continue(继续输入) elif score>=30 and score<50: print("少年:\n") continue elif score>=50 and score<60: print("中年:\n") continue elif score>=60 and score<70: print("老人") break
13、举例说明字符串常用的方法
(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.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'))
while True:
score=int(input("输入年龄:\n"))
if score<30:
print("重新输入:\n")
continue
elif score>=30 and score<50:
print("少年:\n")
continue
elif score>=50 and score<60:
print("中年:\n")
continue
elif score>=60 and score<70:
print("老人")
break