Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
In this article, we will document some of Python’s daily programming tips and use IPython to conduct interactive tests to better understand and learn some of Python’s features.
Format and print mode information
In [3] :print('success'.center(20.'='))
======success=======
In [4] :print('failed'.center(20.'='))
=======failed=======
Copy the code
Else for loop
Grammar:
forvariableinIterable: the body of a loopelse: The loop ends normallyCopy the code
IPython test
# for loop
In [1] :for i in range(5) :... :print(i) ... :else:
...: print('For loop completes properly')
...: print(i) ... :0
1
2
3
4
forNormal end of cycle4
Copy the code
Note: the else loop will not run if the loop is broken.
Let’s look at an example. Print primes up to 10.
A prime number is a natural number greater than 1 that has no other factors except 1 and itself.
for n in range(2.10) :for x in range(2, n):
if n % x == 0:
Covariance of # is 0 means that n has other factors
print(n, '=', x, The '*', n // x)
break
else:
No other factors of n were found
# Loop completes normally
print(n, It's a prime number.)
Copy the code
The results show
2Is a prime number3Is a prime number4 = 2 * 2
5Is a prime number6 = 2 * 3
7Is a prime number8 = 2 * 4
9 = 3 * 3
Copy the code
Comparison of mathematical expressions
In [8]: num = 5
In [9] :3 < num < 8
Out[9] :True
In [10] :6 < num < 8
Out[10] :False
Copy the code
Unpacking lists and dictionaries
The list of split open a case
In [12]: a, b, c = [1.2.3]
In [13]: a, b, c
Out[13] : (1.2.3)
In [25]: li = [1.2.3.4.5]
In [26] :print(*li)
1 2 3 4 5
Copy the code
Pass the list element as an argument to the function
In [34]: li = [1.2.3.4.5]
In [35] :def sum(*args) :. : total =0. :for i inargs: ... : total += i ... :print(total) ... : In [36] :sum(*li)
15
Copy the code
The dictionary split open a case
In [27]: user_dict = { ... :'name': 'ithui'. :'sex': 'male'. :'age': 20. : } In [28]: user_info = 'My name is {name}, gender {sex}, this year {age}'.format(**user_dict)
In [29] :print(user_info) My name is Ithui, gender male, this year20At the age ofCopy the code
Function keyword arguments are automatically matched
func_params = {
'username': 'ithui'.'mobile': '13022331752',}def query_user(username=None, mobile=None) :
# Simulate query user information
print(username)
print(mobile)
sql = """select * from user where username={} and mobile={}""".format(username, mobile)
print(SQL) query_user func_params (* *) = = = = = = = = = = = = = = = = = = = = = = = the result as follows = = = = = = = = = = = = = = = = = = = = = = = ithui13022331752
select * from user where username=ithui and mobile=13022331752
Copy the code
Take the middle element
In [15]: a, *mid, b = [1.2.3]
In [16]: mid
Out[16] : [2]
In [17]: a, *mid, b = [1.2.3.4.5.7]
In [18]: mid
Out[18] : [2.3.4.5]
In [21]: a, b, *mid, c, d = [1.2.3.4.5.7.8]
In [22]: mid
Out[22] : [3.4.5]
Copy the code
Gets the n largest or smallest elements in the list
In [38] :import heapq
In [39]: salarys = [5000.6000.3500.5500.7000.6500.8000]
In [40] :# Three largest numbers
In [41]: heapq.nlargest(3, salarys)
Out[41] : [8000.7000.6500]
In [42] :# Two smallest numbers
In [43]: heapq.nsmallest(2, salarys)
Out[43] : [3500.5000]
Copy the code
The tail language
✍ Code writes the world and makes life more interesting. ❤ ️
✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️
✍ code word is not easy, but also hope you heroes support. ❤ ️