Python3.6 programming skills summary, python3.6 programming skills
I have been studying python for more than half a year and found many practical programming skills that are easy to ignore. The following is a summary.
1. Print the file path of the import module
import threadingimport socketprint(threading)print(socket)
Module 'threading' from '/Users/taorui/anaconda3/lib/python3.6/threading. py'
Module 'socket 'from'/Users/taorui/anaconda3/lib/python3.6/socket. py'
2. Operator _ in interactive environment
2+1
3
_
3
print(_)
3
3. Check python objects
test=[1,3,5,7]print(dir(test))
['Add','Class','Contains','Delattr','Delitem','Dir','Doc','Eq','Format','Ge','Getattribute','Getitem','Gt','Hash','Iadd','Imul','Init','Init_subclass','Iter','Le','Len','Lt','Mul','Ne','New','Reduce','Performance_ex','Repr','Reversed','Rmul','Setattr','Setitem','Sizeof','Str','Subclasshook', 'Append', 'clear', 'copy', 'Count', 'extend', 'index', 'insert', 'pop', 'delete ', 'reverse', 'sort ']
4. Check the python version
import sysprint(sys.version)
3.6.3 | Anaconda, Inc. | (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
5. Combine multiple strings
test=['I','Like','Python','automation']print(test)ss=""print(ss.join( test))
['I', 'like', 'python', 'automation ']
ILikePythonautomation
5. Four string/list flip Methods
testList=[1,3,5]testList.reverse()print(testList)
[5, 3, 1]
Flip in a loop and iterate the output
for element in reversed([1,3,5]): print(element)
5
3
1
One line of code flip string
"Test Python"[::-1]
'Nohtyp tset'
Use slice flip list
[1,3,5][::-1]
[5, 3, 1]
6. Playback Enumeration
testlist=[10,20,30]for i,value in enumerate(testlist): print(i,':',value)
0: 10
1: 20
2: 30
7. Use enumeration in python
class Shapes: Circle,Square,Tringle,Quadrangle=range(4)print(Shapes.Circle)print(Shapes.Square)print(Shapes.Tringle)print(Shapes.Quadrangle)
0
1
2
3
8. Use the * operator to unpack function parameters
def test(x,y,z): print(x,y,z)testDict={'x':1,'y':2,'z':3}testList=[10,20,30]test(*testDict)test(**testDict)test(*testList)
X y z
1 2 3
10 20 30
9. use dictionaries to store selection operations
stdcalc={ 'sum':lambda x,y:x+y, 'subtract':lambda x,y:x-y}print(stacalc['sum'](9,3))print(stacalc['subt'](9,3))
12
6
10. dictionary set Derivation
testSet={i*2 for i in range(10)}testDict={i: i*i for i in range(10) }print(testSet)print(testDict)
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
11. In-situ exchange of two numbers
x,y=10,20print(x,y)x,y=y,xprint(x,y)
10 20
20 10
12. chain-like comparison Operators
n=10result=1<n<20print(result)result=1>n>20print(result)
True
False
13. conditional assignment using ternary Operators
out=[m**2 if m>10 else m**4 for m in range(50)]print(out)
[0, 1, 16, 81,256,625,129 6, 2401,409 6, 6561,100 00, 121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,729,784,841,900,961,102 4, 1089,115 6, 1225,129 6, 1369,144 4, 1521,160 0, 1681,176 4, 1849,193 6, 2025,211 6, 2209,230 4, 2401]
14. Store list elements to new variables
testList=[1,2,3]x,y,z=testListprint(x,y,z)
1 2 3
15. One line of code is used to calculate the factorial of any number.
import functoolsresult=(lambda k:functools.reduce(int.__mul__,range(1,k+1),1))(3)print(result)
6
16. Find the number that appears most frequently in the list
test=[1,2,3,4,2,2,3,4,1,4,4,4,4,]print(max(set(test),key=test.count))
4
17. Check the memory usage of an object
import sysx=1print(sys.getsizeof(x))
28
18. Build a dictionary from the connected Sequence
t1=(1,2,3)t2=(10,20,30)print(dict(zip(t1,t2)))
{1: 10, 2: 20, 3: 30}
19. Search for multiple prefix and suffix strings in one line of code
print("http://www.google.com".startswith(("http://","https://")))print("http://www.google.co.uk".endswith((".com",".uk")))
True
True
20. Construct a switch-case statement without loops
import itertoolstest=[[-1,-2],[30,40],[25,35]]print(list(itertools.chain.from_iterable(test)))
[-1,-2, 30, 40, 25, 35]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. // Blog.csdn.net/qq_30262201/article/details/78795127