Please explain the output of this python code? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
>>> 0 < 10 != 1 < 5
True
Why is it?? 0<10 is true. 1<5 is also true.True != True should be false 🤔. Then why the the output is True ???

Because of operations priorities meaning of your expression is different. You need to put parentheses: (0 < 10) != (1 < 5), to have what you want.
Otherwise your original expression means same as (0 < 10) and (10 != 1) and (1 < 5) which is not what you expected. (thanks to #TomKarzes)

Related

How to find combination in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I'm trying to make combinations of two number I have facto defined before in my code now I want to use it to find combinations between n and k
I have this already
def facto(n):
res = 1
if n == 0:
return 1
for i in range(1, n+1):
res *= i
return res
now I need
def comb(n, k):

Function that returns even numbers from a list and halves them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I am having trouble concatenating this, I am not allowed to use .append(), and right now Im getting the error 'int' object not iterable.
def halveEvens(l):
num = []
for n in l:
if n % 2 == 0:
num += (n // 2)
return num
print(halveEvens([10,21,32,42,55]))```
sum(x//2 for x in numbers if x%2 == 0)
is probably how I would do it
if you just want to collect them (without summing them)
generator (x//2 for x in numbers if x%2 == 0) would be evaluated as you iterate it
or list comprehension that is evaluated immediatly
[x//2 for x in numbers if x%2 == 0]

Where is the infite loop and how to fix it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a practice quiz question for a course I'm taking. Need to fix the code.
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0:
n = n / 2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
for n = 0, each cycle n is 0, and it will run into next cycle
while n % 2 == 0:
n = n / 2

recursive function that goes through a built in list of numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
need help with this question. Much appreciated.
Write a recursive function big_numbers that takes a Python built-in list of numbers and returns True if each number in the list is greater than 100 and False otherwise.
def isGreater(myList, i=0):
if(i == len(myList)): # Termination True case
return True
if myList[i] > 100:
return isGreater(myList, i+1)
else:
return False # Termination False case
Demonstration:
test = [101, 129, 130]
print(isGreater(test))
True

Python: how to display all possible cases to place brackets [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a number of brackets pairs. I want to display all correct combinations of these brackets. By correct I mean each bracket should be opened before closing in each combination. For example, if the number of brackets is 2 the output should be:
(())
()()
For 3:
((()))
()()()
(()())
(())()
()(())
The order of the output lines doesn't matter.
How can I do it with Python?
Try this code, please:
def __F(l, r, pref):
if r < l or l < 0 or r < 0:
return
if r == 0 and l == 0:
print(pref)
return
__F(l - 1, r, pref + "(")
__F(l, r - 1, pref + ")")
def F(n):
__F(n, n, "")
F(2)
F(3)

Categories