python string comparison / use'list'and'in' instead of'==' and'or'

python string comparison

This output when the content of the argument is'range'or'r' That output when the content of the argument is'ladder' or'l' I want to do that I tried to make a conditional branch by comparing strings

def function1(mode):
	if mode == ('range' or 'r'):
		x=1
	elif mode == ('ladder' or 'l'):
		x=2
	return x
print(function1('r'))


'''Execution result
Traceback (most recent call last):
    print(function1('r'))
    return x
UnboundLocalError: local variable 'x' referenced before assignment
'''

Doesn't it react with ʻor? I tried to reverse the front and back of ==`

def function2(mode):
	if ('range' or 'r') == mode :
		x=1
	elif ('ladder' or 'l') == mode :
		x=2
	return x
print(function2('r'))


'''Execution result
Traceback (most recent call last):
    print(function2('r'))
    return x
UnboundLocalError: local variable 'x' referenced before assignment
'''

What should I do (・ ω ・ \ `) I thought about it and tried using the list and the in operator.

def function3(mode):
	if mode in ['range' ,'r']:
		x=1
	elif mode in ['ladder' ,'l']:
		x=2
	return x
print(function3('r'))

'''Execution result
1
'''

It went well I looked it up and didn't come out, so I thought it might be a problem for someone, so I wrote it down.

It may be embarrassing to make a note in python (・ ω ・ \ `)

By the way, there is also a comparison operator called ʻis`, but this seems to mean something like "Does the object point to itself?" And it seems to be a narrower usage.

Recommended Posts

python string comparison / use'list'and'in' instead of'==' and'or'
(Java, JavaScript, Python) Comparison of string processing
Comparison of 4 Python web frameworks
About Python string comparison operators
Python> Read from a multi-line string instead of a file> io.StringIO ()
Speed comparison of Python XML parsing
Python string
Conversion of string <-> date (date, datetime) in Python
Basic grammar of Python3 system (character string)
Comparison of Python serverless frameworks-Zappa vs Chalice
Comparison of matrix transpose speeds with Python
A memorandum of python string deletion process
Introduction of Python
Python string format
First Python 3 ~ First comparison ~
python string slice
Performance comparison of face detector with Python + OpenCV
[Python3] Coarse graining of numpy.ndarray Speed comparison etc.
Basics of Python ①
Basics of python ①
Calculation of match rate of character string breaks [python]
Thorough comparison of three Python morphological analysis libraries
Copy of python
Simple comparison of Python libraries that operate Excel
Python2 string type
Comparison of R and Python writing (Euclidean algorithm)
Python string format
Python # string type
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
Python string inversion
Comparison of Python and Ruby (Environment / Grammar / Literal)
Introduction of Python
Cut a part of the string using a Python slice
Use urlparse.urljoin instead of os.path.join for Python URL joins
Python implementation comparison of multi-index moving averages (DEMA, TEMA)
A quick comparison of Python and node.js test libraries
Basics of Python learning ~ What is a string literal? ~
Comparison table of frequently used processes of Python and Clojure
I tried using Python (3) instead of a scientific calculator
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
I tried to summarize the string operations of Python
[Python] Operation of enumerate
Python SDP runtime comparison
Unification of Python environment
Copy of python preferences
String manipulation in python
Basics of Python scraping basics
[python] behavior of argmax
Comparison of LDA implementations
Comparison of online classifiers
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
Comparison of fitting programs
[Python2] Date string-> UnixTime-> Date string
Random string generation (Python)
Summary of string operations
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments