python数据类型
数字
字符串
列表
元祖
集合
字典
数字类型
整数
浮点数
变量
命名:字母、数字、下划线,首字母不能是数字
变量命名有意义
# 定义一个变量
first_number = 1
second_number = 1.2
print(first_number)
print(type(first_number))
print(second_number)
print(type(second_number))
#布尔变量 True False
third_bool = False
print(third_bool)
print(type(first_number))
print 打印
print(type()) 打印变量类型
运算符
#基本运算
# + - * /
first_number = 12
second_number = 9
result = first_number + second_number
print(result)
first_number = 12
second_number = 9
result = first_number - second_number
print(result)
first_number = 12
second_number = 9
result = first_number * second_number
print(result)
first_number = 12
second_number = 9
result = first_number / second_number
print(result)
注:python除法运算所得结果必定为浮点类型
first_number = 12
second_number = 4
result = first_number / second_number
print(result)
print(type(result))
3.0
<class 'float'>
#取模运算
i= 10
j = 3
z = i % j
print(z)
#幂运算
z = i ** j
print(z)
#取整除
z = i // j
print(z)
print(type(z))
1
1000
3
<class 'int'>
#赋值运算符
# = +=
i = 5
i += 1
print(i)
i -= 4
print(i)
6
2
#进制运算
#二进制、八进制、十进制、十六进制、十二进制(时钟)
#十进制转化二进制
i = 16
j = bin(i)
print(j)
#十进制转八进制
i=16
j=oct(i)
print(j)
#十进制转十六进制
i=16
j=hex(i)
print(j)
#二进制转十进制
i="10"
j=int(i,2)
print(j)
#八进制转十进制
i="10"
j=int(i,8)
print(j)
#十六进制转十进制
i="10"
j=int(i,16)
print(j)
0b10000
0o20
0x10
2
8
16
#位运算 & | ^ ~ >>
i = 11
j = 2
z = i & j
print(z)
z = i | j
print(z)
z = i ^ j
print(z)
z = ~i
print(bin(i))
print(bin(z))
print(z)
i=11
z = i<<2
print(z)
i=11
z=i>>2
print(z)
2
11
9
0b1011
-0b1100
-12
44
2
条件判断
'''
注释 ''' '''
""" """
#
'''
if_see = input("请输入数字")
if if_see:1
print("买两个包子")
else:
print("买一个包子")
输出
请输入数字1
买两个包子
字符串
#声明一个字符串
#单引号
s='hello'
print(s)
#双引号 与单引号无区别
s = "hello python"
print(s)
#三引号
s = """hell0
python
"""
print(s)
hello
hello
hell0
python
#字符串操作
s = "hello python"
print(s[0])
print(s[1])
h
e
#访问字符串中的子字符串 切片操作
print(s[2:7])
llo p
#字符串相加运算
s1="hello"
s2=" python"
print(s1+s2)
hello python
#字符串更新
s1="hello world"
s2="python"
print(s1[:6]+s2)
hello python
#字符串包含运算
s1 = 'hello'
s2 = 'll'
print(s1 in s2)
print(s2 in s1)
#不包含运算
print(s1 not in s2)
False
True
True
#转义字符
print("\'\"")
print("hello\npython")
print("hello\tpython")
print("hello\rpyt")
#原始字符串输出
print(r"hello\rpython")
print(R"hello\npython")
'"
hello
python
hello python
pyt
hello\rpython
hello\npython
#字符串格式化输出
s1 = 'hello'
i = 12
print("%s -- %d"%(s1,i))
hello -- 12
#字符串内建函数
#字符串查找
#显示查找到的位置
s = "hello python".find('l')
print(s)
2
#大小写转换
print("HellO".lower())
print("HellO".upper())
hello
HELLO
#返回字符串长度
print("hello python".__len__())
12
#判断字符串是否只包含空格
print(" ".isspace())
True
#字符串替换
print("hello python".replace("o","aa"))
hellaa pythaan
列表
'''
列表:一组数据
list是有序序列
序列中的每一个元素分配一个数字,也是索引
从0开始
'''
list1 = ["xiaoming",12,"xiaohua",13,"xiaoxiao",11]
print(list1)
print(type(list1))
['xiaoming', 12, 'xiaohua', 13, 'xiaoxiao', 11]
<class 'list'>
#访问列表
print(list1[1])
print(list1[2:])
print(list1[1:3])
12
['xiaohua', 13, 'xiaoxiao', 11]
[12, 'xiaohua']
#列表更新
list1[1] = 15
print(list1)
['xiaoming', 15, 'xiaohua', 13, 'xiaoxiao', 11]
#列表添加
list1.append("xiaohong")
list1.append(13)
print(list1)
['xiaoming', 15, 'xiaohua', 13, 'xiaoxiao', 11, 'xiaohong', 13]
#列表删除
del list1[2]
del list1[3]
print(list1)
['xiaoming', 15, 13, 11, 'xiaohong', 13]
#嵌套列表
list1 = [["xiaoming",12],["xiaohua",13],["xiaoxiao",11]]
print(list1)
print(list1[1])
[['xiaoming', 12], ['xiaohua', 13], ['xiaoxiao', 11]]
['xiaohua', 13]
#计算列表元素个数
count = len(list1)
print(count)
print(len(list1[1]))
3
2
#移除列表元素,并返回移除元素
l = list1.pop(1)
print(l)
print(list1)
['xiaohua', 13]
[['xiaoming', 12], ['xiaoxiao', 11]]
#列表排序
list1 = [12,34,11,42,33,25]
list1.sort()
print(list1)
[11, 12, 25, 33, 34, 42]
#查找列表中匹配第一个元素的索引值
i = list1.index(33)
print(i)
3
元祖
'''
元祖:一个小括号内包含
元祖不可修改
'''
list1 = (12,14,51,33)
print(list1)
print(list1[2:])
(12, 14, 51, 33)
(51, 33)
集合
'''
集合:无序的不重复的序列
声明方式
set{}
{}
'''
set_p = {12,44,245,51,12,343}
print(set_p)
{12, 44, 51, 245, 343}
#判断元素是否在集合
print(13 in set_p)
print(12 in set_p)
False
True
#两个集合间的运算
a = set('abcdef')
b = set("abcxyz")
print(a&b)
print(a|b)
print(a^b)
{'a', 'c', 'b'}
{'x', 'f', 'd', 'c', 'y', 'b', 'a', 'e', 'z'}
{'x', 'f', 'y', 'e', 'd', 'z'}
#集合添加元素
a = {12,43,55,24}
a.add(30)
print(a)
{43, 12, 55, 24, 30}
#移除指定元素
a = {12,44,64,23,64,22,52}
a.remove(22)
print(a)
{64, 12, 44, 52, 23}
#随机移除元素
pop_s = a.pop()
print(a)
{12, 44, 52, 23}
#计算集合元素个数
print(len(a))
4
#清空集合
a.clear();
print(a)
set()
字典
'''
字典是一种可变容器类型可以存储任意类型变量
'''
#定义
a = {"小米":1000,"vivo":2000,"华为":3000}
print(a)
#访问
key = a.keys()
print(key)
print(a['vivo'])
#添加
a['苹果'] = 4000
print(a)
#更新
a["小米"] = 1500
print(a)
#删除
del a["苹果"]
print(a)
#函数操作
#判断键值是否在字典
i = '小米' in a
print(i)
#清除
a.clear()
print(a)
dict_keys(['小米', 'vivo', '华为'])
2000
{'小米': 1000, 'vivo': 2000, '华为': 3000, '苹果': 4000}
{'小米': 1500, 'vivo': 2000, '华为': 3000, '苹果': 4000}
{'小米': 1500, 'vivo': 2000, '华为': 3000}
True
{}
条件语句
'''
条件语句
1、比较运算符
== >= <= > < !=
'''
i = 'a'
j = 'n'
if i<j:
print("j大")
else:
print("j小")
'''
ascii表
'''
print(ord(i))
print(ord(j))
print(chr(100))
j大
97
110
d
成员运算符
'''
成员运算符
in 、 not in
'''
str1 = 'abcdef'
str2 = "fighj"
if 'f' in str1:
if 'f' in str2:
print("s在str1 和str2中")
if 't' not in str1:
print("'t'不在str1中")
#and 并列
if ('f' in str1) and ('f' in str2):
print("f在str1和str2中")
elif 'f' in str1:
print("f在str1中")
elif 'f' in str2:
print("f在str2中")
else:
print("f不在str1和str2中")
s在str1 和str2中
't'不在str1中
f在str1和str2中
真假值判断
'''
1地表true、0代表false
and需要判断到最后一个,只有全部为真时才会返回真
or只需要判断到真就停止,后面不会判断
'''
print(True and False)
print(True or False)
print(not True)
print(1 and 0)
print(1 or 0)
print(not 1)
print(3 and 4 and 5)
print(3 or 4 or 5)
False
True
False
0
1
False
5
3
身份运算符
'''
身份运算符
is 、 is not
is 判断地址是否一致 == 判断值是否相等
'''
i = 1
j = 1
print(i == j)
print(i is j)
print(id(i))
print(id(j))
list1 = {22,33,44}
list2 = {22,33,44}
print(list1 == list2)
print(list1 is list2)
print(id(list1))
print(id(list2))
1630364848
1630364848
True
False
3710408
3709088
循环
'''
for while
'''
i = 1
count = 0
while i <11:
count += i
i += 1
print(count)
i=0
count = 0
for i in range(101):
count += i
print(count)
55
5050
中断循环
'''
中断循环
break continue
'''
i = 1
count = 0
while i <101:
if i==50:
i+=1
break
count += i
i += 1
print(count)
i = 1
count = 0
while i <101:
if i==50:
i+=1
continue
count += i
i += 1
print(count)
while True:
print("循环1")
while True:
print("跳出循环2")
break
print("跳出循环1")
break;
1225
5000
循环1
跳出循环2
跳出循环1