Boolean-logic condition in Python [duplicate] - python

This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
It's just a little problem I have with logical condition in Python but :
What about a or b == m --> return true !
I'm going to explain with a piece of code
p = 58
m = 100
s = true
p or s == m #returns me 58
s or p == m #returns me 1
p or s # returns me 58
s or p # returns me 1
I can't explain this ! p or s is not equal to 100 .. And also the last 2 condition in the code ! might seem like a silly question but what why comparing a true and an int returns me an int ?
Sorry if I seem stupid ^^'

Related

why is this conditional statement using comparison operators incorrect? [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Comparison operators and 'is' - operator precedence in python?
(2 answers)
Check if all values in list are greater than a certain number
(9 answers)
Closed 6 months ago.
This is a small element of a larger script. Through trial and error I have found that this is the offending line:
>>> print('lower than all') if (3 < (2 and 9 and 9)) is True else print('false')
lower than all
This is clearly incorrect. However changing to this gives the right answer:
>>> print('lower than all') if 3 < (2 and 9 and 9) is True else print('false')
false
I have simplified the example, in reality all the numbers are variables being checked. I really don't understand the difference here!
Again why does this work correctly?!
>>> print('lower than all') if 3 < 2 and 9 and 9 is True else print('false')
false
If you see this chunk, it returns 9
>>> 2 and 9 and 9
9
To understand this, take a look at how the and operation between integers works. From the official doc:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
In this case, 2 is true, 9 is true and the final 9 is also true. Here, the last 9 is y and that's the value being returned.
Try out 2 and 1 and you'll see 1 as the result.
Due to this, your statement
print('lower than all') if (3 < (2 and 9 and 9)) is True else print('false')
Actually becomes
print('lower than all') if (3 < 9) is True else print('false')
And prints 'lower than all'

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.

Character exist at -1 index of a string [duplicate]

This question already has answers here:
Negative indexing in Python [duplicate]
(4 answers)
Closed 6 years ago.
str1 = "hello"
print(str1[-1])
The output of the program is o, but, shouldn't it give error as an output, as nothing exist at -1 index?
Negative indices in Python means they are relative to the end of the sequence. Which means -1 will give you the last, and -2 the second last, etc.
Or, if you prefer, you can think of the string as circular:
-3-2-1 0 1 2 3 4 5 6
...l l o h e l l o h e...

Strange python 'is' operator in arithmetic [duplicate]

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Closed 8 years ago.
I tried this operation in python interactive mode :
>>> (1*1) is 1
True
>>> (377*35) is 13195
False
>>> 377*35
13195
>>> 377*35 is 377*35
False
>>> 1*1 is 1
True
Could anybody explain why ' (377*35) is 13195 ' is false?
Thanks in advance!
A is B checks that A and B refer to the same object. It does not check whether A equals B numerically.
The reason for the different behaviour in your examples is that ints with small values (typically between -1 and 99
inclusive) are "interned" by the interpreter -- whenever a result has such a value,
an existing short int with the same value is returned.
This explains why is returns True for your examples involving small numbers but not for those involving large numbers.

Need help to understand code in python [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
first = int(input('first int: '))
second = int (input('second int: '))
result =0
if first and second:
result =1
elif not first:
result =2
elif first or second:
result=3
else:
result=4
print(result)
when I enter 1 and 0, the result is 3. I would appreciate if anyone could add some explanation.
You are using or- it means the statement will return True when it first finds True.
When you say 5 or 9, both 5 and 9 represent truism (as does any non-zero value). So it returns the first - 5 in this case. When you say 9 or 5, it returns 9.
EDIT: k = 1 or 0 would evaluate to True since 1 represents truism. Thus, as per your code, result is 3
In many program languages, the boolean operations only evaluate their second argument if needed for their outcome. These called short-circuit operator. And in python, according the docs, it returns:
x or y : if x is false, then y, else x
x and y : if x is false, then x, else y

Categories

Resources