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

Related Questions

Level: Beginner In the following code my 'samePoint' function returns False where i am
This is a beginner level question for asp.net MVC I have the following code
Excuse the beginner level of this question. I have the following simple code, but
Question : Beginner Level Code: Pure Javascript I had created a web page in
i am writing javascript code on nodepad++,i am at beginner level and write just
I'm a JavaScript beginner and managed to piece together to following code. It gets
I'm at beginner level in C programming language. I'm trying to print out a
(VB Express Level: Beginner) I want to do following, A column from Workbook 1
whats is wrong with following? (Level:Beginner) I want to add the answers obtained after
Level: Beginner I'm doing my first steps in Object Oriented programming. The code is

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.