python_notes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python_notes [2023/05/19 20:53] – [read file into a list] admin | python_notes [2024/01/03 22:15] (current) – [useful links] raju | ||
---|---|---|---|
Line 2: | Line 2: | ||
==== useful links ==== | ==== useful links ==== | ||
+ | * [[https:// | ||
+ | * very easy to follow; high information density; well thought out examples | ||
* [[https:// | * [[https:// | ||
* can be useful while doing live coding interviews. | * can be useful while doing live coding interviews. | ||
* [[https:// | * [[https:// | ||
+ | ==== links to documentation ==== | ||
+ | |||
+ | * os.path.basename - https:// | ||
+ | * continue statement - https:// | ||
==== validate inputs ==== | ==== validate inputs ==== | ||
Sample snippet: | Sample snippet: | ||
Line 115: | Line 121: | ||
</ | </ | ||
+ | ==== check if something is float ==== | ||
+ | < | ||
+ | % python | ||
+ | Python 3.11.4 (main, Jul 5 2023, 13:45:01) [GCC 11.2.0] on linux | ||
+ | Type " | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | False | ||
+ | >>> | ||
+ | False | ||
+ | </ | ||
+ | ==== check if something is int ==== | ||
+ | < | ||
+ | % python | ||
+ | Python 3.11.4 (main, Jul 5 2023, 13:45:01) [GCC 11.2.0] on linux | ||
+ | Type " | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | False | ||
+ | >>> | ||
+ | False | ||
+ | </ | ||
+ | |||
+ | ==== Python comparisons ==== | ||
+ | |||
+ | < | ||
+ | $ python | ||
+ | Python 3.9.17 (main, Jul 5 2023, 20:47:11) [MSC v.1916 64 bit (AMD64)] on win32 | ||
+ | Type " | ||
+ | >>> | ||
+ | False | ||
+ | >>> | ||
+ | True | ||
+ | </ | ||
+ | |||
+ | ==== Chained comparisons ==== | ||
+ | Q: In Python '' | ||
+ | |||
+ | < | ||
+ | $ python | ||
+ | Python 3.9.17 (main, Jul 5 2023, 20:47:11) [MSC v.1916 64 bit (AMD64)] on win32 | ||
+ | Type " | ||
+ | >>> | ||
+ | False | ||
+ | </ | ||
+ | |||
+ | Why? | ||
+ | |||
+ | A: The ''<'' | ||
+ | |||
+ | So '' | ||
+ | |||
+ | Ref:- | ||
+ | * https:// | ||
+ | |||
+ | ==== List of comparison operators ==== | ||
+ | < | ||
+ | "<" | ||
+ | | " | ||
+ | </ | ||
+ | |||
+ | Ref:- https:// | ||
===== tasks ===== | ===== tasks ===== | ||
* [[get urls in a url]] | * [[get urls in a url]] | ||
Line 121: | Line 191: | ||
===== difference between ===== | ===== difference between ===== | ||
+ | ==== os.getenv vs. os.environ.get ==== | ||
+ | What is the difference between os.getenv vs os.environ.get? | ||
+ | None. I prefer to use os.getenv as it is less number of characters to type. | ||
+ | |||
+ | To experiment, use [[https:// | ||
+ | |||
+ | Sample usage from https:// | ||
+ | < | ||
+ | def __init__(self, | ||
+ | ... | ||
+ | username = os.getenv(" | ||
+ | password = os.getenv(" | ||
+ | if username and password: | ||
+ | self.login(username, | ||
+ | </ | ||
===== Snippets ===== | ===== Snippets ===== | ||
Line 244: | Line 329: | ||
tags | file readlines strip newline | tags | file readlines strip newline | ||
+ | |||
+ | Another approach is to use read() with splitlines(). | ||
+ | |||
+ | < | ||
+ | In [6]: | ||
+ | file = ' | ||
+ | with open(file) as fh: | ||
+ | lines = fh.read().splitlines() | ||
+ | print(lines) | ||
+ | [' | ||
+ | </ | ||
+ | |||
+ | ===== programming notes ===== | ||
+ | ==== which python executable am I running? ==== | ||
+ | < | ||
+ | import sys | ||
+ | print(sys.executable) | ||
+ | </ | ||
+ | |||
+ | For example | ||
+ | |||
+ | < | ||
+ | $ python | ||
+ | Python 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] on win32 | ||
+ | Type " | ||
+ | >>> | ||
+ | >>> | ||
+ | C: | ||
+ | </ | ||
===== programming related ===== | ===== programming related ===== |
python_notes.1684529584.txt.gz · Last modified: 2023/05/19 20:53 by admin