a=10 def addgoled(): global a a+=1 dd=[{'name':'zhang','age':10}, {'name':'tu','age':13}] d={'name':'zhang','age':10} def adddict(): global d #for a in d.values(): d['age']+=1 # addgoled() print(a) adddict() for k,v in d.items(): print(k,v) list1=[] for i in range(1,11): list1.append(i) for i in range(1,11): if i%3==0: list1.append(i) print(list1) list2=[i for i in range(1,11)] list4=[i*i for i in range(1,6)] print(list4) list5=[i*i for i in range(1,11) if i%2==0] print(list5) list2=[i for i in range(1,11) if i%3==0] print(list2) l1=['Hello','GOOD','ABC','kkh'] l2=[] for i in l1: l2.append(i.lower()) print(l2) #生成器 l8=[i.lower() for i in ['Hello','GOOD','ABC','kkh']] print(l8) l3=[] a=['A','B','C'] b=['X','Y','Y'] for k in a: for j in b: l3.append(k+j) print(l3) #生成器 #l9=[k+j for k in a for j in b] l9=[k+j for k in ['A','B','C'] for j in ['X','Y','Y']] print(l9) #奇数 list3=[i for i in range(1,10) if i%2!=0] print(list3) list4=[i for i in range(1,10) if (i%2!=0)&(i<5)] print(list4)
''' drawrect.py file editor: geovindu, Geovin Du date: 2023-06-11 ''' import turtle as t def drawonerect(x,y,side): """ 画正方形 :param x: :param y: :param side: :return: """ t.penup() t.goto(x,y) t.pendown() for _ in range(4): t.fd(side) t.right(90) def drawfourrect(x,y,side): """ :param x: :param y: :param side: :return: """ side /=2 plist=[(x,y),(x+side,y),(x,y-side),(x+side,y-side)] #元祖拆包 for a in plist: drawonerect(*a,side)
''' Simple.py file editor: geovindu, Geovin Du date: 2023-06-11 ''' import sys import turtle as t ''' t.speed(10) t.ht() t.pu() t.goto(-100,100) t.pendown() for _ in range(4): t.fd(100) t.right(90) t.mainloop() ''' def drawRect(x,y,side=50): """ 画正方形 :param x: X坐标值 :param y: Y坐标值 :param side: 少输入这个参数值时,用默认参数 :return: """ t.penup() t.goto(x, y) t.pendown() for _ in range(4): t.fd(side) t.right(90) #有默认参数,可以不用传递 #drawRect(10,50) #drawRect(10,50,80) # 函数无参数 def printdem(): """ :return: none 返回无值 """ for i in range(1, 11): print(i, end=' ') print() # 有参数函数 def printdemEndInt(n): """ :param n: :return: none 返回无值 """ for i in range(1, n+1): print(i, end=' ') print() #printdem() #printdemEndInt(20) # 被3整除的数 def findthree(number): """ 被3整除的数 (连敲三个双引号,现现六个,从三个中间敲回画,自动生成注择格式,再添加自定义注释,这样会自动提示注释 PyCharm 2023.1.2 geovindu, Geovin Du 涂聚文 :param number: 需要整除的整数的输入参数 :return: 返回个数 """ num = 0 for i in range(1, number+1): if(i%3==0): num = num + 1 print("被3整除的数:",str(i)) #print() return num def numberoperater(n,m): """ 两个数的加减乘除 :param n: 第一个数 :param m: 第二个数 :return: 返回值 """ sum=0 sum=m+n print("和:",sum) print("减:",n-m) print("除:",n/m) print("乘:",n*m) return m+n,n-m,n/m,n*m def unevenNumber(n,m): """ 计算从1到某个数以内的奇数的和并返回 :param n: 起始的数1 :param m: 最大的数 :return: 返回奇的和 """ sum=0 #for n in range(1,m+1,2) for i in range(n, m + 1): if (i % 2 != 0): sum+=i#sum=sum+i print(i) return sum def evenNumber(nmuber): """ 判断某个数是否偶数 :param nmuber: 输入整数 :return: 返回结果为逻辑值 """ if(nmuber%2==0): return True else: return False def ComparingNumbers(n,m): """ 比较两个数的大小,返回较大的一个 :param n: 第一个数 :param m: 第二个数 :return: 返回最大的数 """ # return a if a>b else b # 如三目运算 if(n>m): print("第一个数大",n) return n elif(m>n): print("第二个数大",m) return m elif(m==n): print("一样大") return 0 def NumberEvenSum(n): """ 返回1至N的所有偶数的和,奇数的和 :param n: 整数参数 :return: 返回偶数的和,奇数的和 """ evensum=0 #偶数的和 unevensum=0 #奇数的和 for i in range(1, n + 1): if (i % 2 != 0): unevensum=unevensum+i if (i % 2 == 0): evensum = evensum + i return evensum,unevensum def NumberEvenSum2(n): """ 返回1至N的所有偶数的和,奇数的和 :param n: 整数参数 :return: 返回偶数的和,奇数的和 """ evensum=0 #偶数的和 unevensum=0 #奇数的和 for i in range(1, n + 1, 2): unevensum += i for i in range(2, n + 1, 2): evensum += i return evensum,unevensum def du(a): """ 如果函数没有返回值为none :param a: :return: """ a=[1,2,3] a.append(40) return a def duParameterPassing(a): """ 如果函数,这会传递参数 传递实参 :param a: :return: """ a.append(40) def editDic(n): """ 修改年龄 传递形参 :param a: 字典 :return: none """ oldage=a['age'] #oldage=30 n['age'] += 1 print("old age",oldage,"new age:",a['age']) def defaultfunction(a,b=100): """ :param a: :param b: :return: """ return a+b def keyfunction(a,b): """ :param a: :param b: :return: """ print("a=",a,"b=",b) def keyfunctiontype(a:int,b:int)->int: #None 返回值为int 类型 """ 指定数据类型 :param a: :param b: :return: """ print("a=",a,"b=",b) def ParamArray(*args): """ 接受不定长的位置参数 :param args: :return: """ print(args,type(args)) #args 是元祖,把所有参数都放在args元祖里 for i in args: print(i, end=' ') def kwParamArray(**kwargs): """ **kwargs 接受的是不定长的关键字参数 字典 :param kwargs: :return: """ print(kwargs) def avgeParamArray(*args): """ 求不定长度的参数的整数的平均值 :param args: 不定长的参数 :return: 返回平均值 """ pass sum=0 #和 n=len(args) #个数 avg=0 for i in args: sum += i avg = sum/n return avg def avgeParamArray2(*args): """ 求不定长度的参数的整数的平均值 :param args: 不定长的参数 :return: 返回平均值 """ print(sum(args)/len(args)) def CitykwParamArray(**kwargs): """ **kwargs 接受的是不定长的关键字参数 字典 :param kwargs: :return: """ for key in kwargs.keys(): #print(kwargs.keys(),kwargs.values()) print(key,kwargs[key]) for key in kwargs: print(f'{key}={kwargs[key]}') # 格式化输出 def generator(): list1 = [] for i in range(1, 11): list1.append(i) for i in range(1, 11): if i % 3 == 0: list1.append(i) print(list1) list2 = [i for i in range(1, 11)] list4 = [i * i for i in range(1, 6)] print(list4) list5 = [i * i for i in range(1, 11) if i % 2 == 0] print(list5) list2 = [i for i in range(1, 11) if i % 3 == 0] print(list2) l1 = ['Hello', 'GOOD', 'ABC', 'kkh'] l2 = [] for i in l1: l2.append(i.lower()) print(l2) # 生成器 l8 = [i.lower() for i in ['Hello', 'GOOD', 'ABC', 'kkh']] print(l8) l3 = [] a = ['A', 'B', 'C'] b = ['X', 'Y', 'Y'] for k in a: for j in b: l3.append(k + j) print(l3) # 生成器 # l9=[k+j for k in a for j in b] l9 = [k + j for k in ['A', 'B', 'C'] for j in ['X', 'Y', 'Y']] print(l9) # 奇数 list3 = [i for i in range(1, 10) if i % 2 != 0] print(list3) list4 = [i for i in range(1, 10) if (i % 2 != 0) & (i < 5)] print(list4)
标签:return,generator,python,args,param,range,print,def From: https://www.cnblogs.com/geovindu/p/17472983.html