Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6792155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:56:04+00:00 2026-05-26T17:56:04+00:00

PEP 8 doesn’t mention the slice operator. From my understanding, unlike other operators, it

  • 0

PEP 8 doesn’t mention the slice operator. From my understanding, unlike other operators, it should not be surrounded with whitespace

spam[3:5]   # OK
spam[3 : 5] # NOT OK

Does this hold when using complex expressions, that is, which one is considered better style

     1. spam[ham(66)//3:44+eggs()]
     2. spam[ham(66) // 3: 44 + eggs()]
     3. spam[ham(66) // 3 : 44 + eggs()]
     4. something else?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:56:04+00:00Added an answer on May 26, 2026 at 5:56 pm

    As you already mentioned, PEP8 doesn’t explicitly mention the slice operator in that format, but spam[3:5] is definitely more common and IMHO more readable.

    If pep8 checker is anything to go by, the space before : will be flagged up

    [me@home]$ pep8  <(echo "spam[3:44]")   # no warnings
    [me@home]$ pep8  <(echo "spam[3 : 44]")  
    /dev/fd/63:1:7: E203 whitespace before ':'
    

    … but that’s only because of it assumes : to be the operator for defining a literal dict and no space is expected before the operator. spam[3: 44] passes for that reason, but that just doesn’t seem right.

    On that count, I’d stick to spam[3:44].


    Nested arithmetic operations are a little trickier. Of your 3 examples, only the 2nd one passes PEP8 validation:

    [me@home]$ pep8 <(echo "spam[ham(66)//3:44+eggs()]")
    /dev/fd/63:1:13: E225 missing whitespace around operator
    
    [me@home]$ pep8 <(echo "spam[ham(66) // 3:44 + eggs()]")  # OK
    
    [me@home]$ pep8 <(echo "spam[ham(66) // 3 : 44 + eggs()]")
    /dev/fd/63:1:18: E203 whitespace before ':'
    

    However, I find all of the above difficult to parse by eye at first glance.

    For readability and compliance with PEP8, I’d personally go for:

     spam[(ham(66) // 3):(44 + eggs())]
    

    Or for more complication operations:

     s_from = ham(66) // 3 
     s_to = 44 + eggs()
     spam[s_from:s_to]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From PEP 8 : - Imports should usually be on separate lines, e.g.: Yes:
I don't know why this doesn't work: I'm using the odict class from PEP
From reading PEP-8, I get it that you should put the closing parenthesis on
I learned from the websitethat I should add the code declaration in python when
First, I couldn't find an answer to this in PEP 8 . That doesn't
PEP 8 says that Python package and module names should be short, since some
Using the following query, WITH cteTest (Employee_ID) AS ( SELECT employee_ID FROM pep.dbo.labor_ticket )
The PEP 8 style guide (Python) says methodnames should be lowercase and that sometimes
Is there a Python design decision (PEP) that precludes a sorted container from being
I'm having some trouble understanding the Importer Protocol as per http://www.python.org/dev/peps/pep-0302/ . Should the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.