python学习笔记
1.统计函数库 -------collections ----调函数Counter
a = [1,2,3,4,5,6,7,2,3,4,5,5]-----list列表
print (a[-2])
from collections import Counter------统计列表里重复的数据个数
print(Counter(a))-------------------打印数据个数
print(Counter(a).most_common(2))-----打印出最多的两个元素的数据
结果:
2.输入函数---------------input()
a=input()-------输入保存到a内
print("my name is:",a)
print("my name is:",a)
3.整数相除输出小数
print(1/float(2))
print(float(1)/2)
print(1./2)
print(float(1)/2)
print(1./2)
结果:
0.5
0.5
0.5
4.转义字符“\”
print("hell\'o,world!!")
当输出字符串中有非法字符时,利用转义字符 可以正常输出
hell'o,world!!
5.计算字符串拼接后长度
print ("hello"+"world")
a="hello"+" "+"world"
lenth=len (a)
print(lenth)
a="hello"+" "+"world"
lenth=len (a)
print(lenth)
结果:
helloworld
11
6.数据类型转换
print(str(123)+"234")
结果:输出字符串
123234
7.字符串切片
a="hello"
b=a[2:5]
print (b)
b=a[2:5]
print (b)
结果:
llo
8.字符串切片第三个参数:设置步长
a="helloworld"
c=a[::2]-------正序切片
d=a[::-2]------倒序切片
print(c)
结果:间隔两个字符输出一个字符
hlool
9.list使用-----增删改查
a=[1,2,3,4,5,6]
a.append("2")----向list的末尾添加字符串2
a.insert(0,"3")----向list的第0号位置上添加字符串3
a.append("2")----向list的末尾添加字符串2
a.insert(0,"3")----向list的第0号位置上添加字符串3
a[0]=15-----------list更改
print(a)
print(a)
del a[0]-----------删除list的第0号元素
print(a)
print(a)
结果:
['3', 1, 2, 3, 4, 5, 6, '2']
[1, 2, 3, 4, 5, 6, '2']
10.元组使用---无法修改元组内的元素,只读型
作用:读性能比list高;
a=(1,2,3,4)
a[0]=5
print(a)
a[0]=5
print(a)
11.字典使用----增删改查
dic = {'lihei':'1424523', 'lihong':'22433534'}
print (dic)
dic ["lucy"]=12312424
print (dic)
dic["lihei"]=22242342
print(dic)
del dic['lihei']
print(dic)
print(dic['lihei'])
print (dic)
dic ["lucy"]=12312424
print (dic)
dic["lihei"]=22242342
print(dic)
del dic['lihei']
print(dic)
print(dic['lihei'])
12.==,!=,and,or与not使用
print( 1+1==2 and 1+1==3)
print (1+1==2 and 1+1!=3)
print (1+1!=2 and 1+1==3)
print (1+1==2 or 1+1==3)
print (1+1==2 or 1+1!=3)
print (1+1!=2 or 1+1==3)
print ( not 1+1==2)
print (not 1+1!=2)
print (1+1==2 and 1+1!=3)
print (1+1!=2 and 1+1==3)
print (1+1==2 or 1+1==3)
print (1+1==2 or 1+1!=3)
print (1+1!=2 or 1+1==3)
print ( not 1+1==2)
print (not 1+1!=2)
结果:
False
True
False
True
True
False
False
True
12.判断list里元素是否存在
a=[1,2,3,4]
print (1 in (a))
print (1 in (a))
结果:
True
13.if ...elseif 使用
light = "yellow"
if light=="red":
print ("stop")
elif light =="green":
print("go")
elif light =="yellow":
print ("slow down")
if light=="red":
print ("stop")
elif light =="green":
print("go")
elif light =="yellow":
print ("slow down")
结果:
slow down
14.while----循环使用
sum = 0
i = 1
while i<=100:
sum=sum +i
i=i+1
print (sum)
i = 1
while i<=100:
sum=sum +i
i=i+1
print (sum)
结果:
5050
15.字典循环遍历使用
dic = {"lihei":1424523, "lihong":22433534}
for i in dic:
print ((i).dic[i])
for i in dic:
print ((i).dic[i])
16.break,contn
a=[1,2,3,4,-1,5,6]
for i in a:
if i<0:
break--------跳出循环
for i in a:
if i<0:
break--------跳出循环
continue----跳过小于零继续执行
print (i)
print (i)
break结果:
1
2
3
4
continue
1
2
3
4
5
6
17.操作打包--函数
a=[1,2,3,4,-1,5,6]
def addsum(num):
sum=0
for i in a:
sum=sum +i
return (sum)
print (addsum(a))
def addsum(num):
sum=0
for i in a:
sum=sum +i
return (sum)
print (addsum(a))
18.函数作用域
------函数局部变量x无法影响全局变量x
x=1
def change():
def change():
18.函数打包-
import math
print (math.pi)
dir (math)---查询函数库提供所有功能
x=10
print ("change:",x)
change()
print(x)
print (math.pi)
dir (math)---查询函数库提供所有功能
x=10
print ("change:",x)
change()
print(x)
结果:
change: 10
1
-------函数局部变量x影响全局变量x
在函数体中定义:global x
x=1
def change():
global x
x=10
print ("change:",x)
change()
print(x)
def change():
global x
x=10
print ("change:",x)
change()
print(x)
结果:
change: 10
10
19.python---共享内存,并非复制;
a=[1,2,3]
b=a
b[0]=0
a[-1]=5
print (a)
print (b)
b=a
b[0]=0
a[-1]=5
print (a)
print (b)
结果:
[0, 2, 5]
[0, 2, 5]
20.list列表推导
a=[1,2,3,4,5,6,7,8]
b=[]
for i in a:
if i%2 == 0:
b.append(i)
print (b)
c=[i for i in a if i%2==0]
print (c)
b=[]
for i in a:
if i%2 == 0:
b.append(i)
print (b)
c=[i for i in a if i%2==0]
print (c)
结果:
[2, 4, 6, 8]
[2, 4, 6, 8]
21.判断数据在1-10之间
if 1<a<10即可;
22.for...else使用
a=[1,2,1,3,4,-1,5,6,]
for i in a:
if i<0:
break-------遍历到负数就推出
print (i)
else:
print ("edele sj :")
for i in a:
if i<0:
break-------遍历到负数就推出
print (i)
else:
print ("edele sj :")
结果:
1
2
1
3
4
23.集合操作
a={1,2,3,4}
b={4,5,6}
print (a|b)
print (a&b)
b={4,5,6}
print (a|b)
print (a&b)
结果:
{1, 2, 3, 4, 5, 6}
{4}
大悲无泪--大悟无言--大笑无声