Python good habits (1)

Control flow

If related

  • Do not put variables andTrue False None () [] {}Direct comparison

    if foo == True:
    should be written as
    if foo:
  • Check variables in the list
#error
is_generic_name = False
name = 'Tom'
if name == 'Tom' or name == 'Dick' or name == 'Harry':
  is_generic_name = True

But

name = 'Tom'
is_generic_name = name in ('Tom', 'Dick', 'Harry')
  • If not should be written in one line
name = 'Jeff'
address = 'New York, NY'
if name: print(name)
print(address)

It should be:

name = 'Jeff'
address = 'New York, NY'
if name:
  print(name)
print(address)

For related

  • Must have an index scene with enumerate
my_container = ['Larry', 'Moe', 'Curly']
index = 0
for element in my_container:
  print ('{} {}'.format(index, element))
  index += 1

Should be:

my_container = ['Larry', 'Moe', 'Curly']
for index, element in enumerate(my_container):
  print ('{} {}'.format(index, element))

Intelligent Recommendation

2017-08-17 Learn three three struts1

Today, I'm finished yesterday. It is roughly a small project that shows success after registration with the struts1 framework. It doesn't use the database. I feel that the theory of light is not so bi...

Python random seed function np.random.seed () Learning notes

Python random seed function np.random.seed () Learning notes Recently, I was watching some video of machine learning and deep learning. I encountered the number of np.random.seed () in the program. I ...

Brief and practical examples of regular expressions

1. What is a regular expression Regular Expression (Regular Expression, referred to as regexp) Is a grammatical structure describing a string It is a specific formatting mode, used to verify whether v...

Python processing Excel data (4) -writing data

1. Quote plug -in 2. The writing operation of sheet, Cell 1. Get the sheet object Output results: 2. Modify/add/delete operation of SHEET Output results: 3. Write operation of cells 4. Save file Combi...

More Recommendation

Search algorithm (BFS, DFS)

1 Scenary Priority Search (BFS) 1.1 Catch That Cow (Hangzhou OJ 2717)...

[python study notes] Create a django project from scratch-message board project 2

1. Extract data from the previous html page and save it to the database: The action in the form tag corresponds to the url in urls.py. Here we write index, so the above action should be set to ‘...

File import to database-complex version

demand There are several modules setup, smsdelivery, release, connect, smssubmit, callin, finish; each module corresponds to a directory, there are 01 to 31 subdirectories (folders) in the directory, ...

Third, modify the page style in the rails

Earlier we talked about verification, now if that home is ugly, then it's time for an overhaul at the layout of the page, and nothing more than two points, modify the template page and modify CSS styl...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top