python入门学习笔记(2)
文章目录
一、数据类型
python3中主要有6种标准数据类型:
Number(数字)
String(字符串)
List(列表)
Tuple(元组)
Set(集合)
Dictionary(字典)
提示:type() 可以用来查看变量的数据类型
type( ) #用来查看变量的数据类型 ,()里放变量
1.Number(数字)
int(整形):123 456 12 56
float(浮点型):9.32 6.54 3.64 7.12
bool(布尔类型):True和bool (在python3中bool是int的子类,Ture和False这两个布尔类型可以参与数字运算。 Ture转换为1 False转换为0)
num = 123 # 整型
float = 9.43 # 浮点型
bool1 = True # 布尔类型
bool2 = False # 布尔类型
print(num,float,bool1,bool2) #123 9.43 True False
print(type(num)) # <class 'int'>
print(type(float)) # <class 'float'>
print(type(bool1)) # <class 'bool'>
print(type(bool2)) # <class 'bool'>
2.String(字符串)
符串是由数字 字母 下划线 汉字等 组成的一串字符。 一般使用 ‘’ 或者 “” 或者 ‘’‘文本’‘’ 或者 “”“文本”“”
str1 = "hello"
str2 = 'world'
str3 = '''
床前明月光
疑是地上霜
'''
str4 = """
举头望明月
低头思故乡
"""
print(str1,str2,str3,str4)
print(type(str1)) # <class 'str'>
print(type(str2)) # <class 'str'>
print(type(str3)) # <class 'str'>
print(type(str4)) # <class 'str'>
3.List(列表)
列表使用[]来定义,他支持数字 字符串 甚至还可以是列表(嵌套) (保存多个数据)
list1 = ['王楚然','章若楠','张静怡','庄达菲']
print(list1) # ['王楚然','章若楠','张静怡','庄达菲']
print(type(list1)) # <class 'list'>
list中可以存放不同的数据类型
list2 = [12,34,56,5.32,True,False,'hello']
print(list2) # [12, 34, 56, 5.32, True, False, 'hello']
list中可以嵌套list
list3 = [87,39,9.43,True,[45,48,8.43,False]]
print(list3) # [87, 39, 9.43, True, [45, 48, 8.43, False]]
4.Touple(元组)
元组使用()来定义,他支持 数字 字符串 还可以嵌套元组。 类似于列表(嵌套不去演示)
touple = (12,34343,4.43,'world',True)
print(touple) # (12, 34343, 4.43, 'world', True)
5.Set(集合)
表示集合类型 集合使用 {} 来定义,里面只有值,没有索引.他支持 数字 字符串。
set1 = {123,54,6.54,True,False,'apple'}
print(set1) # {False, True, 6.54, 54, 'apple', 123}
6.Dictionary(字典)
表示字典类型。字典使用 {} 来定义。字典是由key和value组成。
集合没有key只有value,字典有key也有value
{key1:value1,key2:value2,key3:value3,......}
dict1 = {'name':'张三','age':18,'height':178}
print(dict1) # {'name': '张三', 'age': 18, 'height': 178}
二、数据转换
因为在python中不同的数据类型在进行运算时,运算规则不一样,所以需要进行数据类型转换
1.int(x): 将其他数据类型转化为整形
参数:
第一个参数: x表示转换的数据,
第二个参数:base表示的是进制数,默认值是10,base表示将前面的参数x当做几进制进行转换。
age = input('靓仔,请输入你的年龄:')
print(age)
# 需求:age 输入18,将age变量的值 加10
print(age + 10) # TypeError: can only concatenate str (not "int") to str 会产生错误
在python中,字符串类型和整形(数字类型)不能直接进行数学运算。此时就需要将
print(type(age)) # <class 'str'>
print(int(age)) #18
print(int(age) + 10) #28
int()函数中base参数的介绍
str1 = "12"
print(int(str1)) # 12
print(int(str1,base=10)) # 12
print(int(str1,base=16)) # 18将字符串“12”转换为整形,并将以16进制显示
2.str(x): 将其他数据类型转换为字符串
参数:x表示要转换的数据。
num = 12
num1 = 87
print(num + num1) # 99
print(type(num)) # <class 'int'>
print(type(num1)) #<class 'int'>
# 以上是int类型的加法
# 在python中的字符串类型,使用 + 表示将字符串进行拼接。
str1 = str(num)
str2 = str(num1)
print(type(str1)) # <class 'str'>
print(type(str2)) # <class 'str'>
print(str1 + str2) # 1287
3.float(x): 将其他数据类型转换为浮点型
参数:表示将要转换的数据。
float1 = "12.34"
print(type(float1)) # <class 'str'>
print(float1 + 10) # TypeError: can only concatenate str (not "int") to str
float2 = float(float1)
print(type(float2)) # <class 'float'>
print(float2 + 10) # 22.34
注:有些不能使用float函数转换
float3 = "hello123.34"
print(float(float3)) # ValueError: could not convert string to float: 'hello123.34'
4.bool(x):将其他数据类型转换为布尔类型。如果没有参数,返回False
参数:x 表示要转换的数据。
python中布尔类型主要有两个:Ture 和 False
num1 = 100
float1 = 9.54
str1 = "hello"
list1 = [23,54.53,'apple']
tuple1 = (12.43,56,74,'pig')
dict1 = {'name':'hanhao','age':19}
set1 = {12,43,45.4}
print(bool(num1)) # True
print(bool(float1)) # True
print(bool(str1)) # True
print(bool(list1)) # True
print(bool(tuple1)) # True
print(bool(dict1)) # True
print(bool(set1)) # True
当字符串中有空格,表示该字符串不是空字符串。
num2 = 0
float2 = 0.0
str2 = ''
list2 = []
tuple2 = ()
dict2 = {}
none1 = None
str3 = ' ' #当字符串中有空格,表示该字符串不是空字符串。
print(bool(num2)) # False
print(bool(float2)) # False
print(bool(str2)) # False
print(bool(list2)) # False
print(bool(tuple2)) # False
print(bool(dict2)) # False
print(bool(none1)) # False
综上所述:在python中,0、0.0、空字符集、空列表、空元组、空字典、None、空集合 这些数据转换为bool类型全部为False。
5.隐式类型转换:主要用于 if if…else 等条件表达式中
print(3>2) # Ture
print(98<87) # False
if 3>2:
print('hello world') # hello world
if 98<87:
print('dog') # 不输出