# 字符串的编码解码
s ="妙蛙种子"
byte = s.encode(encoding='GBK')# 编码print(byte.decode(encoding='GBK'))# 解码# 函数defcalc(a, b):
c = a + b
return c
x = calc(10,20)# y=calc(b=10,a=20)print(x)# print(y) # 结果相同 关键字参数 不按顺序传递defadd_end(L=[]):
L.append('End')return L
print(add_end())print(add_end())#['End'] ['End', 'End']#L是可变对象defadd_end(L1=None):if L1 isNone:
L1=[]
L1.append('END')return L1#问题解决#可变参数defcalc(*numbers):sum=(0)for n in numbers:sum=sum+n*n
returnsum#已有数据
nums=[1,2,3]
calc(*nums)#避免繁琐defenroll(name,gender,age=6,city='Beijing'):print('name:',name)print('gender:',gender)print('age:',age)print('city:',city)#递归deffact(n):if n==1:return1return n* fact(n-1)deffact(n):return fact_iter(n,1)deffact_iter(num,product):if num==1:return product
return fact_iter(num-1,num*product)deffact(n):if n==1:return1return n*fact(n-1)print('input:')
n=int(input())print('Results:',fact(n))#汉诺塔defmove(n, a, b, c):if n ==1:print('move', a,'-->', c)else:
move(n-1, a, c, b)#按搬动顺序 可把中间字母当作箭头
move(1, a, b, c)#每一步调用move
move(n-1, b, a, c)
Plate=int(input())#自由输入
move(Plate,'A','B','C')#move函数包含输出 直接调用
c
#include<stdio.h>intmain(){voidswap(int*p1,int*p2);//声明swap函数 int a,b;int*pointer_1,*pointer_2;//定义两个指针变量 printf("please enter a and b:");scanf("%d %d",&a,&b);
pointer_1=&a;
pointer_2=&b;if(a<b)swap(pointer_1,pointer_2);//这里都是指针变量 printf("%d %d",a,b);return0;}voidswap(int*p1,int*p2)//定义swap {int temp;
temp=*p1;*p1=*p2;*p2=temp;}