User Tools

Site Tools


python_notes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
python_notes [2023/11/17 19:29] – [programming notes] rajupython_notes [2023/12/31 19:54] – [List of comparison operators] raju
Line 143: Line 143:
 </code> </code>
  
 +==== Python comparisons ====
 +
 +<code>
 +$ python
 +Python 3.9.17 (main, Jul  5 2023, 20:47:11) [MSC v.1916 64 bit (AMD64)] on win32
 +Type "help", "copyright", "credits" or "license" for more information.
 +>>> 4 == True
 +False
 +>>> 1 == True
 +True
 +</code>
 +
 +==== Chained comparisons ====
 +Q: In Python ''3 < 4 == True'' evaluates to False.
 +
 +<code>
 +$ python
 +Python 3.9.17 (main, Jul  5 2023, 20:47:11) [MSC v.1916 64 bit (AMD64)] on win32
 +Type "help", "copyright", "credits" or "license" for more information.
 +>>> 3 < 4 == True
 +False
 +</code>
 +
 +Why?
 +
 +A: The ''<'' and ''=='' are comparison operators. If a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then in python, a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.
 +
 +So ''3 < 4 == True'' is equivalent to ''3 < 4 and 4 == True'' which evaluates to ''True and False'' which turns out to be ''False''.
 +
 +Ref:-
 +  * https://docs.python.org/3/reference/expressions.html#comparisons - describes the above rule and also gives a list of comparison operators
 +
 +==== List of comparison operators ====
 +<code>
 +"<" | ">" | "==" | ">=" | "<=" | "!="
 +| "is" ["not"] | ["not"] "in"
 +</code>
 +
 +Ref:- https://docs.python.org/3/reference/expressions.html#comparisons
 ===== tasks ===== ===== tasks =====
   * [[get urls in a url]]   * [[get urls in a url]]
Line 308: Line 347:
  
 <code> <code>
-$python +$ python 
-Python 3.6.|Anaconda 4.4.0 (64-bit)| (defaultMay 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32+Python 3.11.packaged by Anaconda, Inc. | (mainSep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] on win32
 Type "help", "copyright", "credits" or "license" for more information. Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys >>> import sys
 >>> print(sys.executable) >>> print(sys.executable)
-C:\ProgramData\Continuum\Anaconda\envs\py36\python.exe+C:\Users\kkusuman\AppData\Local\conda\conda\envs\py311\python.exe
 </code> </code>
  
python_notes.txt · Last modified: 2024/01/03 22:15 by raju