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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:12:17+00:00 2026-06-03T16:12:17+00:00

For an assignment we were asked to define a fibonacci function, which I accomplished

  • 0

For an assignment we were asked to define a fibonacci function, which I accomplished with this:

def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

However, I have seen recursive functions, such as the factorial function, defined in a one line return statement like so:

def factorial(n):
    return n > 1 and n * factorial(n-1) or 1

So, I attempted to apply the same to my fibonacci function. After several attempts, I got it to work for all tested cases except when s = 0, in which case it returns False when it should return 0. Here is where I am:

def fibonacci(n):
    return ((n == 0 or n == 1) and n) or (n > 1 and (fibonacci(n-1) + fibonacci(n-2)))

I understand that python evaluates 0 to False, so how would I have python return zero instead of False when n is 0 while maintaining the current length/structure of the code? Is that even possible?

Also, is this style of creating a function (recursive or otherwise) more or less desirable/pythonic than the textbook version? (I would imagine not just because of readability)

To be clear, I have satisfied the requirements for the assignment, and for personal knowledge only, I would like a clearer understanding of what is happening in the return statement.

  • 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-06-03T16:12:18+00:00Added an answer on June 3, 2026 at 4:12 pm

    The x and y or z idiom doesn’t work if y is falsy. You can swap the condition to make it work nonetheless:

    def fibonacci(n):
        return n >= 2 and fibonacci(n-1) + fibonacci(n-2) or n
    

    However, as of Python 2.5 (released 6 years ago), we have proper conditional expressions and don’t need the and/or hack any longer:

    def fibonacci(n):
        return n if n < 2 else fibonacci(n-1) + fibonacci(n-2)
    

    Now this has an exponential runtime complexity. If you want to be efficient, use the O(n) algorithm:

    def fibonacci(n):
        a, b = 0, 1
        for _ in range(n):
            a, b = b, a + b
        return a
    

    Or even write a generator to yield all the numbers and only take as much as you need.

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

Sidebar

Related Questions

I have this homework assignment where the user is asked to input numbers and
I have an assignment where I'm asked to modify some code. The original function
For an assignment, we were asked to create a function which returns an inverse
I have a programming assignment and we are asked to create a program in
I have been asked to write a program using python for an assignment. I
I have an assignment for my architecture class which to implement a 31 backgammon
another memory management question: I have asked this before, but did not really get
What does it mean to return the client? My teacher asked in an assignment
This is a school assignment and I have most of it under control, but
Earlier I asked this question which was on how to make my own set

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.