x, y =10,20
print(x, y)
x, y = y, x
print(x, y)
输出
10 20
20 10
2.反转字符串**
a ="GeeksForGeeks"
print("Reverse is", a[::-1])
输出
Reverse is skeeGroFskeeG
3.连接列表中的元素**
a =["Geeks","For","Geeks"]
print(" ".join(a))
输出
GeeksForGeeks
4.多比较符**
n =10
result = 1 < n < 20
print(result)
result = 1 > n <= 9
print(result)
输出
True
False
5.输出模块的位置**
import os;
import socket;
print(os)
print(socket)
输出
'os'from'/usr/lib/python3.5/os.py'><>
'socket'from'/usr/lib/python3.5/socket.py'><>
6.使****用枚举**
classMyName:
Geeks,For,Geeks= range(3)
print(MyName.Geeks)
print(MyName.For)
print(MyName.Geeks)
输出
2
1
2
7.函数返回多个值**
def x():
return 1,2,3,4
a, b, c, d = x()
print(a, b, c, d)
输出
1 2 3 4
8.找到数组中出现频率最高的数**
test =[1,2,3,4,2,2,3,1,4,4,4]
print(max(set(test), key = test.count))
输出
4
**
9.检查对象占用内存大小**
import sys
x =1
print(sys.getsizeof(x))
输出
28
10.检查两个字符串是否字谜(字母和出现次数一致)**
from collections importCounter
def is_anagram(str1, str2):
returnCounter(str1)==Counter(str2)
print(is_anagram('geek','eegk'))
print(is_anagram('geek','peek'))
输出
True
False
全部0条评论
快来发表一下你的评论吧 !