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:27] – [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 193: | Line 278: | ||
===== Reading files ===== | ===== Reading files ===== | ||
==== read file into a list ==== | ==== read file into a list ==== | ||
+ | Given | ||
< | < | ||
- | $ cat great.txt | + | $ cat -A great.txt |
- | kamaraju | + | kamaraju$ |
- | kamaraj | + | kamaraj |
- | kamara | + | kamara |
- | kamar | + | kamar $ |
- | kama | + | kama $ |
- | kam | + | kam $ |
- | ka | + | ka $ |
- | k | + | k $ |
</ | </ | ||
+ | where the $ denotes the end of line. | ||
+ | < | ||
+ | $ du -b great.txt | ||
+ | 72 great.txt | ||
+ | $ wc great.txt | ||
+ | | ||
+ | </ | ||
+ | |||
+ | To read it | ||
< | < | ||
- | In [2]: file = ' | + | In [3]: |
- | ...: with open(file) as fh: | + | file = ' |
- | ...: contents = [line.rstrip() for line in fh] | + | with open(file) as fh: |
- | ...: print(contents) | + | contents = [line.rstrip(' |
- | [' | + | print(contents) |
+ | [' | ||
- | In [3]: len(contents) | + | In [4]: |
- | Out[3]: 8 | + | len(contents) |
+ | Out[4]: | ||
+ | 8 | ||
</ | </ | ||
Ref:- https:// | Ref:- https:// | ||
- | If you use readlines(), there will be a new line character at the end. | + | You can also do it using readlines. |
+ | |||
+ | < | ||
+ | In [5]: | ||
+ | file = ' | ||
+ | with open(file) as fh: | ||
+ | lines = fh.readlines() | ||
+ | lines = [line.rstrip(' | ||
+ | print(lines) | ||
+ | [' | ||
+ | </ | ||
+ | |||
+ | 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 | ||
< | < | ||
- | In [4]: file = ' | + | $ python |
- | | + | Python 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] on win32 |
- | ...: contents = fh.readlines() | + | Type " |
- | | + | >>> |
- | [' | + | >>> |
+ | C:\Users\kkusuman\AppData\Local\conda\conda\envs\py311\python.exe | ||
</ | </ | ||
python_notes.1684528043.txt.gz · Last modified: 2023/05/19 20:27 by admin