程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

100 important Python functions, it is recommended to memorize

編輯:Python

Preface

Novices are easy to get stuck when writing code , Especially when the contact function and other knowledge are more , Often I don't know what method I should use to realize the requirement after reading it , The logic of implementation may be that you have , But I forgot what function to use , This is actually the lack of knowledge , You can't remember which function works , Nature is confused .

I've been sorting out these days Python Some commonly used functions , From the most basic input-output functions to regular functions 12 One plate , in total 100 Multiple common functions , It is convenient for small partners to remember quickly , Go through it quickly every day , Deepen it when you use it , Slowly you'll get rid of the code jam .

Although when we teach ourselves programming, we emphasize more on understanding and actually Typing Code , But there are some things you must remember , Otherwise you will be unable to write code . Of course, the veteran has a bad memory , Novices want to develop quickly and easily , Remember that functions used in high frequency are a good way .

1. Basic function

** Case study :** Converts a floating-point value to a string , Output the converted data type

f = 30.5
ff = str(f)
print(type(ff))
# The output is class 'str'

2. Process control

** Case study :** Judge the score according to the score entered by the user , lower than 50 Time sharing tips “ Your score is below 50 branch ”,5059 Time sharing tips “ Your score is 60 about ”, Greater than or equal to 60 A pass ,8090 Divided into excellent , Greater than 90 Divided into very excellent .

s = int(input(" Please enter the score :"))
if 80 >= s >= 60:
print(" pass ")
elif 80 < s <= 90:
print(" good ")
elif 90 < s <= 100:
print(" Excellent ")
else:
print(" fail, ")
if s > 50:
print(" Your score is 60 about ")
else:
print(" Your score is below 50 branch ")

3. list

** Case study :** Judge 6 This number is in the list [1,2,2,3,6,4,5,6,8,9,78,564,456] Position in , And output its subscript .

l = [1,2,2,3,6,4,5,6,8,9,78,564,456]
n = l.index(6, 0, 9)
print(n)
# The output is 4

4. Tuples

** Case study :** Modify tuple

# Subscript the tuple in 1~4 Between 3 Number , Convert to list 
t = (1,2,3,4,5)
print(t[1:4])
l = list(t)
print(l)
# Mark the list with 2 Insert at 1 individual 6
l[2]=6
print(l)
# The modified list is converted into tuples and output 
t=tuple(l)
print(t)
# The running result is :
(2, 3, 4)
[1, 2, 3, 4, 5]
[1, 2, 6, 4, 5]
(1, 2, 6, 4, 5)

5. character string

** Case study :** use format() Three ways to output a string

The way 1: Use numbers to occupy space ( Subscript )

"{0} Hey ".format("Python")
a=100
s = "{0}{1}{2} Hey "
s2 = s.format(a,"JAVA","C++")
print(s2)
# The running result is :100JAVAC++ Hey 

The way 2: use {} placeholder

a=100
s = "{}{}{} Hey "
s2 = s.format(a,"JAVA","C++","C# ")
print(s2)
# The running result is :100JAVAC++ Hey 

The way 3: Use letters to occupy space

s = "{a}{b}{c} Hey "
s2 = s.format(b="JAVA",a="C++",c="C# ")
print(s2)
# The running result is :C++JAVAC# Hey 

6. Dictionaries

** Case study :** Look up data in the dictionary

d = {
"name": " Little black "}
print(d.get("name2", " We didn't find out "))
print(d.get("name"))
# The running result is :
We didn't find out
Little black

7. function

Function is more about custom functions , There are not many commonly used built-in functions , There are mainly the following :

** Case study :** Define a local variable in a function , The jump function can still call the variable

def fun1():
global b
b=100
print(b)
fun1()
print(b)
# The running result is :
100
100

8. Processes and threads

** Case study :** Inherit Thread Class implementation

# Multithreading creation 
class MyThread(threading.Thread):
def __init__(self,name):
super().__init__()
self.name = name
def run(self):
# What threads do 
for i in range(5):
print(self.name)
time.sleep(0.2)
# Instantiate child threads 
t1 = MyThread(" be doomed ")
t2 = MyThread(" The most intimate person ")
t1.start()
t2.start()

9. Modules and packages

** Case study :** How to use the package 4

from my_package1 import my_module3
print(my_module3.a)
my_module3.fun4()

10. File operations

(1) Regular file operations


About the general mode of file operation :

file Object properties of

file Object method

(2)OS modular

  • About file functions

  • About folder functions

11. Decorators / Decorator

** Case study :**classmethod An example of the use of

class B:
age = 10
def __init__(self,name):
self.name = name
@classmethod
def eat(cls): # Ordinary function 
print(cls.age)
def sleep(self):
print(self)
b = B(" Ha ha ha ")
b.eat()
# The running result is :10

12. Regular

** Case study :** use split() Function to split a string and convert it to a list

import re
s = "abcabcacc"
l = re.split("b",s)
print(l)
# The running result is :['a', 'ca', 'cacc']

Conclusion

The purpose of this article , Not to teach you how to use functions , But for the sake of speed 、 Easily remember common function names , So I didn't give you examples of the usage of each function , You have to remember the function name and its function , You'll have a clue , As for the use of functions , Baidu will come out at once , Use it a few times and you'll .

If you don't even know the function name and its purpose , You'll spend more time and energy , It's bound to be faster than when we check the information with a purpose .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved