Skip to content

Instantly share code, notes, and snippets.

@archmangler
Last active May 15, 2020 08:18
Show Gist options
  • Save archmangler/0cf153ac0ae4ca13e869f9cd8de4af31 to your computer and use it in GitHub Desktop.
Save archmangler/0cf153ac0ae4ca13e869f9cd8de4af31 to your computer and use it in GitHub Desktop.
Python 3 Adventures 2020

A Journal of Tiny Python 3 Lessons and Notes

#0003 really simple command line arguments management

#!/usr/bin/env python3
#Purpose: Say hello

import argparse
parser = argparse.ArgumentParser(description='Say Hello')
parser.add_argument('name',help='Name to greet')
args = parser.parse_args()
print('Hello, ' + args.name + '!')

#0002 (tiny python projects)

  • keywords = [ pytest, black/yapf, flake8/pylint, new.py ]
  • python3 -m pip install black flake8 ipython mypy pylint pytest yapf

#0001

#!/usr/local/bin/python3.7
#playing with yield (generators)

def infinite_sequence():
  num = 0
  while True:
   yield num
   num += 1

#Question: Why does this not work?
result=next(infinite_sequence())
print(result)
result=next(infinite_sequence())
print(result)
result=next(infinite_sequence())
print(result)

#But this does ...
for item in infinite_sequence():
 print(f"{item}")
 print("\n")

Breadcrumbs

  • Comprehensions: list, set, and dictionary comprehensions
  • Generator expressions
  • Generator functions: Generator functions use the Python yield keyword instead of return.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment