Reaallllly simple question

In python, why does:

>>> print('hello')
hello

and:

>>> print(variable1)
23

but:

>>> print('hello', variable1)
('hello', '23')

And perhaps more importantly, what do I do to get: hello 23

ROFL
Nevermind, I did it. Just remove the brackets.
LOL, what is it with me and working something out the second I ask about it!!

“()” is a ‘tuple’ constructor.

i.e. it effectively constructs a read-only list from the parameters provided. If you “print” a tuple (or list), it will show you it’s contents essentially in the form used to create it in the first place. Try;

a=(1,2,3,4,5)
print a
print a[1]
a[1]=9

The last line will error as being a tuple, it’s read-only. If you try;

a=[1,2,3,4,5]
print a
print a[1]
a[1]=9

The results will be similar except the last line won’t error, “” is a list constructor. (lists are read/write)

To really bend your mind a little “(1)” will not generate a tuple as it thinks it’s an arithmetic expression, i.e. a “1” in brackets … “(1,2)” will generate a tuple because it’s obviously not an arithmetic expression, so if you want a tuple containing a single numeric entry, you need to write “(1,)”. An example of when this is useful;

#Add an additional digit to a tuple;
a=(1,2,3)
a+4 # will not work
a+(4) # will not work
a+(4,) , will work and yields (1,2,3,4)

Exercise, work out why / how this works;

a=('a','b','c')
"-".join(a)

MP Loves Python … :smiley:

I’m probably way off here, but is it because the (‘a’,‘b’,‘c’) is the list but the contents are the strings a b and c and the joining is happening outside of the strings, like is the read only part the strings themselves rather than the whole construct?
/making a fool of herself

:slight_smile:

What I was getting at, is that the “-” is a string. Strings are objects, which have a number of methods attached to them, one of which is “join”. The join method takes one parameter, a list in this instance, and uses the base object (i.e. “-”) to join all the elements of what it was supplied with (i.e. the tuple)

It’s worth noting the difference, tuple / list … subtle but the cause of many bugs! You can add items to a tuple, but you can’t change them (!) Lists, you can add and change … tuple is “()” , list is “” … :wink:

That makes sense. And I wasn’t so far off really :stuck_out_tongue:

Ok, if you’re feeling adventurous ;

def factorial(n): return 1 if n==1 else n*factorial(n-1)

For 15% of the marks - what does it do and how would you use it ?
For another 35% - in context, what sort of function is this and what sort of if-test is this ?
For the remaining 50% - what’s the methodology’s major shortcoming and why would you generally never use it in real programming ?

:wink:

(if you try this and it errors, you need a newer version if Python, get v2.6+)

For a bonus 25%, rewrite the function in one line of “C” … :o

Is this an open book test?
(uh… kind of too late if it isn’t…)

  1. uh it finds the factorial which is what you get if you multiply every number up to and including the number in question.

  2. Wasn’t really sure what you were aiming for. Recursive function? Integer function?

  3. fact.factorial(-3) OR fact.factorial(9.3)
    blah blah blah blah

    RuntimeError: maximum recursion depth exceeded.

  4. (AHH one thing at a time! :p) googles looks extremely confused

UHHH, given I know nothing about this topic and when I tried google I found impenetrable things I hereby declare that if you void all rights to laugh at me if this is completely wrong.

int factorial(int n)
{
if (n == 1)
return 1
else
return 1 * factorial(n-1)
}

  1. 100% - 15 marks
  2. 50% - 17.5 marks
  3. 50% - 25 marks
  4. 0%

Giving you a total of %57.50! :wink:

  1. The if-test is an “inline if”

  2. You were sort of there with the stack overflow, but I’m thinking maybe for the wrong reason (?) Recursive functions rely on the stack for their variable storage, different languages and indeed different settings within each language vary, so you can never “know” how deeply you can recurse before you experience a stack overflow. If you “know” for example that all Python configurations will recurse to at least a depth of 40 frames, and your maximum requirement is !10, the function is a very elegant solution. However, for general problem solving this sort of specification is not generally something you can rely on and iterative processes that do not have such a limitation, although less ‘elegant’, should always be preferred.

  3. I’m pretty sure that’s not one line (!) although you could write it on one line, the point was to replicate the in-line “if” in “C”;

int factorial(int n) { return n==1 ? n : n*factorial(n-1); }

Ultimate point being; all languages started out as discrete, as time progresses they’re all morphing into “C” one …
(inline-if is new in Python, appeared in 2.5 as an extension and is available in 2.6 by default)

And you’re missing a “;” after both returns, and you have “return 1 * factorial(n-1)” rather than "return nfactorial(n-1)" … with the added bonus of 1 anything being surplus to requirements … :wink:

For the pink bunny and free tickets for the raffle, can you show the assembler version of the C function … :wink:

The Fraction is the typical example to explain a recursion, but in practice it is never used for this purpose, because the range of int or long-int is reached with a few steps, in practice you are much better off with something like:


int FAC (int    iIn)
{
        if ((iIn == 0) || (iIn == 1))
                return 1;
        else if (iIn == 2)
                return 2;
        else if (iIn == 3)
                return 6;
        else if (iIn == 4)
                return 24;
        else if (iIn == 5)
                return 120;

/* etc. */

BTW - if want a faction over R or C, you may have a look into the Gamma-Function Gamma function - Wikipedia

I am beginning to regret dropping out of A level maths…

They made us sit through a whole year of two different Hons maths courses, in addition to a numerical analysis course. For what it’s worth, 25 years on, the mathematics have proven to be 100% useless to me … I strongly suspect they taught us the maths because their computing was so crap … I’m pretty sure that Eigenvalues and Eigenvectors have their commercial applications, but trying to teach it at first year computer geeks does strike me as a monumental waste of time.

I learnt two things at University.

a. The concept of a recursive function.
b. How to drink lots of beer.
c. How poor all some computing academics are at computing

For anyone planning on doing a Uni degree these days and getting themselves into lots of debt, consider that you can teach (a) to a trained monkey in a day and to your average computer student in 5 minutes. So I guess whether it’s worthwhile depends to a greater extent how much you like beer … :wink:

Recursions were the great fashion of programmers in 1970s and early 1980s - especially they can look really cool 8) and it was near-by standard with some Pascal programmers to use the recursion anywhere if even remotely possible.

In the 1980s there was a swift, because a recursion is handled via the stack, and all local variables, including function/procedure parameters are stored in the stack and the stack has to be re-winded by the end of the recursion, causing performance problems or stack overflows. Since the dogma shifted and recursions are considered as the absolute No-No.

I personally prefer the middle way and use recursions in certain situations as the most elegant way of doing thinks, but limited.