注释写法
#我是单行注释
'''
我是多行注释
'''
user_name = "zhangsan"
print(user_name)
import sys
#获取系统支持的最大整形
print(sys.maxsize)
i=9
print(i)
m = 0.01
n = 5e3
print('m=',m,'n=',n)#m= 0.01 n= 5000.0
x = True
y = False
print(x and y) #只有x和y同时为True时,结果为True
print(x or y) #只要x和y其中一个为True时,结果为True
print(not x) #取反,即x为True时,结果为False
print(x+y) # 当两个布尔型进行算数运算时就会转换成int类型
a = 'hello'
b = "world"
print(a,b)
输出结果为:
hello world
list1 = [1,2,3,4,'hello']
print("列表list1的第一个值:",list1[0])
print("列表list1的最后一个值:",list1[-1])
list1 = [1,2,3,4,5,6,7,8,9]
list2 = list1[2:6]
list3 = list1[-6:-3]
list1 = [1,2,3]
list2 = [4,5,6]
list3 = list1+list2
#不允许list1*list2
list4 = list1*3
print("list3=",list3)
print("list4=",list4)
输出结果如下:
list3= [1, 2, 3, 4, 5, 6]
list4= [1, 2, 3, 1, 2, 3, 1, 2, 3]
list1 = [1,2,0,4,5,6,7]
# 将索引值为2的元素的值修改为3
list1[2] = 3
print(list1)
list1[2:6]=[0,0,0,0]
print(list1)
# 输出结果为:
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 0, 0, 0, 0, 7]
list1 = [1,2,1,4,5,6,7]
# 删除列表索引为1的值
del list1[1]
print("list1=",list1)
list2= [1,2,1,4,5,6,7]
# 默认删除列表中的最后一个值
list2.pop()
print("list2=",list2)
# 删除列表索引为1的值
list2.pop(1)
print("list2=",list2)
list3 = [1,2,1,4,5,6,7]
# 删除列表中值为1的元素,如果列表中有多个,则默认删除第一个
list3.remove(1)
print("list3=",list3)
list4 = [1,2,1,4,5,6,7]
# 清空列表
list4.clear()
print("list4=",list4)
list1 = [1,2,3,4,5,6,7]
list1.append(8)
list1.append([9,10])
print("list1 =",list1)
list2 = [1,2,4,5,6,7]
#在列表索引值为2的位置,增加元素3
list2.insert(2,3)
print("list2 =",list2)
list3 = [1,2,3]
#对当前列表元素后面进行批量增加
list3.extend([4,5,6])
print("list3 =",list3)
list1 = [1,2,3,4,5,6]
print(list1.index(4,1,5)) # 查找到了索引位置为3
print(list1.index(5)) # 查找到了索引位置为4
list1 = [1,2]
list2 = [1,2,3]
list3= [list1,list2]
print(list3) #[[1, 2], [1, 2, 3]]
tup1 = (1,2,3,4,'hello')
print(tup1)
#(1, 2, 3, 4, 'hello')
# 元组可以包涵多种数据类型
tup1 = (1,2,3,4,'hello')
# 当元组只有一个元素时,需要在元素后面加一个逗号
tup2 = ("world",)
# 给元组进行+ 运算,从新生成一个新的元组
tup3 = tup1 + tup2
print(tup3)
tup1 = (1,2,3,4,'hello')
del tup1
print(tup1)
#再次使用删除后的元组,会报下面的错误
Traceback (most recent call last):
File C:\Users\zhanggang\Desktop\python\basic\test\Test2.py, line 3, in <module>
print(tup1)
NameError: name 'tup1' is not defined
list1 =[1,2,3,45]
tuple1 = (91,2,3,4)
#元组转列表
list2 = list(tuple1)
#列表转元组
tuple2 = tuple(list1)
print(list2) #[91, 2, 3, 4]
print(tuple2) #(1, 2, 3, 45)
stu = {"name":"zhangsan","age":18,"sex":"男"}
print(stu)
stu = {"name":"zhangsan","age":18,"sex":"男"}
print(stu["name"],stu["age"])
print(stu.get("name"),stu.get("age"))
stu = {"name":"zhangsan","age":18,"sex":"男"}
stu["age"] ="20"
print(stu)
stu = {"name":"zhangsan","age":18,"sex":"男"}
del stu["age"]
print(stu)
stu = {"name":"zhangsan","age":18,"sex":"男"}
stu["county"] = "China"
print(stu)
set1 = {1,2,3,5,"123",1,2,3}
print(set1)
#{1, 2, 3, 5, '123'}
#元素和集合只有属于或不属于的关系。
set1 = {1,2,3,5,"123",1,2,3}
print(set1)
print(1 in set1) #True
print( 1 not in set1) #False
set1 = {1,2,3,5,"123",1,2,3}
#添加一个元素
set1.add(23)
print("ste1=",set1)
set2 = {1,2,3,4}
#同时添加多个元素
set2.update({5,6,7,8})
print("ste2=",set2)
set1 = {1,2,3,5,"123"}
set1.remove(1)
print(set1)
set1 ={1,2,3,4}
# set2是一个不可变集合,若对其进行修改,添加,则会报错
set2 = frozenset(set1)