While loop performing more loops than instructed [duplicate] - python

This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 3 years ago.
In this piece of code the iterations should stop at one position earlier than they do.
k = 0
while 2^k < 5:
k += 1
print(k)
I expect it to print '3' but it prints out '4'.

^ is the bitwise XOR operator. You probably meant ** for exponentiation.

In Python the ^ operator is bitwise exclusive-or - not exponentiation. Use ** for exponentiation.

Related

Can some describe how this part works "result = n factorial(n-1)" [duplicate]

This question already has answers here:
Understanding factorial recursion [duplicate]
(3 answers)
Python recursive Factorial Function [duplicate]
(6 answers)
Understanding recursion in Python
(4 answers)
Closed 2 years ago.
I apologize if this is a dumb or repeating question firstly. I have been learning python for about 2 weeks now and I keep having small problems with loops. For example.
def factorial(n):
if n < 2: # I understand once the number becomes < than 2 the loop continues
return 1. #and here we return the value 1
result = n * factorial(n-1) #why do we subtract 1 from n?
return result
factorial(5) #my random number
I am having trouble understanding why you subtract 1 from n. Shouldn't you add 1 instead to include the number?
In my mind it looks like this:
result = n(1) * factorial((1)-1) # doesn't n stay = to 1 or am i mistaken?
Thank you for you help. Sorry again if its an idiot question.
E.g. first you call the function with 5, then would you again call it with 5 and multiply, it will overflow the recursion limit and endless loop.
So, you need to decrement by 1 and then call by 4, 3 and so on and when it is less than 2 return 1.

Why is this python 3 statement using a bitwise or instead of an "or"? [duplicate]

This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
Closed 4 years ago.
This python code below I found as part of a problem statement over here:
Divide two integers without using multiplication, division and mod operator
Given a two integers say a and b. Find the quotient after dividing a by b without using multiplication, division and mod operator.
sign = (-1 if((dividend < 0) ^
(divisor < 0)) else 1);
I notice the code doesn't work if you don't have bitwise or operator. Why do you need a "bitwise or" instead of a logical "or" operator?
^ is an exclusive OR, but Python doesn't have a logical exclusive OR. This code takes advantage of the fact that True == 1 and False == 0 so that using the bit-wise exclusive-or has the same effect. (For example, True ^ False == 1 ^ 0 == 1 == True.)
Using regular short-circuiting and and or, then test (with redundant parentheses) would look like
sign = -1 if (dividend < 0 and divisor > 0) or (dividend > 0 and divisor < 0) else 1
That is a bitwise XOR operator. See here for more about XORs.
It works because this is not a bitwise OR operator, but rather a bitwise XOR operator. Check out the Python documentation on bitwise operators.
From Wikipedia:
Exclusive or or exclusive disjunction is a logical operation that outputs true only when inputs differ (one is true, the other is false).

How is this statement including an if statement evaluated? [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 5 years ago.
Suppose that I have the following Python Code:
def fun5(a, b, c):
return a <= b if b <= c else False
fun5(2, 4, 6)
The output of fun5 is True. How is this code being evaluated by Python?
I was expecting a SyntaxError because of the lack of indentations and Python requires indentation.
What you're looking at is called a conditional expression/ternary operator, and it is perfectly valid syntax.
It's equivalent to:
if b <= c:
return a<=b
else:
return False

equals sign after a decrement operator in C++ code [duplicate]

This question already has answers here:
"num - 1" vs "num -= 1"
(9 answers)
Closed 8 years ago.
I have trying to translate code from C++ to python and on one line(in for loop), I have:
x -= (t = u/1.0+ MIN(c, EPS))
I want to know what the '=' sign after a decrement indicates? how can I translate this line in python
thank u
In c assigments are functions at there own, in Python assignments are expressions.
In python this means
t = u/1.0 + min(c, EPS)
x -= t # same as x = x - t

Is There '?' Control Flow in Python? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python Ternary Operator
Is there control flow operator similar to '?' of C/C++ in python?
If there is a chunk of code similar to this:
return n <= 1 ? n : fibo(n-1) + fibo(n-2)
Will got an error like this:
File "fibonacci.py", line 2
return n <= 1 ? n : fibo(n-1) + fibo(n-2)
^
SyntaxError: invalid syntax
Yes, the conditional expression is available in Python 2.5+:
return n if n <= 1 else fibo(n-1) + fibo(n-2)
You can try this short circuit expression return n > 1 and fibo(n-1) + fibo(n-2) or n. While this is not the ternary statement, it is concise and does the job in this scenario.

Categories

Resources