Recommend eight very practical Python Case study , I hope you can gain something after watching !
1、 Merge two dictionaries
Python3.5 after , Merging dictionaries becomes easier , We can go through ** Symbol decompression Dictionary , And pass multiple dictionaries into {} in , To achieve a merger .
def Merge(dict1,dict2):
res = {
**dict1,**dict2}
return res
# Two dictionaries
# Python Exchange of learning 1 skirt 815624229 ###
# Python Exchange of learning 2 skirt 279199867 ###
dict1 = {
"name":"joy","age":25}
dict2 = {
"name":"joy","city":"New York"}
dict3 = Merge(dict1,dict2)
print(dict3)
Output
{
'name':'Joy','age':25,'city':'New York'}
2、 Check if the file exists
We know Python There are special modules dealing with system interaction -OS , He can handle all kinds of operations of adding, deleting, modifying and checking documents .
How to check whether a file exists ?OS Modules can be easily implemented .
from os import path
def chaeck_for_file():
print("Does file exist:",path.exists("data.csv"))
if __name__=="__main__":
check_for_file()
Output
Does file exist:Fales
3、 Retrieve the last element of the list
When using lists , Sometimes you need to take the last element , There are several ways to achieve .
my_list = ['banana','apple','orange','pineapple']
# Index method
last_element = my_list[-1]
# pop Method
last_el
Output
'pineapple'
4、 Computer code execution time
Python in time The module provides various function methods related to time processing , We can use it to calculate code execution time .
import time
start_time = time.time()
total = 0
for i in range(10):
total += i
print("Sum:",total)
end_time = time.time()
time_taken = end_time - start_time
print("Time:",time_taken)
Output :
Sum:45
Time:0.009975433349609375
5、 exception handling
Python Provides TRY…EXCEPT…FINRLLY To handle different code exceptions , Of course, there are other ways to combine .
a,b = 1,0
try:
print(a/b)
except ZeroDivisionError:
print("Can not divide by zero")
finally:
print("Executing finally block")
Output
Can not divide by zero
Executing finally block
6、 Variable memory footprint
How to output Python The memory footprint of the variable in , Can pass sys Module to achieve .
import sys
var1 = 15
list1 = [1,2,3,4,5]
print(sys.getsizeof(var1))
print(sys.getsizeof(list1))
Output
28
104
7、 Remove duplicate items from the list
Deleting duplicate items in a list can be filtered by traversal , Or just use the set method .
list1 = [1,2,3,3,4,'John','Ana','Mark','John']
# Method 1
def remove_duplicate(list_value):
return list(set(list_value))
print(remove_duplicate(list1))
# Method 2
result = []
[result.append(x) for x in list1 if x not in result]
print(result)
Output
[1,2,3,4,'Ana','John','Mark']
[1,2,3,4,'John','Ana','Mark']
8、 Chain function call
Multiple functions are called in a line of code
def add(a,b):
return a + b
def subtract(a,b):
return a - b
a, b = 5, 10
print((add if b > a else subtract)(a,b))
Output
15
Brother Meng , That's the end of today's sharing , If it helps , Remember to like collection + Follow WOW ~