Python libraries: Difference between revisions

From GotOpinion
Jump to navigation Jump to search
Created page with "== Matplotlib == import matplotlib.pyplot or import matplotlib.pyplot as plt To Python"
 
m added Seaborn link
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Matplotlib ==
== [https://docs.python.org/3/library/random.html Random] module ==
 
=== Generate random integer with random number of bits ===
 
Examples using IDLE with Python 3.7.2 and <code>>>> from random import getrandbits</code>
 
First I simply return a random integer using 4 as an argument, which produces random integers from 0 to 15 or '0000' to '1111' in binary, , also known as a [https://en.wikipedia.org/wiki/Nibble nibble], or '0' to 'f' in hexadecimal (represented as 0x0 to 0xf)
 
<pre>>>> getrandbits(4)
0
>>> getrandbits(4)
15</pre>
 
Cast the integer into binary using 4, 8, 16, 32, 64, and 128 bits
 
<pre>>>> bin(getrandbits(4))
'0b101'
>>> bin(getrandbits(4))
'0b1010'
>>> bin(getrandbits(4))
'0b1001'
>>> bin(getrandbits(8))
'0b10100110'
>>> bin(getrandbits(8))
'0b1001000'
>>> bin(getrandbits(16))
'0b11001110111101'
>>> bin(getrandbits(32))
'0b10111001011000110110000111011001'
>>> bin(getrandbits(64))
'0b1001111101111000100100110111000101011000010010101001001011100010'
>>> bin(getrandbits(128))
'0b10100111110000000100110000001110001111111101110010110110111111010111111110010000011000111011110000011100101011111001101001010101'</pre>
 
Basic output can be boring. Let us do some formatting with 4, 8, 16, 32, 64, and 128 bits
 
<pre>>>> '{0:08b}'.format(getrandbits(4))
'00001110'
>>> '{0:08b}'.format(getrandbits(8))
'10000010'
>>> '{0:08b}'.format(getrandbits(8))
'01101100'
>>> '{0:08b}'.format(getrandbits(8))
'00101011'
>>> '0x{0:01x}'.format(getrandbits(4))
'0xc'
>>> '0x{0:01x}'.format(getrandbits(4))
'0x6'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xf5'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xfc'
>>> '0x{0:08x}'.format(getrandbits(4))
'0x0000000d'
>>> '0x{0:08x}'.format(getrandbits(8))
'0x00000093'
>>> '0x{0:08x}'.format(getrandbits(16))
'0x0000daed'
>>> '0x{0:08x}'.format(getrandbits(32))
'0x90967fce'
>>> '0x{0:08x}'.format(getrandbits(64))
'0x89cc0e8dc42edf3'
>>> '0x{0:08x}'.format(getrandbits(128))
'0xc8c4348f8f44487d0a13c004405c647c'
>>> '0x{0:040x}'.format(getrandbits(128))
'0x0000000059de380b301667517bba418193c1a799'
>>> '0x{0:080x}'.format(getrandbits(128))
'0x0000000000000000000000000000000000000000000000002565dab4d4fe52fc64294099cc336ba8'</pre>
 
Explanation of string format syntax used above:
* {} places a variable into a string
* 0 takes the variable at argument position 0, which is getrandbits() return value
* : adds formatting options for position 0 variable
* 08 formats the output to eight digits zero-padded on the left
* b converts the integer to binary representation
* x converts the integer to hexadecimal representation
 
See [https://docs.python.org/3/library/string.html#string-formatting Python string formatting] docs for additional information
 
== Python visual ==
 
=== Matplotlib ===


import matplotlib.pyplot
import matplotlib.pyplot
Line 7: Line 88:
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt


[[Python|To Python]]
Example: build a histogram
 
=== Resources ===
 
[https://www.marsja.se/python-data-visualization-techniques-you-should-learn-seaborn/ Python data visualization techniques you should learn with Seaborn] a Python data visualization library based on matplotlib
 
<center>[[Python|To Python]]</center>

Latest revision as of 16:57, 18 July 2019

Random module[edit | edit source]

Generate random integer with random number of bits[edit | edit source]

Examples using IDLE with Python 3.7.2 and >>> from random import getrandbits

First I simply return a random integer using 4 as an argument, which produces random integers from 0 to 15 or '0000' to '1111' in binary, , also known as a nibble, or '0' to 'f' in hexadecimal (represented as 0x0 to 0xf)

>>> getrandbits(4)
0
>>> getrandbits(4)
15

Cast the integer into binary using 4, 8, 16, 32, 64, and 128 bits

>>> bin(getrandbits(4))
'0b101'
>>> bin(getrandbits(4))
'0b1010'
>>> bin(getrandbits(4))
'0b1001'
>>> bin(getrandbits(8))
'0b10100110'
>>> bin(getrandbits(8))
'0b1001000'
>>> bin(getrandbits(16))
'0b11001110111101'
>>> bin(getrandbits(32))
'0b10111001011000110110000111011001'
>>> bin(getrandbits(64))
'0b1001111101111000100100110111000101011000010010101001001011100010'
>>> bin(getrandbits(128))
'0b10100111110000000100110000001110001111111101110010110110111111010111111110010000011000111011110000011100101011111001101001010101'

Basic output can be boring. Let us do some formatting with 4, 8, 16, 32, 64, and 128 bits

>>> '{0:08b}'.format(getrandbits(4))
'00001110'
>>> '{0:08b}'.format(getrandbits(8))
'10000010'
>>> '{0:08b}'.format(getrandbits(8))
'01101100'
>>> '{0:08b}'.format(getrandbits(8))
'00101011'
>>> '0x{0:01x}'.format(getrandbits(4))
'0xc'
>>> '0x{0:01x}'.format(getrandbits(4))
'0x6'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xf5'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xfc'
>>> '0x{0:08x}'.format(getrandbits(4))
'0x0000000d'
>>> '0x{0:08x}'.format(getrandbits(8))
'0x00000093'
>>> '0x{0:08x}'.format(getrandbits(16))
'0x0000daed'
>>> '0x{0:08x}'.format(getrandbits(32))
'0x90967fce'
>>> '0x{0:08x}'.format(getrandbits(64))
'0x89cc0e8dc42edf3'
>>> '0x{0:08x}'.format(getrandbits(128))
'0xc8c4348f8f44487d0a13c004405c647c'
>>> '0x{0:040x}'.format(getrandbits(128))
'0x0000000059de380b301667517bba418193c1a799'
>>> '0x{0:080x}'.format(getrandbits(128))
'0x0000000000000000000000000000000000000000000000002565dab4d4fe52fc64294099cc336ba8'

Explanation of string format syntax used above:

  • {} places a variable into a string
  • 0 takes the variable at argument position 0, which is getrandbits() return value
  • : adds formatting options for position 0 variable
  • 08 formats the output to eight digits zero-padded on the left
  • b converts the integer to binary representation
  • x converts the integer to hexadecimal representation

See Python string formatting docs for additional information

Python visual[edit | edit source]

Matplotlib[edit | edit source]

import matplotlib.pyplot

or

import matplotlib.pyplot as plt

Example: build a histogram

Resources[edit | edit source]

Python data visualization techniques you should learn with Seaborn a Python data visualization library based on matplotlib

To Python