认真是我们
参与这个社会的方式

Python基础笔记2

1、函数的本质是地址

2、函数返回值可以当作参数使用

3、函数内部的局部变量加上global,就会引用全局

def go():
  global num
  num = 3
  print(num,id(num))

4、函数可以无限嵌套

def  Test():
    def TestX():
        print("hello world")
    TestX()

Test()

5、nonlocal

def  Test():
    num=10 #函数内部都是局部变量
    def Testin():
        #nonlocal num #内层testin,直接操作外层Test,引用外层变量,直接改变
        num=100 #没有noloacal解释为新建一个变量,否则解释为重新赋值
        print(num)
    Testin()
    print(num)
Test()

如果注释nonlocal那句,结果是100,10;如果不注释,则是100,100。

6、字符串检索

print("hello".find("el")) #find函数找到返回位置
print("hello".find("ak")) #没找到返回-1

读取-查找

import codecs  #编码
#第一个参数路径,第二个参数,rb二进制读写 第三个参数汉字编码,第十四个参数忽略错误
file=codecs.open("Z:\\balalbala\\balabala.txt","rb","gbk","ignore")

while True:
    mystr = input("输入要查询的数据")
    while True:
        linestr=file.readline()#读取一行
        if linestr.find(mystr)!=-1:
            print(linestr,end="") #显示数据
        if linestr== None: #读取失败返回值为None
            break

7、匿名函数

#lambda mystr:print(mystr)  函数 ,mystr参数,print(mystr)执行体
(   lambda mystr:print(mystr)  )("hello world")

8、tuple

python特例,(1)是int,(1,)是tuple

9、set类型,数据不可以重合

10、

in用于判断存在,not in用于判断不存在

11、列表构造表达式

mylist=[x*x   for  x  in range(10)]#列表构造表达式
print(mylist)

输出

[0,1,4,9,16,25,36,49,64,81]

12、eval()只能处理字符串或字符串表达式;exec()把文本当作语句执行。

注意转义字符,字符串中的 \”  等价于  ”

13、python中,”用于字符,””用于字符串,””” “””用于多行字符串。

提取字符串时注意,换行符也算一个字符。

14、编码问题

utf-8包含ACSII;ASCII格式,没有中文,可以编译;UTF-8格式编译中文;UTF-8空间占用较大;python3默认字符串都是utf-8 unicode (utf-8 ,unicode-8 unicode16)

r节约时间,不用输入转义字符,特殊处理,不能处理” (双引号)。一下4种打印结果一样。

print(r"C:\Users\Tsinghua-yincheng\Desktop\tools")
print(r"C:\Users\Tsinghua-yincheng\Desktop\tools")
print("C:\\Users\\Tsinghua-yincheng\\Desktop\\tools")
print(r"""C:\Users\Tsinghua-yincheng\Desktop\tools""")

utf-8两个字符表示汉字,一个字符汉字结束,3个字符;GBK两个字符表示汉字,没有结束

encode()返回一个 bytes 对象

15、判断文件是否以某个字符串结束

file= input("输入一个文件名称")
print(file.endswith(".txt")) #判断文件是否以某个字符串为结束

16、\t转换空格

mystr="1\t2\t3"  #\t转化为空格 默认8个,tabsize=1制定数量
print(mystr.expandtabs(tabsize=1))

17、find()

mystr="QQ WW EE RR TT YY UU FF GG EE"
print(mystr.find("EE")) #find返回第一个  ,从头部
#rfind返回字符串最后一次出现的位置,如果没有匹配项则返回-1。

18、index()

mystr="QQ WW EE RR TT YY UU FF GG EE"
print(mystr.index("EE")) #index查找字符串是否存在,存在返回第一个下标,不存在,异常错误
#rindex查找,返回最后一个的下标

19、字符串判断

print("hello123我".isalnum()) #判断字符串是否由数字字母组成,汉字当作字母
print("13213213213a".isdigit()) #判断字符串是否纯数字
print("advcf我".isalpha()) #判断字符串纯字母
print("abc我".islower()) #判断都是小写,#汉字没有大小写
print("ABC我".isupper()) #判断都是大写
print(" ".isspace()) #判断字符串是否纯空格组成
print("hello".istitle()) #istitle是否首字母大写且其他字母小写
print("0x109".isdecimal()) #判断进制

20、字符串间隔/填充

print("-".join("abcdefg"))
print("----".join("abc defg"))  #"abcdefg"每个字符串之间每两个字符之间插入一个字符串间隔
print(len("sadsadsad")) #求长度

for  i  in  range(5,50):
    #print("8888".rjust(i,'-')) #i长度,左边填充*   8888在右边
    print("8888".ljust(i,"*"))#i长度,右边填充*  8888在左边

21、去除空格

print(" ab c ".strip()) #前后去掉空格
print(" ab c ".rstrip()) #去掉右边
print(" ab c ".lstrip()) #去掉左边

22、字符串切割

mystr="A1021879743 # 1021879743 # www.1021879743@qq.com"
mylist=mystr.split(" # ") #切割,按照 # 返回列表
print(mylist)

mystr="88548 n小姐 女 22 162"
mylist=mystr.split(" ",2) #切割,按照 # 返回列表;参数为切割次数
print(mylist)

23、字符串模板

from string import Template #string文件导入Template
print(type(Template))
mystr=Template("hi,$name  你是  $baby")
print(mystr.substitute(name="茄子糕",baby="lovely babay"))

 

 

 

 

 

 

没有故事 也没有酒

点也没用点也没用