12 essential Python functions, beginners can get started after reading it!

thumbnail

https://sslljy.blog.csdn.net/?type=blog

foreword

Hello everyone, I'm a rookie.

Novices are easy to get stuck when writing code, especially when they are exposed to a lot of functions and other knowledge. After reading the requirements, they often do not know what method to use to implement it. You may have the logic to implement it, but how should you do it? Forget which function to use, this is actually a lack of knowledge reserves. If you can't remember which function does what, you will naturally be at a loss.

In the past few days, I have specially sorted out some commonly used functions of Python, from the most basic input and output functions to regular and other 12 sections, a total of more than 100 commonly used functions, which are convenient for friends to quickly memorize, go through it quickly every day, use When it’s time to deepen it, slowly you will get rid of the situation of writing code stuck.

Although when we teach ourselves programming, we emphasize more on understanding and actually typing code, but there are some things you must keep in mind, otherwise you will be difficult to write code. Of course, veterans have already memorized them by heart. If newbies want to develop quickly and easily, remembering the functions used frequently is a good way.

  1. Basic functions

Case: Convert a floating-point value to a string and output the converted data type

f = 30.5

ff = str(f)

print( type(ff))

#The output result is class 'str'

  1. Process Control

Case: The score is judged according to the score entered by the user. When the score is lower than 50, it will prompt "Your score is lower than 50." When the score is 5059, it will prompt "Your score is around 60." Excellent, more than 90 points are very excellent.

s = int(input( "Please enter the score:" ))

if80 >= s >= 60:

print ( "Pass" )

elif80 < s <= 90:

print ( "excellent" )

elif90 < s <= 100:

print ( "very good" )

else:

print ( "Failed" )

ifs > 50:

print ( "Your score is around 60" )

else:

print ( "Your score is below 50" )

  1. List

Case: Determine the position of the number 6 in the list [1, 2, 2, 3, 6, 4, 5, 6, 8, 9, 78, 564, 456] 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)

#output result is 4

  1. Tuples

Case: Modify a tuple

Take 3 numbers with tuple subscripts between 1 and 4 and convert them into lists

t = (1,2,3,4,5)

print(t[1:4])

l = list(t)

print(l)

#Insert a 6 at the position marked 2 in the list

l[2]=6

print(l)

#Convert the modified list into a tuple and output

t=tuple(l)

print(t)

#The result of the operation is:

(2, 3, 4)

[1, 2, 3, 4, 5]

[1, 2, 6, 4, 5]

(1, 2, 6, 4, 5)

  1. Strings

Case: Output strings in three ways of format

Method 1: Use digital placeholders (subscripts)

"{0} hehe" .format( "Python" )

a=100

s = "{0}{1}{2} hehe"

s2 = s.format(a, "JAVA", "C++")

print(s2)

#The result of the operation is: 100JAVAC++ hehe

Method 2: Use {} to occupy space

a=100

s = "{}{}{} hehe"

s2 = s.format(a, "JAVA", "C++", "C# ")

print(s2)

#The result of the operation is: 100JAVAC++ hehe

Method 3: Place placeholders with letters

s = "{a}{b}{c} hehe"

s2 = s.format(b= "JAVA",a= "C++",c= "C# ")

print(s2)

#The result of the operation is: C++JAVAC# hehe

  1. Dictionary

Case: Find data in a dictionary

d = { "name" : "Little Black" }

print (d.get( "name2" , "Not found" ))

print(d.get( "name"))

#The result of the operation is:

not found

little black

  1. Functions

The highlight of the function is more custom functions. There are not many commonly used built-in functions, mainly the following:

Case: Defining a local variable in a function, the variable can still be called by jumping out of the function

def fun1:

global b

b=100

print(b)

fun1

print(b)

#The result of the operation is:

100

100

  1. Processes and threads

Case: Inheriting the Thread class implementation

#Create multiple threads

class MyThread(threading.Thread):

def init(self,name):

super.init

self.name = name

def run(self):

#Thread to do

for i in range(5):

print(self.name)

time.sleep(0.2)

#Instantiate child thread

t1 = MyThread( "cool" )

t2 = MyThread( "My dearest person" )

t1.start

t2.start

  1. Modules and Packages

Case: How to use the package 4

from my_package1 import my_module3

print(my_module3.a)

my_module3.fun4

  1. File Operations

(1) Regular file operations

General mode for file operations:

file object properties

methods of the file object

(2) OS module

  • About the function of the file

About the function of the file

  • About the function of folders

About the function of folders

Case: usage example of classmethod

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( "Little Bitch" )

b.eat

#Run result is: 10

  1. Regular

Example: Split a string and convert it to a list with the split function

import re

s = "abcabcacc"

l = re.split( "b",s)

print(l)

#The result of the operation is: ['a', 'ca', 'cacc']

Epilogue

The purpose of this article is not to teach you how to use functions, but to memorize commonly used function names quickly and easily, so instead of giving you an example of the usage of each function, you only have to remember the function name and its After the function of the function, you will have a clue. As for the usage of the function, Baidu will come out at once, and you will know it after a few times.

If you don't even know the function name and its purpose, you will spend more time and energy, and it will be faster than we will search the information with purpose.

Recommended books

The popularization of the Internet and the maturity of technologies such as big data, cloud computing, 5G , artificial intelligence, and blockchain have contributed to the great prosperity of the digital economy. Based on computing power, a new era of digital economy in which everything senses, interconnects and is intelligent is coming. The amount of data is growing explosively, the demand for computing power has reached an unprecedented height, and computing power has become a new engine of the digital economy.

There are 8 chapters in this book , which systematically describe computing power and computing power economy, involving new infrastructure, new energy system, data resources, computing power technology system, computing power center based on new energy and electricity, computing power industry, etc.; From the perspective of industrial applications, we analyze the driving logic of computing power to the digital economy, and help enterprises and individuals find the direction of their efforts.

Getting Started: The most complete problem of zero-based learning Python | Zero-based learning Python for 8 months | Practical projects | Learning Python is this shortcut

Dry goods: Crawling Douban short reviews, the movie "Later Us" | Analysis of the best NBA player in 38 years | From much anticipation to word of mouth! Tang Detective 3 is disappointing | Laugh at the new Yitian and Dragon Slayer | The king of lantern riddles | Use Python to make a large number of sketches of young ladies | Mission Impossible is so popular, I use machine learning to make a mini recommendation system movie

Fun: Pinball game | Jiugongge | Beautiful flower | Two hundred lines of Python "Running every day" game!

AI: A robot that can write poetry | Color pictures | Predict income | Mission Impossible is so popular, I use machine learning to make a mini recommendation system movie

Widget: Convert Pdf to Word, easily get tables and watermarks! | Save html web pages as pdf with one click! | Goodbye PDF extraction charges! | Create the strongest PDF converter with 90 lines of code, one-click conversion of word, PPT, excel, markdown, html | Make a DingTalk low-cost ticket reminder! |60 lines of code make a voice wallpaper switcher, watch Miss Sister every day! |

The year's hottest copy

  • 1). Shit! Convert Pdf to Word easily with Python!
  • 2). Learning Python is really fragrant! I made a website with 100 lines of code, helped people PS travel pictures, and earned a chicken leg to eat
  • 3). The first broadcast has exceeded 100 million, and it has become popular all over the Internet. I analyzed "Sister Riding the Wind and Waves" and found these secrets
  • 4). 80 lines of code! Use Python to make a Doraemon clone
  • 5). 20 python codes you must master, short and powerful, infinitely useful
  • 6). A collection of 30 Python weird tricks
  • 7). The 80-page "Rookie Learning Python Selected Dry Goods.pdf" that I summarized are all dry goods
  • 8). Goodbye Python! I'm going to learn Go! 2500 words in-depth analysis!
  • 9). Discover a licking dog benefit! This Python crawler artifact is so cool, it automatically downloads pictures of girls
Latest Programming News and Information | GeekBar

Related Posts