-
浮点数,字符串,整数,整数查看全部
-
print("a")查看全部
-
1)0、空字符串、None 都会被识别为 False,其他为 True
2)not 优先级高于 and 和 or
3)短路运算:
and 运算时,左边为 False,则直接返回左边,否则返回右边;
or 运算时,左边为 True,则直接返回左边,否则返回右边
查看全部 -
def greet(x=None):
if x is None:
x = 'world'
s = 'Hello, {}.'.format(x)
return(s)
查看全部 -
写代码是一种只有在写的时候才能想清楚整体逻辑结构怎么继续下去——对于我这只新手菜鸟而言。
定义一个函数
def hoj(x):
if not isinstance(x,list) and not isinstance(x,tuple):
print('param type error.')
return None
if isinstance(x,list):
s = 0
for i in x:
if isinstance(i,int) or isinstance(i,float):
s = s + i
return s
if isinstance(x,tuple):
s = 1
for i in x:
if isinstance(i,int) or isinstance(i,float):
s = s * i
return s
x = [1,2,3,5,'abc']
y = hoj(x)
print(y)
x =(1,3,5,'abc')
y = hoj(x)
print(y)
查看全部 -
循环求和:
def f(x):
s = 0
i = 0
while i <= x:
s = s + i
i += 1
return(s)
s = f(100)
print(s)
递归求和:
def g(x):
if x == 1:
return 1
return g(x-1)+x
s = g(100)
print(s)
查看全部 -
定义平方和函数
def squ(L):
squ = 0
for x in L:
squ = squ + x*x
return squ
L =[1,2,3]
squ01 = squ(L)
print(squ01)
查看全部 -
# Enter a code
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
s3 = set([])
x = s1.isdisjoint(s2)
print(x)
if x == False:
for m in s1:
for n in s2:
if m == n:
s3.add(m)
else:
print('The collection does not overlap')
print(s3)
查看全部 -
方法一:
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
i = 0
for item in L:
a = L[i]
b = a in S
if b == True:
S.remove(a)
else:
S.add(a)
i += 1
if i == 10:
break
print(S)
方法二:
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
for x in L:
s = x in S
if s == True:
S.remove(x)
else:
S.add(x)
print(S)
查看全部 -
# coding=utf-8
s = set([])
s.add('Jenny')
s.add('Ellena')
s2 = ['Alice', 'Candy', 'David', 'Hally', 'Bob', 'Isen', 'Karl']
s.update(s2)
print(s)
x = ['Jenny', 'Ellena', 'Alice', 'Candy', 'David', 'Hally', 'Bob', 'Isen', 'Karl']
s = set([])
for i in x:
s.add(i)
print(s)
查看全部 -
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key in d:
value=d[key]
s=0
for s in value:
print(key,":",s)
s+=1
查看全部 -
Extend和append函数是List列表的两种对列表的扩展函数:
Tuple:append时,将原来tuple作为一个整体加到列表的最后;extend时,将tuple中的每个元素依次添加到列表中元素的后面
Set:append时,将原来的set作为一个整体添加到列表的最后;extend时,将set中的元素一个个添加到列表中
Dict:append时,将整个dict作为一个整体,包含key和value放到列表中;extend时,将dict中的value添加到L1的最后。
# 代码:
d = {
'Alice': [45],
'Bob': [60],
'Candy': [75],
}
d['Alice'].append([50, 61, 66])
d['Bob'].extend([80, 61, 66])
d['Candy'].extend([88, 75, 90])
print(d)
# 输出:
{'Alice': [45, [50, 61, 66]], 'Bob': [60, 80, 61, 66], 'Candy': [75, 88, 75, 90]}
查看全部 -
print(d[关键词])和print(d.get('关键词'))的区别在于后者在未检索到关键词时会输出none而非报错
查看全部 -
python2默认的编码格式是ASCII格式,python3默认的编码格式是utf-8格式。
在python2环境中编写python代码时,如果代码(或者注释)有中文,需要在python文件的开头加入# coding=utf-8。如果未指定编码格式,使用默认编码格式ASCII码,那么在执行该文件时,会出现报错.
python3环境中,源码文件默认使用utf-8编码,可以正常解析中文,不需要开头加上面的代码,但是为了代码的可移植性,建议在编写程序的时候加上。
另外,使用编辑器编写python程序时,还需要设置py文件存储的格式为UTF-8,否则会出现乱码或者报错。
查看全部 -
name = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
score = [89, 72, 88, 79, 99]
#定义一个空列表
s=[]
#定义压缩列表,降序排序后依次加入空列表
for i,j in sorted(zip(score,name),reverse=True):
s.append(j)
print s
查看全部
举报