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 1017499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:42:53+00:00 2026-05-16T10:42:53+00:00

level: beginner the following code will print ‘False’ def function(x): if len(x) == 5:

  • 0

level: beginner

the following code will print ‘False’

def function(x):
    if len(x) == 5: return True
    else: return x[0] == x[-1]

print function('annb')

why does the line “else: return x[0] == x[-1]” print False?
i do understand what’s happening but i’m having difficulties to put this into plain english…how can this behaviour be described?

is this a commonly / often used “technique”?

I first came across this particular syntax when trying to solve a palindrome exercise recursivley. It seems that the only way to make recursion work is to use this shorthand approach:

def isPalindrome(s):
 if len(s) <= 1: return True
 else: return s[0] == s[-1] and isPalindrome(s[1:-1])

print isPalindrome('anna')

thanks
Baba

  • 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-16T10:42:54+00:00Added an answer on May 16, 2026 at 10:42 am

    Sorry, I’m not entirely sure what you mean, but here think of it this way:

    return (x[0] == x[-1])
    

    If you only consider what is within the parenthesis, you realize that, that ‘statement’ equates to a boolean, right? That’s why you can also do:

    if x[0] == x[-1]
    

    So basically, what is being returned here is a boolean that says whether or not x[0] is equal to [-1].

    One could be more explicit and expand this statement to something like this:

    if x[0] == x[-1]: # if this is true
        return True # then return true
    else:
        return False
    

    But as you can see, both the condition and what you would like to return are the same value, so one just does it shorthand like you saw:

    return x[0] == x[-1]
    

    Sorry if I misunderstood your question.

    EDIT: If you referred to the negative index (x[-1]), in Python, negative indices basically ‘wrap around’, so where as x[0] would be the first element from ‘left-to-right’ so to speak, x[-1] loops around such that it is the first element from ‘right-to-left’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 525k
  • Answers 525k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Django never creates the raw sql, so no. To prevent… May 16, 2026 at 10:14 pm
  • Editorial Team
    Editorial Team added an answer Assuming F2 is the same for every F1 (which is… May 16, 2026 at 10:14 pm
  • Editorial Team
    Editorial Team added an answer Optional words need an epsilon (empty) transition to the next… May 16, 2026 at 10:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is a beginner level question for asp.net MVC I have the following code
This is a beginner-level question. I have a catalog of mtypes: mtype_id name 1
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
I'm a beginner level programmer trying to make a game app for the iphone
I have a beginner level Json question with MVC.net (I've never really used jquery
I'm looking for suggestions for a beginner level ASP/XML test. Essentially I'm trying to
I`m a beginner C++ programmer. And I want to pursue my career in system-
This stems from my question that i started at MVC Beginner Question Im now
I have a top level domain where my main site is located. I want
I'm using low level hooks, but I can't determine what key was pressed. Values

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.