Top 10 Error Idioms Stories for Novice Python Languages

1. Superfluous-extra semicolon

The Python language is different from most programming languages. It does not need to end with a semicolon after its statements. Some Python novices have extensive experience in writing other languages, but can't adapt to it for a while, often add "semicolons" habitually

a = 5 (正确)
a = 5; (错误)

2. Suspended warts-useless parentheses

In the Python language, the part of the expression of the if witch statement does not need to add parentheses (although the program can still run), which is different from many programming languages.

if a > b: (正确)
print(a)
if (a > b): (错误)
print(a, b)

Recommendation: 020 is continuously updated, the small circle of boutiques has new content every day, and the concentration of dry goods is extremely high.
There are everything you want to make connections and discuss technology!
Be the first to join the group and outperform your peers! (There is no fee for joining the group)
Click here to communicate and learn with Python developers.
Group number: 745895701
application and delivery :
Python software installation package, Python actual combat tutorial,
free collection of materials, including Python basic learning, advanced learning, crawling, artificial intelligence, automated operation and maintenance, automated testing, etc.

Three, Zhang Guan Li Dai-assignment operator to judge equality

To judge the equality of two expressions/variables/constants/references, the relational operator "==" should be used instead of the assignment operator "=".

if a == b: (正确)
print(“相等!”)
if (a = b): (错误)
print(“相等!”)

Fourth, it is difficult for others-string and other data types do "+" operation

The addition of character strings and other data types is supported by some programming languages ​​to achieve the effect of string splicing. But Python does not support it.

print(‘There is’ + str(num)+ ’ apples.’) (正确)
print(‘There is’ + num+ ’ apples.’) (错误)

Five, turn the throat taboo-use keywords as variable names

There are 33 keywords in Python3:

and,as,assert,break,class,continue,def,del,elif,else,except,finally,False,for,from,global,if,import,in,is,lambda,None,
nonlocal,not,or,pass,raise,return,True,try,while,with,yield

Avoid using keywords in variable naming, function naming, and class naming

Sixth, break the convention-indentation error

The indentation of sentences is very important in the Python language. Indentation distinguishes the levels of sentences, and sentences at the same level need the same indentation width.

for i in range(10):	print(“Good Morning!!”)			# 循环内的语句	
                    print(“Good Afternoon!!”)		# 循环内的语句
      print(“Good Night!!”) 				# 循环外的语句

Seven, impose on others-try to modify string elements

String type data in Python cannot be modified.

s = ‘hello world!’
s[0] = ‘H’ (错误)

Eight, the friend between you and you-mistakenly treat two objects as one

Two objects created by a class each have their own memory space, and the stored data does not affect each other.

Nine, wrong investigation Xianyu-variable or function name transcription error

For novices, copying errors when copying variable or function names is commonplace.

Ten, overwhelmingly-list index out of bounds

l = [0, 1, 2, 3, 4, 5]

Then l has 6 elements, which can be accessed normally through l[0]-l[5], there is no l[6], l[-1] refers to the last element of the list

Guess you like

Origin blog.csdn.net/Python_xiaobang/article/details/112391287