python学习笔记 变量和内置函数

开发工具pycharm 系统windows 
a=[1,
   2,3,
   4]
print(a)

if True:
          print(1)
else:
       print(0)

'''input("dsada")'''

'''复数'''
cc=complex(1,2)
print(cc)

'''Tupple类型'''
t=(100,"tom",12,89.09)
print(t[0])

'''D字典类型'''
dicts={}
dicts["key1"]="wuyue"
dicts["key2"]="caohao"
print(dicts["key1"])
print(dicts.keys())
print(dicts.values())
cc=dicts.keys()
'''print(cc)'''

'''类型转换'''
print(int("200"));
'''4进制转换'''
print(int("10",4 ))
'''2进制转换'''
print(int("10",2))

float("15.5")
str(100)

'''repr 对象以字符串形式输出'''
lists=["aa",1,2]
repr(lists)

'''用于求值计算。字符串可计算,输出250,而不是100150'''
print(eval("100+150"))

'''序列转元组,结果为 ('2','3','4') '''
print(tuple("234"))

'''元组转为列表 结果为['22', 'wuyue', 1]'''
tupples=("22","wuyue",1)
print(list(tupples))

'''转为可变集合,结果为{1, 'wuyue', '22'}。但set不记录元素位置或者插入点。因此,sets不支持 indexing '''
print(set(tupples))

'''创建一个字典  结果为{'one': 100, 'tow': 200, 'three': 300}'''
print(dict(one=100,tow=200,three=300))

'''冻结对象,与set()不同的是,对象不可变'''
frozenset()

'''一个整数转为一个字符 结果为d  '''
print(chr(100))

'''转为字符转证书 结果为97'''
print( ord('a'))
'''运算符系列'''
'''2的平方 2的三次方'''
print(2**3)

'''结果为False'''
print(not True)


age=22
if age<20 or age>70:
    print('不是壮年')
    print('打印成功')
else:
    print('壮年')
print('das')

'''if age=20 and age<70: 并且'''

if not age<20:
    print('年龄不小于20')

test1=1
'''test1的值是否在数组a中'''
test2=test1 in a
print(test2)

test1=2220

print(test1 not in a)
test3=[1,2,3,4]
print(test3 is a)
print(test3 is not a)

'''if elif else'''
if age<20:
    print(age,'小孩子')
elif age>20 and age<50:
    print(age,'青年了')
else:
    print(age,'老年了')


'''while循环'''
ntest=1
while ntest<=10:
    ntest+=1
    print(ntest)

'''while循环 九九乘法表'''
#
# rowindex=1
# '''  end=''时不换行    '''
# while rowindex<=9:
#     cindex = 1
#     while cindex<=rowindex:
#         if cindex==rowindex:
#             print(str(cindex)+'x'+str(rowindex)+'='+str(cindex*rowindex)+'	',)
#         else:
#             print(str(cindex)+'x'+str(rowindex)+'='+str(cindex*rowindex)+'	',end='')
#         cindex+=1
#
#     rowindex+=1


'''for 九九乘法表'''
# forrows=[1,2,3,4,5,6,7,8,9]
# forcols=[1,2,3,4,5,6,7,8,9]
#
# rindexs,cindexs=1,1
# for rindexs in forrows:
#     for cindexs in forcols:
#         if cindexs<rindexs:
#             print(str(cindexs)+'x'+str(rindexs)+'='+str(cindexs*rindexs)+'	',end='')
#         if cindexs==rindexs:
#             print(str(cindexs) + 'x' + str(rindexs) + '=' + str(cindexs * rindexs) + '	')


print(100//3)

'''公鸡 5元/只 母鸡3/只 小鸡1元/3只。100块钱刚好买100只鸡,怎么实现'''


'''python 元组'''
tup1=('yuan','zu',1997)
tup2=(1,2,3,4,5)
tup3="a","b","c"
tup4=1,2,3,4,5
tup5=()
tup6=( 1,)
print(tup2[1:2])
print(tup2[1:-1])
print(tup2[-2])

print(len(tup2))
print(3 in tup2)
for xtup in tup2:
    print(xtup)

'''不是元组'''
tup7=(1)

'''字符串'''
str="hello world"
print(str[1:3])

'''字符串引号前加R或者r,则字符串内转义符也被当作字符串内容输出 ,例 输出 hello world 	 abc    '''
str1=R"hello world 	 abc"
print(str1)


'''python函数/自定义函数'''
def myhanshu():
    print("hello")

myhanshu()


def testadd(praa,prab):
    return praa+prab

defresult=testadd(2,6)

defresult1=testadd(prab=6,praa=2)

'''参数可变长 加*星号 '''
def outs(*outsa):
    for dexx in outsa:
        print(dexx)

outs(1,2,3,4)

'''lamada 表达式'''
lasum=lambda laa,lab:laa+lab
print(lasum(1,2))


新建了一个python文件
MyPython2
'''from MyPython import  *  导入MyPython.py文件中的所有函数'''
'''导入MyPython.py文件中的myhanshu函数'''
from MyPython import myhanshu
myhanshu()

MyPython2

'''
from MyPython import * 导入MyPython.py文件中的所有函数''' '''导入MyPython.py文件中的myhanshu函数''' # from MyPython import myhanshu # myhanshu() # # # v='a:b:c' # # print(v[2]) # import MyPython # # MyPython.myhanshu() #files=open("e:/Python/filetest.txt") # # print(files.name) # # print( files.readline()) # files.close() # # files=open("e:/Python/filetest.txt","a+") # files.write("caohao2 ") # print( files.readline()) # files.close() #读取整个文件 #files=open("e:/Python/filetest.txt") #print(files.readlines()) #定位读取,例从第二个字符之后读取 (字符也包括,例如换行符 等) #files.seek(6) # print(files.readline()) #0表示从文件起始位置 1当前位置 2末尾未知 # files.seek(3,0) # print(files.readline()) # files.close() #导入os包 import os #对文件重命名 #os.rename("e:/Python/filetest.txt","e:/Python/filetest222.txt") #删除文件 #os.remove("e:/Python/filetest222.txt") #创建目录 只能创建一级目录 wuyuemulu为文件夹名称 #os.mkdir("e:/Python/wuyuemulu") #创建多级目录 wuyueduoji/a/b/c #os.makedirs("e:/Python/wuyueduoji/a/b/c") #删除目录 c目录被删除 #os.rmdir("e:/Python/wuyueduoji/a/b/c") #删除目录 a/b/c目录全被删除 #os.removedirs("e:/Python/wuyueduoji/a/b/c") #中文问题 files=open("e:/Python/filetest.txt") #获取文件大小 import os filesize=os.path.getsize("e:/Python/filetest.txt") #或者 获取文件大小另一种写法 filesize=os.path.getsize(files.name) print(filesize) #读取文件一行内容 lines=files.readlines() # end=''时不换行 for line in lines: print(line,end='') #文本文件复制 filetest复制到 #先定位从文件起始位置读取 #files.seek(0) #读取所有内容 # #打开filetestfuzhi.txt 文件,没有则创建 a+代表可以写入 #destfile=open("e:/Python/filetestfuzhi.txt","a+") #写入内容 #destfile.write(filescontent) # # w新建只写,w+新建读写,二者都会将文件内容清零 # (以w方式打开,不能读出。w+可读写) # **w+与r+区别: # r+:可读可写,若文件不存在,报错;w+: 可读可写,若文件不存在,创建 # (a:附加写方式打开,不可读;a+: 附加读写方式打开) #destfile.close() #二进制文件复制 例如复制图片 rb读二进制文件 # imgfiles=open("e:/Python/timg.jpg",'rb') # imgcont=imgfiles.read() # imgfiles.close() #wb写入二进制文件 # destimgfile=open("e:/Python/timg2.jpg",'wb') # destimgfile.write(imgcont) # destimgfile.close() #文件删除 #os.remove("e:/Python/filetestfuzhi.txt") #目录下所有文件 filesall=os.listdir("e:/Python/") #列出目录下所有文件 目录下一级子文件 # for filemodel in filesall: # print(filemodel) #列出目录下所有子文件 递归函数输出 def mulu(p): print(p); if os.path.isdir(p):#判断是否为目录文件 children=os.listdir(p) if children is not None: for child in children: print(p+"/"+child); mulu(p+"/"+child)#递归再次调用 mulu("e:/Python")

  

 
原文地址:https://www.cnblogs.com/wuyiran/p/7762839.html