This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for n in range(2, 10): | |
| for x in range(2, n): | |
| if n % x == 0: | |
| print(n, 'equals', x, '*', n//x) | |
| break | |
| else: | |
| # loop fell through without finding a factor | |
| print(n, 'is a prime number') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> def get_status(error): | |
| ... return 'OK' if not error else 'We have a problem' | |
| ... | |
| >>> get_status(error=True) | |
| 'We have a problem' | |
| >>> get_status(error=False) | |
| 'OK' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import sys | |
| >>> l = [1,2,3] | |
| >>> t = (1,2,3) | |
| >>> sys.getsizeof(l) | |
| 120 | |
| >>> sys.getsizeof(t) | |
| 64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import subprocess | |
| thedir = input() | |
| >>> result = subprocess.run([f'ls -al {thedir}'], shell=False) | |
| Traceback (most recent call last): | |
| File "<stdin>", line 1, in <module> | |
| File "/usr/lib/python3.8/subprocess.py", line 489, in run | |
| with Popen(*popenargs, **kwargs) as process: | |
| File "/usr/lib/python3.8/subprocess.py", line 854, in __init__ | |
| self._execute_child(args, executable, preexec_fn, close_fds, | |
| File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import subprocess | |
| thedir = input() | |
| # Don't do this: | |
| result = subprocess.run([f'ls -al {thedir}'], shell=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import subprocess | |
| >>> result = subprocess.run(['ls -al | head -n 1'], shell=True) | |
| total 396 | |
| >>> result | |
| CompletedProcess(args=['ls -al | head -n 1'], returncode=0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import subprocess | |
| >>> subprocess.run(['ls', '-al']) | |
| (a list of your directories will be printed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import subprocess | |
| >>> code = """ | |
| ... for i in range(1, 3): | |
| ... print(f"Hello world {i}") | |
| ... """ | |
| >>> result = subprocess.run(['python3'], input=code, capture_output=True, encoding='UTF-8') | |
| >>> print(result.stdout) | |
| >>> print(result.stdout) | |
| Hello world 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import subprocess | |
| >>> result = subprocess.run(['python3', '--version'], capture_output=True, encoding='UTF-8') | |
| >>> result | |
| CompletedProcess(args=['python3', '--version'], returncode=0, stdout='Python 3.8.5\n', stderr='') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import subprocess | |
| >>> result = subprocess.run(['python3', '--version']) | |
| Python 3.8.5 | |
| >>> result | |
| CompletedProcess(args=['python3', '--version'], returncode=0) |