2
def get(self){
   n=self.get.request(n,0)
   n=n and int(n)
   self.render(fizzbuzz.html,n=n)
}

In this what is the meaning of line n=n and int(n) this is lines of codes are took from udacity cs253 course. and yes can u also suggest me any similar course but an updated because cs 253 is way to old...

aijax
  • 47
  • 10

2 Answers2

4

The and operator returns the value of the first "falsey" value it sees, or the second value if both are true. So you have:

>>> 0 and False
0
>>> False and 0
False
>>> 0 and 1
0
>>> -2 and 0
0
>>> 5 and 3
3

In your case, you have n and int(n). If n or int(n) is zero, this will return 0. If both are nonzero, it will return int(n).

It's worth remarking that (a) this code is unnecessarily confusing, and (b) it could be simplified just by using int(n); if n is zero, then int(n) is zero, so there's no point in checking n first.

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
2

The statement

n = n and int(n)

will first evaluate n. If its value is 0, or "", or False (or a number of other "falsy" values) then n will be unchanged. If n is anything else, then int(n) will be called to convert it to an integer and the result will be assigned to n.

This is a rather unusual thing to write and it's not clear from the code you posted why the author didn't simply write n = int(n).

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285