条件判断
x=input("输入一个整数")
x=int(x)
if x%2==0:
print("偶数")
elif x%2!=0:
print("奇数")
else:
print("wrong")
条件表达式
a=int(input("输入整数"))
b=int(input("输入整数"))
print((a,'大于等于',b) if a>=b else (a,"小于",b))
pass 占位符 无实际意义
range函数
r=range(10)
print(list(r))
print(r)
r2=range(2,10)
print(list(r2))
r3=range(2,10,2)
print(list(r3))
结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(0, 10)
[2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
不管range对象存储的序列有多长,占用的内存空间都是相等的,只有在用到range对象时才会计算序列中的相关元素
while循环(Python中每行代码前空格的长短会影响结果!!!)
a=1
sum=0
while a<=100:
if a%2==0:
sum=sum+a
a=a+1
print(sum)
for-in循环 (只能遍历迭代对象 如字符串或range对象)
for item in '人生苦短,我用python':
print(item)
r=range(20)
for item in r:
print(item)
for _ in r:
print('人生苦短,我用python')
练习 for-in写出100-999之间的水仙花数
for item in range(100,1000):
a=item%10
temp=item//10
c=temp%10
b=item//100
if a**3+b**3+c**3==item:
print(item)
for-else语句
for item in range(3):
numb=int(input("请输入密码"))
if numb==9999:
print("密码正确")
break
else:
print("错误")
else:
print("三次机会已用完")
while-else语句
输出99乘法表
for i in range(1,10):
for j in range(1,i+1):
temp=i*j
print(str(i)+'*'+str(j)+'='+str(temp),end='\t') //end='\t'不换行输出
print('\n')
列表(数组) :元素可以重复,有两组下标(见下图),混存不同的数据类型元素,动态分配和回收内存
两种创建方式
ls2=['sls',658]
ls1=list(['hello',65,45.2])
print(ls1)
查找指定元素下标 index
lst=['hello','word',98,'hello']
numb=lst.index('hello')
numb2=lst.index('hello',1,4)
print(numb,numb2)
结果:
0 3
列表切片,切片的结果是愿列表部分的拷贝
lst1=[10,20,30,40,50,60]
lst2=lst1[1:4:1]
print(lst2,id(lst1),id(lst2))
切片步长为负数
lst1=[10,20,30,40,50,60]
lst2=lst1[5:0:-1]
print(lst2)
for-in遍历列表
lst1=[10,20,30,40,50,60]
for item in lst1:
print(item)
列表添加
append末尾添加一个元素 extend末尾添加至少一个元素 比较:
list1=[20,30,40]
list1.append(50)
print(list1)
list2=['ear','breath']
list1.append(list2)
print(list1)
[20, 30, 40, 50]
[20, 30, 40, 50, ['ear', 'breath']]
list1.extend(list2)
print(list1)
[20, 30, 40, 50, 'ear', 'breath']
insert任意位置添加
list1=[20,30,40]
key=list1[2]
print(key)
list1.insert(1,90)
print(list1)
40
[20, 90, 30, 40]
切片添加
list1=[20,30,40,50,60]
list2=[70,80,90]
list1[1:]=list2
print(list1)
[20, 70, 80, 90]
list1=[20,30,40,50,60]
list2=[70,80,90]
list1=list2
print(list1)
[70, 80, 90]
remove 移除一个指定元素 pop按下标删除 切片删除[] clear清空数组 del销毁数组
list1=[20,30,40,50,60]
list1.remove(30)
print(list1)
list1=[20,30,40,50,60]
list1.pop(1)
print(list1)
list1=[20,30,40,50,60]
list1[1:3]=[]
print(list1)
[20, 50, 60]
list1=[20,30,40,50,60]
list1.clear()
print(list1)
[]
del list1
按下标修改,单个or批量
切片修改
list1=[20,30,40,50,60]
list1[1:3]=[70,80,90]
[20, 70, 80, 90, 50, 60]
单个修改
list1[0]=100
列表排序,sort将原列表排序,内置函数sorted则会产生新的列表
lst=[10,30,20,50,60,25]
lst.sort()
print(lst)
[10, 20, 25, 30, 50, 60]
lst=[10,30,20,50,60,25]
new_lst=sorted(lst)
print(new_lst,lst)
[10, 20, 25, 30, 50, 60] [10, 30, 20, 50, 60, 25]
逆序:
lst=[10,30,20,50,60,25]
new_lst=sorted(lst,reverse=True)
print(new_lst,lst)
[60, 50, 30, 25, 20, 10] [10, 30, 20, 50, 60, 25]
lst=[10,30,20,50,60,25]
lst.sort(reverse=True)
print(lst)
[60, 50, 30, 25, 20, 10]
列表生成式
lst=[i for i in range(1,10)]
print(lst)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
lst=[i*i for i in range(1,10)]
print(lst)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
字典(类似HashMap)
三种创建方式
dict1={80:40,'two':50}
print(dict1)
dict2=dict(two=60)
print(dict2)
d={}
print(d)
{80: 40, 'two': 50}
{'two': 60}
{}
取值
dict1={80:40,'two':50,'three':60}
print(dict1)
print(dict1['two'])
print(dict1.get('three'))
字典的增删改查,判断key值是否存在于字典中
dict1={80:40,'two':50,'three':60}
print('three' in dict1)
del dict1[80]
print(dict1)
dict1['four']=70
print(dict1)
dict1['four']=80
print(dict1)
dict1.clear()
print(dict1)
{'two': 50, 'three': 60}
true
{'two': 50, 'three': 60, 'four': 70}
{'two': 50, 'three': 60, 'four': 80}
{}
查询字典中的所有key,value
dict1={80:40,'two':50,'three':60}
keys=dict1.keys()
values=dict1.values()
items=dict1.items()
print(keys,values,items)
dict_keys([80, 'two', 'three']) dict_values([40, 50, 60]) dict_items([(80, 40), ('two', 50), ('three', 60)])
遍历
dict1={80:40,'two':50,'three':60}
for item in dict1:
print(item,dict1[item],dict1.get(item))
80 40 40
two 50 50
three 60 60
字典中的key不允许重复,value可以重复,字典中的元素是无序存储的,字典的key必须是不可变对象,字典可以根据需要动态伸缩,字典会浪费较大内存,是一种用空间换时间的数据结构
字典生成式
keys=['one','two','three']
values=[10,20,30]
dict1={keys.upper():values for keys,values in zip(keys,values)}
print(dict1)
{'ONE': 10, 'TWO': 20, 'THREE': 30}
元祖
创建元祖的几种方式(元祖中只有一个元素时后面要加,)
t1=('one','two',10)
print(t1)
t2='one','two',10
print(t2)
t3=tuple(("one",'two',10))
print(t3)
t4=('one',)
print(t4)
t5=()
t6=tuple()
print(t5,t6)
('one', 'two', 10)
('one', 'two', 10)
('one', 'two', 10)
('one',)
() ()
元组,字符串都是不可变序列!
获取元组元素
t1=('one','two',10)
print(t1[1])
for item in t1:
print(item)
two
one
two
10
集合,两种创建方式,set方法可以把别的容器,例如列表,元组,字符串之类的转换成集合
集合内的元素无序且不允许重复,类似于只存储key的字典,用hash函数计算存储位置
s1={'one','two',3,4}
s2=set([10,20,30])
s3=set(range(6))
s5=set()
print(s1,s2,s3,s5)
{'two', 'one', 3, 4} {10, 20, 30} {0, 1, 2, 3, 4, 5} set()