Python Tips and Tricks

Below are some quick fun little projects/tip/tricks that script or use as a one-liner from console using Python.   This code does require that you have a fairly recent version of Python installed (i.e. v3.8 or higher). 


Notes:

Tip: Installing and Using the Jokes Module

Description: Python One-liner jokes for programmers

Requirements: To use the code, you need to Install the following module. At the console type: 

pip3 install pyjokes

Execute the code, type: 

python3 -c "import pyjokes; print(pyjokes.get_joke())"

More Information: 

https://pypi.org/project/pyjokes/ 

Tip: Installing and Using the Quotes Module

Description: A Python wrapper for the Goodreads Quote API

Requirements: To use the code, you need to Install the following module. At the console type: 

pip3 install quotes

Source Code: Create a file with the following code and save it (i.e. quotes.py) directory of your choice.

from quote import quote

author = 'Albert Einstein'

result = quote(author, limit=2)

print(result)

Execute the code, type: 

$ python3 quotes.py

More Information: 

https://pypi.org/project/quote/ 

One-liner trick (Bonus): This trick will allow you to run the code from the command line.

python3 -c "import quote; print(quote.quote('Steven Wright', limit=1)[0]['quote'])"

Tip: Lambda Function (Anonymous Function)

This function can have any number of parameters but, can have just one statement. 

For Example:

a = lambda x, y : x*y

print(a(5, 12))

Tip: List Comprehension

List comprehension is a syntax construction to ease the creation of a list based on existing iterable.

For Example:

a = [i for i in range(1, 10)]

Notice: 

All this information is only provided purposes educational, and can change at anytime. There is no warranty of the quality of the content. So use at your own risk.  I am not endorsing any products, sites, content or authors.