I likelove Python the language. The libraries are a bonus and the reason I use Python. Here are a few examples. Notice there are no imports, just pure Python. If “using Python” to you means using Pandas, numpy, TensorFlow, and matplotlib, this list is unlikely to resonate. But if you are looking for a programming language it just might. As ever, YMMV.
# lambda functions, factorial function, and arbitrary precision arithmeticfact =lambda x: x ==0or x * fact(x -1)x = fact(1000)x
# dictionaries, comprehensions, and string manipulation# count the frequency of digits in 1000!x =str(x)freq = {i: x.count(i) for i in'0123456789'}freq
# string manipuationlast_first = characters = ["Corleone, Vito", "Adams, Kay", "Tessio, Salvatore", "Clemenza, Peter", "Hagen, Tom"]first_last = [' '.join(i[::-1]) for i in [n.split(', ') for n in last_first]]first_last
('negaH moT aznemelC reteP oisseT erotavlaS smadA yaK enoelroC otiV',
'Vt oloeKyAasSlaoeTsi ee lmnaTmHgn')
# built in classes# almost all the programming I do builds classesclass MyClass():def__init__(self):self.fact =staticmethod(fact)self.y =20c = MyClass()c.x =10c.x + c.y, c.fact(6)
(30, 720)
# tricks with classes and types: not text book, but clever it worksC =type('MyClass', (), {'fact': staticmethod(fact), 'y': 20})c = C()c.x =10c.x + c.y, c.fact(6)
(30, 720)
# introspection: all the non-internal methods of str# love the depth of built in str supportmethods = [s for s indir(str) if s[0] !='_']# print first 5print('\n'.join(methods[:5]))
capitalize
casefold
center
count
encode
# get the help on the first 2, f-stringsprint('\n\n'.join([f'{n}\n{"="*len(n)}\n{n.__doc__}'for n in methods[:2]]))
capitalize
==========
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
casefold
========
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
# flexible selection[(first, len(z), last) for (first, *z, last) in last_first]
# argv and kwargs, built in helpdef f(x, y, *argv, **kwargs):"""My example function with argv and kwargs"""# oh, and strings auto concatenate ans = (f'f called with {x=}{y=}, a vector of length {len(argv)}, 'f'and keywords with keys {kwargs.keys()}')print(ans)f(3, 'string', 'a', 1, 2, 'z', arg1='arg1', arg2=10)f(42, 'bare')
f called with x=3 y='string', a vector of length 4, and keywords with keys dict_keys(['arg1', 'arg2'])
f called with x=42 y='bare', a vector of length 0, and keywords with keys dict_keys([])
help(f)
Help on function f in module __main__:
f(x, y, *argv, **kwargs)
My example function with argv and kwargs
# decoratorsdef announce(f):def decf(*argv, **kwargs):print(f'Function f called with {argv=} and {kwargs=}') ans = f(*argv, *kwargs)print(f'Answer {ans}')return ansreturn decf@announcedef my_func(x, y):return x * x + y * ymy_func(1, 2)
Function f called with argv=(1, 2) and kwargs={}
Answer 5
5
# sets, assertionss1 =set('the quick brown fox jumped over the lazy dog')assertlen(s1) ==26s2 =set('aeiou')# set operations, e.g.s1 - s2