7 types of common sentences in python interview questions 2023

By XiaoXin
A Bit Randomly

The pcntl_fork() function is a function used to create a process in the php-pcntl module. (does not support windows). As for how to install and enable the php_pcntl extension, I won’t introduce it here, only ana... Read Parallel processing in PHP using pcntl_fork() Explanation

Main Contents
  1. Comparison and exchange
  2. Sorting
  3. Conversion
  4. Fourth, traversal (loop)
  5. Find
  6. Recursion
  7. Statement simplification

7 types of common sentences in python interview questions

Comparison and exchange

1. Compare and output the larger one

print(a if a>b else b)

2. Swap two elements

a,b = b,a 
list1[i],list[j]=list1[j],list[i]

Sorting

1. String sorting

s = ' aaccbgd '
print(''.join(sorted(list(s))))

2. Array sorting

l = [4, 5, 2, 3, 9]
print(sorted(l))  # Sort from small to large
print(sorted(l, reverse=True))  # Sort from big to small

3. Dictionary sorting, converted to a list

d = {3: 2, 2: 1, 1: 3}
l1 = sorted(d.items(), key=lambda x: x[0])  # Sort according to key
l2 = sorted(d.items(), key=lambda x: x[1])  # Sort according to value

Conversion

1. String-Array

s = ' abcde ' 
l = list(s)

2. Array-String

l = [1, 2, 4, 4, 3]
# s = str(l) #Straight forward
s = ''.join([str(x) for x in l])  # Using join connection needs to be converted to string format

3. Dictionary-Array

d = {3: 2, 2: 1, 1: 3}
l1 = sorted(d.items(), key=lambda x: x[0])  # Sort according to key

4. Dictionary-array, convert the two lists into the key and value of the dictionary respectively

l = [1, 2, 4, 4, 3]
s = [' s ', ' y ', ' o ', ' m ', ' z ']
d = dict(zip(l, s))

5. ip address - 32-bit integer

ip = " 192.168.1.1 "
list_ip = list(map(int, ip.split(" . ")))
res = ""
for i in list_ip:
    res += str(" %08d " % (int(bin(i)[2:])))
print(int(res, 2))

Fourth, traversal (loop)

1. Single-layer traversal array for loop

value = 0
for i in range(100):  # accumulate 1- 100 
    value += i
print(value)

2. Multi-layer traversal array for loop

l = [2, 4, 5, 7, 3, 8, 1, 3]
n = len(l)
for i in range(n):  # bubble sort
    for j in range(n[i] - 1):
        if l[j] > l[j + 1]:
            l[j], l[j + 1] = l[j + 1], l[j]

3. while traversing the array

i = 1
while i <= 9:  # print 99 multiplication table
    a = 1  # column counter
    while a <= i:
        print(" %d * %d = %d " % (a, i, i * a), end=" \t ")
        a += 1
print("")
i += 1

Find

1. Find the position of the first element in the string, and return -1 if there is no

print(s.find(" a "))

2. Find the position of the first element in the list, no error is reported

print(list1.index(" hello "))

3. Find the value of the corresponding key in the field, and return the second element if there is no value (default)

print(dict1.get(" k1 "))
print(dict1.get(" k1 ", 0))

Recursion

def Fibonacci(n):  # Fibonacci series
    if n == 1 or n == 2:
        return 1
    elif n == 3:
        return 2
    else:
        return Fibonacci(n - 1) + Fibonacci(n - 2)

Statement simplification

1. Fast assignment/initialization

num,s,l,d = 0,"yes",[],{}

2. Ternary operator

flag = True
print(1 if flag else - 1)

3. List comprehension

x = [1, 1, 1]
y = [1, 2, 3]
z = [a + b for a in x for b in y]  # return the results of various additions
q = [a + b for a, b in zip(x, y)]  # return the result of adding the corresponding subscripts

4. Lamda expression

x = lambda i: i ** 2  # Use lambda to simplify the function content
print(x(4))

d = {3: 2, 2: 1, 1: 3}
l1 = sorted(d.items(), key=lambda x: x[0])  # use lamda to specify the sorted fields: sort by key
l2 = sorted(d.items(), key=lambda x: x[1])  # use lamda to specify the sorted fields: sort by value
Please Share This Article Thank You!

Ecommerce Security Checklist: User, Membership

Login - brute force- Any user/password login- SMS/email bombing- Captcha bypass/blasting/replay/postback- Username/Mobile Number Enumeration- Unauthorized login (such as modifying the user ID in the data packet)- Account p...

PHP download + save file by file_get_contents

file_get_contents()&nbsp;is read the contents of a file into a string. It will use memory mapping techniques, if web server is supported to enhance performance. public function download($url, $savePath): bool { $options = ...