Control flow in python with strings [duplicate] - python

This question already has answers here:
Python 2.7 Boolean Operators Logic
(1 answer)
How does boolean operator work on string in python
(6 answers)
Closed 2 years ago.
I have a very basic snippet of code.
print("a" or "b")
>> a
I'm not entirely sure what I expected to happen.
Why does this print "a", and in general how are strings handled with python control flow?

In python, empty strings evaluate to False in a boolean context, while non-empty strings are True.

Related

Case-insensitive string comparison without using lower or upper [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 3 years ago.
I am trying to compare two string and ignore case- sensitive but i don't want to use .lower() or .upper(). Is there another way of doing this?
example1 = "hello"
example2 = "HeLlo"
if example1.casefold()==example2.casefold():
#do something
This will work without needing upper() or lower(). Might not work for non-ASCII characters.

Does Python Check ALL conditions in an multi-condition if statement? [duplicate]

This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 3 years ago.
I am curious if Python will continue checking conditions in an if statement if the first condition returns False. I'm wondering about this because I want to know if best practice is try to check conditions with low time-complexity before more complex checks.
Is there any difference between these two snippets?
if condition_1() and condition_2():
do_something()
and
if condition_1():
if condition_2():
do_something()
Yes, python boolean operators do short-circuit
Both code samples are semantically equivalent, but the first is more readable, as it has lower level of nesting.

What does this use of Python 'or' operator do? [duplicate]

This question already has answers here:
Boolean Python Value confusion
(2 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 4 years ago.
I came across this piece of Python code. The precise function is not important. I'm trying to understand how the arguments to the json.dumps call work. It looks (at a guess) like the intent is to use either body or kwargs['body'] depending on which one is non-null, but doesn't the or operator just return a boolean?
import json
from aiohttp import web
def json_response(body='', **kwargs):
kwargs['body'] = json.dumps(body or kwargs['body']).encode('utf-8')
kwargs['content_type'] = 'text/json'
return web.Response(**kwargs)

Ruby's "next" implementation in Python [duplicate]

This question already has answers here:
How can I get the next string, in alphanumeric ordering, in Python?
(4 answers)
Closed 6 years ago.
If Python has an implementation of Ruby's next method? I mean something what works exactly the same as in Ruby, so if I type e.g. "z".next it will return "aa" (instead of just next sign in ascii table), "az".next will return "ba" and so on.
I don't believe there is a built-in method for this in Python. A similar question was asked on How can I get the next string, in alphanumeric ordering, in Python? and the accepted answer gives a solution.

Difference between != and <>? [duplicate]

This question already has answers here:
What does `<>` mean in Python?
(5 answers)
Closed 8 years ago.
I just saw some code using the <> operator (don't know what this is called) instead of the != operator in Python. Is there any difference between the two or do they mean the same thing? What's the <> operator called? Thanks.
The <> operator is considered obsolete:
https://docs.python.org/2/reference/expressions.html#not-in

Categories