Why the result of “bag” > “apple” is True in Python? [duplicate] - python

This question already has answers here:
How are strings compared?
(7 answers)
Closed 2 years ago.
Why the result of “bag” > “apple” is True in Python?
I tried this code below i don't know why it show this result and how? Please some one explain it.
print("bag" > "apple")
True

I believe this is True because Python compares the first letter of each word. And b is greater than a in Python.

Related

how to use Ternary operator in python. It works different for me [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 19 days ago.
can anyone tell me why this is so? In the below code you can see the output is different for the same lines of codes with different variables name. I am not able to understand the idea behind the ternary operator in python.
a = "0"
b = "1"
c = a=="0" and 1 or 2
print(c)
output = 1
category_name = "1"
outfsheetno = category_name=="1" and 0 or 1
print(outfsheetno)
output = 1

Weird output in Python program [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 3 years ago.
I tried to make a program to determine whether a number is odd or not, I get the following output
def is_odd(n):
if n % 2 != 0:
print(True)
else:
print(False)
if I put
print(is_odd(1))
I get:
True
None
Whatever number I choose, I always get
None
at the end.
You have to return something to remove this None . You can also modify the code by returning the True or false and print it in the main

== and is not returning the same even though comparing an int [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I'm using the pow function and found out I had a bug in my code because == and is were not having the same behavior.
Here goes an example: pow(3, 47159012670, 47159012671) == 1 returns True but pow(3, 47159012670, 47159012671) is 1 returns False.
I'd like to know what is it that I'm not getting.
The difference between is and == is:
a1 is a2 # return True
only when they share the same location or id in memory, that means when id(a1) and id(a2) are the same. I hope this will help more.

Is str.count() wrong? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
How String.count() works? [duplicate]
(2 answers)
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
I'm having fun with some challenges and one of them makes me count substrings in a string. I have a problem specifically with "banana":
str = "banana"
print(str.count("ana"))
This should return 2 because "ana" appears two times:
b a n a n a
a n a
a n a
But str.count("ana") returns only 1. I've also tried with regexp:
import re
str = "banana"
print(len(re.findall("ana", str)))
But it also returns 1. Am I missing something?
thank you!
Yes, you are missing something.
str.count(): Return the number of (non-overlapping) occurrences of substring sub in string s

Printing integers in python list in correct orderly manner [duplicate]

This question already has answers here:
Pythonic way to check if a list is sorted or not
(27 answers)
Closed 6 years ago.
I have this list from user input:
sun=[8,8,7,7,4,6,4,5,]
>>> sun.index(8)
0
>>> sun.index(7)
2
>>> sun.index(4)
4
Would like to write a program to print "Impossible", because 4,5,6 are not in correct order in the list.
print "Impossible" * (sun != sorted(sun))

Categories