GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python-reference/cheatsheet/writing_efficient_code.md at master · serarm/python-reference

Expand file tree

/

writing_efficient_code.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
68 lines (54 loc) · 2.16 KB
/

writing_efficient_code.md

Copy path

File metadata and controls

68 lines (54 loc) · 2.16 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

Writing Efficient Python Code

This cheatsheet is built based on DataCamp course: Writing Efficient Python Code

Foundations of efficiencies

  1. Try to follow pythonic way and follow zen of python
# Print the list created using the Non-Pythonic approach
i = 0
new_list= []
while i < len(names):
    if len(names[i]) >= 6:
        new_list.append(names[i])
    i += 1
print(new_list)

# Print the list created by looping over the contents of names
better_list = []
for name in names:
    if len(name) >= 6:
        better_list.append(name)
print(better_list)

# Print the list created by using list comprehension
best_list = [name for name in names if len(name) >= 6]
print(best_list)
  1. Python has hundreds of Python Enhancement Proposals, commonly referred to as PEPs. The Zen of Python is one of these PEPs and is documented as PEP20.One little Easter Egg in Python is the ability to print the Zen of Python using the command import this.
  2. Few important built-in function: range(),enumerate(list,start=),map(func,obj)
# Rewrite the for loop to use enumerate
indexed_names = []
for i,name in enumerate(names):
    index_name = (i,name)
    indexed_names.append(index_name) 
print(indexed_names)

# Rewrite the above for loop using list comprehension
indexed_names_comp = [(i,name) for i,name in enumerate(names)]
print(indexed_names_comp)

# Unpack an enumerate object with a starting index of one
indexed_names_unpack = [*enumerate(names, start=1)]
print(indexed_names_unpack)
# Use map to apply str.upper to each element in names
names_map  = map(str.upper, names)

# Print the type of the names_map
print(type(names_map))

# Unpack names_map into a list
names_uppercase = [*names_map]

# Print the list created above
print(names_uppercase)
  1. Use numpy instead of list.Numpy is fast and memory efficient alternative to python list.
    • Important methods: .dtype.Numpy array have homogenous data type
    • Numpy array supports broadcasting(vectorization)
    • Better indexing compare to python list
    • Boolean indexing
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.