Python

Python Random getrandbits() Method with Examples

In Python, the random module is used to generate random numbers. This is not truly random; rather, it is used to generate pseudo-random numbers. This implies that these numbers can be determined at random.

random getrandbits() Method in Python:

getrandbits() method returns an integer of the specified size (in bits).

Syntax:

random.getrandbits(num)

Parameters:

num: This is Required. A number indicating the size of the returned integer in bits.

Examples:

Example1:

Input:

Given number(size) = 6

Output:

The random integer =  56

Example2:

Input:

Given number(size) = 10

Output:

The random integer = 59

Random getrandbits() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the number(size) as static input and store it in a variable.
  • Pass the given number as an argument to the random.getrandbits() method that returns an integer of the given size in bits.
  • Store it in another variable.
  • Print an integer with a size of a given number of bits.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number(size) as static input and store it in a variable.
gvn_numb = 6
# Pass the given number as an argument to the random.getrandbits() method that
# returns an integer of the given size in bits.
# Store it in another variable.
rslt = random.getrandbits(gvn_numb)
# Print an integer with a size of a given number of bits.
print("The random integer = ", rslt)

Output:

The random integer =  56

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the number(size) as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the random.getrandbits() method that returns an integer of the given size in bits.
  • Store it in another variable.
  • Print an integer with a size of a given number of bits.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the random.getrandbits() method that
# returns an integer of the given size in bits.
# Store it in another variable.
rslt = random.getrandbits(gvn_numb)
# Print an integer with a size of a given number of bits.
print("The random integer = ", rslt)

Output:

Enter some random number = 10
The random integer = 59

 

Python Random getrandbits() Method with Examples Read More »

Python Random setstate() Method with Examples

random setstate() Method in Python:

The setstate() method is used to return the random number generator’s state back to the specified state.

To capture the state, use the getstate() method.

Syntax:

random.setstate(state)

Parameters

state: This is Required. It is a state object. The setstate() method returns the random number generator’s state to this state.

Examples:

Example1:

Input:

Given list = [10, 40, 60, 80, 90]
Given length = 2

Output:

[10, 80]
[10, 80]

Example2:

Input:

Given list = [25, 35, 9, 45, 65]
Given length = 3

Output:

[45, 35, 65]
[45, 35, 65]

Random setstate() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Using the random() function print a random number.
  • Capture the state of the object using the random.getstate() method.
  • Using the random() function print the other random number.
  • Restore back the state of the object using the random.setstate() method.
  • Again print the same random value when the state was captured using the random() function.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random

# Using the random() function print a random number.
print(random.random())

# Capture the state of the object using the random.getstate() method
rslt_state = random.getstate()

# Using the random() function print the other random number.
print(random.random())

# Restore back the state of the object using the random.setstate() method
random.setstate(rslt_state)
# Again print the same random value when the state was captured using the random()
# function
print(random.random())

Output:

0.05457533761313371
0.8804207213354224
0.8804207213354224
Practical implementation

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Using the getstate() method, you can capture the current state.
  • Store it in another variable.
  • Print a list of random items of the specified length using the random.sample() method.
  • Using the setstate() method, you can restore the captured state by passing the above state as a parameter to the random.setstate() method.
  • Print the same random list of items using the random.sample() method.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = [10, 40, 60, 80, 90]
# Using the getstate() method, you can capture the current state.
# Store it in another variable.
rslt_state = random.getstate()

# Print a list of random items of the specified length using the random.sample()
# method.
print(random.sample(gvn_lst, 2))

# Using the setstate() method, you can restore the captured state by passing the
# above state as a parameter to the random.setstate() method.
random.setstate(rslt_state)

# Print the same random list of items using the random.sample() method.
print(random.sample(gvn_lst, 2))

Output:

[10, 80]
[10, 80]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Using the getstate() method, you can capture the current state.
  • Store it in another variable.
  • Print a list of random items of the specified length using the random.sample() method.
  • Using the setstate() method, you can restore the captured state by passing the above state as a parameter to the random.setstate() method.
  • Print the same random list of items using the random.sample() method.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Using the getstate() method, you can capture the current state.
# Store it in another variable.
rslt_state = random.getstate()

# Print a list of random items of the specified length using the random.sample()
# method.
print(random.sample(gvn_lst, 3))

# Using the setstate() method, you can restore the captured state by passing the
# above state as a parameter to the random.setstate() method.
random.setstate(rslt_state)

# Print the same random list of items using the random.sample() method.
print(random.sample(gvn_lst, 3))

Output:

Enter some random List Elements separated by spaces = 25 35 9 45 65
[45, 35, 65]
[45, 35, 65]

 

Python Random setstate() Method with Examples Read More »

Python Random getstate() Method with Examples

random getstate() Method in Python:

The getstate() method returns an object containing the random number generator’s current state.

Use this method to capture the state, and then use the setstate() method to restore the state using the captured state.

Syntax:

random.getstate()

Parameters: This method doesn’t accept any parameters.

Examples:

Example1:

Input:

Given list = [10, 40, 60, 80, 90]

Output:

The random value =  90
The random value =  90

Example2:

Input:

Given list = [4, 6, 7, 9]

Output:

The random value = 7
The random value = 7

Random getstate() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Take a variable and initialize it with the random.getstate() method.
  • Store it in a variable.
  • It returns an object containing the random number generator’s current state.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Take a variable and initialize it with the random.getstate() method.
# Store it in a variable.
# It returns an object containing the random number generator's current state.
a = random.getstate()
# Print the above result.
print(a)

Output:

(3, (2147483648, 2958650141, 1960736957, 2406743242, 2338959518, 3100388841, 1559261609, 2563037375, 2413032651, 2215975886, 1233119298, 2664317713, 2518715920, 1830558827, 732301835, 2271834001, 477455944, 921190001, 2858915895, 1580594191, 2936610823, 830667092, 4267652607, 3188563536, 441884984, 3644227226, 1048416985, 2890800050, 3130673655, 2625238051, 143315499, 779359520, 3444435108, 2864567679, 909868605, 1012010189, 3423481953, 4223076182, 141792975, 3552015843, 2254426651, 3627627260, 849125639, 1338840319, 4212732878, 3901314486, 1181210278, 684170455, 1584197225, 3020481743, 2853168588, 2783295314, 139795924, 4204032875, 934190350, 4108077174, 4178893516, 635531203, 2774432906, 1552622764, 2798767114, 1502888489, 3411305313, 2327347547, 4251459828, 2139438774, 4059783449, 1667094461, 3040735816, 893811547, 3798851047, 4185862502, 4290300119, 3228889436, 2788096306, 2395234615, 3209691111, 1070621771, 4015492403, 1848205985, 2400506323, 1385218858, 432017518, 1281006427, 820871546, 1945186639, 371217988, 1728844683, 1947809014, 1346204442, 442723165, 1999068426, 748674435, 2541361738, 2101616097, 769414680, 2831793506, 3524254620, 3049409168, 268951963, 2480400780, 4134107257, 1395477400, 3208207529, 3672445503, 3042969053, 1455397993, 917486726, 1922145706, 312960687, 2844244167, 4071632580, 2736256639, 1696698640, 2024593033, 1736046284, 4086243646, 4037248239, 1270948158, 2164832490, 2111024381, 1270584048, 2382608650, 3893966371, 2084875755, 3107150917, 1675442663, 2825997409, 317702383, 1018988413, 4263608476, 3168541027, 1853724402, 1145291118, 3751201164, 3532950860, 3981155907, 860374305, 2235681923, 2469242486, 1479030325, 3322428502, 3538847454, 2373501168, 1473785534, 1900401009, 1981092293, 3360828321, 482289429, 511973115, 4226523106, 2849499981, 1531287833, 1100249872, 4099483976, 3563434329, 558857975, 125626307, 3861619247, 1444702088, 4205975899, 3378371888, 4265560375, 2713139064, 740237244, 3458131473, 1901892214, 1342841988, 4099573694, 3315194634, 643611024, 4220356547, 3201457242, 1494690431, 2092718697, 1842544099, 2598552989, 4124953268, 3782395520, 647390867, 4217717487, 4220203037, 1007377947, 955588895, 2702341482, 4286159512, 2905988446, 2879841895, 3344876835, 89543556, 3220383365, 1868030775, 3983284672, 3956984449, 2317460400, 167380551, 1771720079, 632723239, 1358564073, 2700988368, 1456709041, 693492112, 2979610345, 3392835350, 1149002431, 203160637, 1934690762, 863425861, 2849572944, 2284497373, 1944080956, 2763450977, 1922145001, 2391900062, 333992930, 143804593, 3920248640, 3305379844, 2977480379, 2607396026, 2131874869, 2145138140, 1039856457, 863538755, 628747354, 3491817509, 154418846, 708726202, 2532063751, 239244789, 2858989690, 1909129016, 551562958, 689142146, 3459240983, 3657147974, 1201513610, 3559321006, 4125253396, 2541356945, 3418921110, 3848089689, 1313908835, 780119980, 2020042542, 2488123905, 2606764201, 2327619300, 2947282177, 540600019, 3730060989, 3813975070, 3876213007, 1084348454, 1332754607, 2012756408, 3396091293, 2727579719, 1399912370, 3369527132, 39070895, 2137512711, 2397705301, 3619684312, 304257835, 1469764078, 2071876341, 2208006222, 3321184261, 1173041128, 80410882, 2196528881, 37450925, 1324727098, 938565820, 2001584763, 3445531763, 491309020, 2676250001, 3483999310, 3998026213, 2461849369, 1400917992, 2191225491, 3563047168, 1212510207, 3488893261, 4118346218, 2207458670, 610723019, 2193695526, 1656574465, 2508898313, 466950484, 3482137648, 1394424081, 1822863820, 1445585358, 3648394106, 122465902, 1080557842, 699414654, 540127462, 1438041739, 1480910531, 4049362348, 1464557834, 3282349027, 853769337, 3353260359, 1914341744, 1019045686, 1270702168, 2147663044, 1106348083, 1688025031, 1690734555, 68198436, 1542301714, 2728642266, 4018559368, 2349097565, 16986703, 3336263888, 2748219259, 24837213, 3283346282, 2945318103, 1704028596, 897286349, 2850090079, 349682702, 3436121433, 3925184939, 1003643225, 754956135, 2631670969, 2397410243, 3450445780, 2312750999, 1299829746, 2763342980, 579312767, 3309267677, 1136498792, 1191114908, 3099205926, 3532865163, 345234544, 169322608, 978276397, 2696096112, 94617221, 2395199781, 991909812, 1024226657, 2219405889, 2271433807, 830613526, 3361259827, 2835737402, 1884999903, 1132224281, 413696493, 3188730185, 2834955258, 3624376277, 1198984184, 4060062947, 3949446077, 3739235065, 1789864174, 4280528658, 2586971248, 1750948931, 2116754654, 2954515174, 413201971, 3583432039, 798521670, 2169603564, 4270760492, 183831111, 4139248474, 3255497557, 1183474278, 1913795851, 1325547141, 3677776936, 3936474002, 1744949980, 2121139632, 4223244743, 4159957234, 2417668346, 2494743507, 2688374620, 3914019604, 4205889545, 2002219821, 1782600588, 117214336, 1177160969, 2965640493, 3912747464, 3942434582, 2336600933, 402520833, 82142402, 3280186928, 3357713478, 1621510120, 3539990616, 3669223424, 1183079917, 3777658477, 1030599448, 918419720, 1502509046, 3388662706, 2083364976, 3911933145, 3847853256, 2848212599, 1474549449, 3330751778, 2521906948, 2018614729, 3916144580, 3403222447, 2952103892, 3933618104, 903065308, 1459881318, 2896864685, 1034126822, 1037081, 2769042406, 628769191, 1540693719, 229813900, 2108474252, 3826882984, 3574339597, 239833054, 455943152, 2484028634, 1881220075, 2000794893, 695313419, 828592221, 547308351, 3932696785, 2935620743, 458142605, 1870966086, 946201181, 2001340621, 318989243, 3178582038, 2079358175, 4274416183, 118499468, 370301603, 4179880278, 572221261, 1760921511, 3298056424, 3507277346, 3084512280, 405919985, 1325536693, 918511433, 1834303658, 3870376942, 3501738211, 1625963426, 348323, 2242030202, 2852399194, 1903009840, 1935058216, 1578333206, 4272914968, 4072588451, 1016293237, 11455168, 3591331250, 1194216492, 1069899244, 2350206362, 3083765718, 4256991375, 2790811626, 718307776, 1410179209, 2632985187, 585324513, 2204861409, 1282232257, 1347492993, 2187847763, 3172468152, 3237258188, 1346964986, 2656582525, 183536557, 2701483246, 2162759624, 2432829348, 943743719, 2742103927, 2939613990, 2166572670, 3006186435, 1873072507, 34183711, 2468723527, 1423146305, 1777041617, 1121418387, 3894651432, 1131983113, 3245961893, 2377445864, 3927853875, 405695582, 3138955469, 1122563736, 1440982335, 1614720491, 1668620793, 3527067702, 3196877256, 1266205401, 2986550769, 4120358291, 1687916565, 1641057278, 2744272320, 1392196246, 2071479158, 4046990526, 3219630994, 1961086429, 1050005143, 149972892, 860756166, 1852412778, 3925332087, 3128578942, 188892714, 2534576442, 1123988891, 3476163348, 2544528605, 1202973395, 3527935356, 3885621558, 4157810548, 3288956832, 1586522375, 922189393, 1324140453, 4241189222, 770650455, 600493778, 2605721103, 2518507483, 1772347558, 2479514605, 2584110305, 4087275637, 3642681286, 2906292783, 751075597, 1222970586, 1791021249, 3780292328, 460147566, 1511703923, 3245139228, 3403378617, 673327266, 1330969073, 2815509917, 1526336993, 284258121, 1907931962, 1439704745, 2744932518, 256613349, 3888237198, 4169828707, 2503468627, 4079210735, 2689086452, 3119155530, 8454111, 771423642, 1158892081, 1075216921, 3940546501, 2419067660, 2469028139, 1853751035, 2052089636, 2815255744, 2630641543, 1185441581, 2781137579, 3023782122, 1941429619, 3727081123, 3957897539, 2718042759, 3286134537, 3962321570, 3128242110, 4272396116, 424628196, 2199184920, 491543448, 714790904, 624), None)
Practical implementation

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Get the state of the object using the random.getstate() method.
  • Store it in another variable.
  • Print a value at random from the given list using the random.choice() method.
  • Set the state of the object using the random.setstate() method.
  • Again print the same random value from the given list using the random.choice() method.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = [10, 40, 60, 80, 90]
# Get the state of the object using the random.getstate() method.
# Store it in a variable.
rslt_state = random.getstate()
# Print a value at random from the given list using the random.choice() method
print("The random value = ", random.choice(gvn_lst))
# Set the state of the object using the random.setstate() method.
random.setstate(rslt_state)
# Again print the same random value from the given list using the
# random.choice() method
print("The random value = ", random.choice(gvn_lst))

Output:

The random value =  90
The random value =  90

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Get the state of the object using the random.getstate() method.
  • Store it in another variable.
  • Print a value at random from the given list using the random.choice() method.
  • Set the state of the object using the random.setstate() method.
  • Again print the same random value from the given list using the random.choice() method.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Get the state of the object using the random.getstate() method.
# Store it in a variable.
rslt_state = random.getstate()
# Print a value at random from the given list using the random.choice() method
print("The random value = ", random.choice(gvn_lst))
# Set the state of the object using the random.setstate() method.
random.setstate(rslt_state)
# Again print the same random value from the given list using the
# random.choice() method
print("The random value = ", random.choice(gvn_lst))

Output:

Enter some random List Elements separated by spaces = 4 6 7 9
The random value = 7
The random value = 7

 

Python Random getstate() Method with Examples Read More »

Python Random seed() Method with Examples

random seed() Method in Python:

The random number generator is initialized using the seed() method.

To generate a random number, the random number generator requires a starting number (a seed value).

The random number generator defaults to using the current system time.

To change the random number generator’s starting number, use the seed() method.

Note: Please keep in mind that if you use the same seed value twice, you will get the same random number both times.

How Does the Seed Function Work?
The seed function saves the state of a random function so that it can generate the same random numbers on multiple executions of the code on the same or different machines (for a specific seed value). The previous value number generated by the generator serves as the seed value. When there is no previous value, it uses the current system time for the first time.

Syntax:

random.seed(x, version)

Parameters

x: This is Optional. A seed value is required to generate a random number.
If it is an integer, it is used directly; otherwise, it must be converted to an integer.
The default value is None, and if None is specified, the generator will use the current system time.

version: An integer that specifies how to convert a parameter to an integer.
The default value is 2.

Examples:

Example1:

Input:

Given seed value = 12

Output:

The random number =  0.4745706786885481

Example2:

Input:

Given seed value = 20

Output:

The random number =  0.9056396761745207

Random seed() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the number (seed value) as static input and store it in a variable.
  • Pass the given number as an argument to the random.seed() method to generate a random number, the random number generator requires a starting number (given seed value).
  • Print the random number using the random() function after applying the seed method.
  • The generator generates a random number based on the seed value, so if the seed value is 12, the first random number will always be 0.4745706786885481.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number (seed value) as static input and store it in a variable.
gvn_numb = 12
# Pass the given number as an argument to the random.seed() method to generate
# a random number, the random number generator requires a starting number
# (given seed value).
random.seed(gvn_numb)
# Print the random number using the random() function after applying the seed()
# method.
# The generator generates a random number based on the seed value, so if the seed
# value is 12, the first random number will always be 0.4745706786885481
print("The random number = ", random.random())

Output:

The random number =  0.4745706786885481

If you use the same seed value twice, you’ll get the same random number both times.

import random
random.seed(12)
print(random.random())
random.seed(12)
print(random.random())

Output:

0.4745706786885481
0.4745706786885481

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the number (seed value) as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the random.seed() method to generate a random number, the random number generator requires a starting number (given seed value).
  • Print the random number using the random() function after applying the seed method.
  • The generator generates a random number based on the seed value, so if the seed value is 12, the first random number will always be 0.4745706786885481.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number (seed value) as user input using the int(input()) function 
# and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the random.seed() method to generate
# a random number, the random number generator requires a starting number
# (given seed value).
random.seed(gvn_numb)
# Print the random number using the random() function after applying the seed()
# method.
# The generator generates a random number based on the seed value, so if the seed
# value is 20, the first random number will always be 0.9056396761745207
print("The random number = ", random.random())

Output:

Enter some random number = 20
The random number = 0.9056396761745207

 

Python Random seed() Method with Examples Read More »

Python statistics.variance() Method with Examples

statistics.variance() Method in Python:

The statistics.variance() method computes the variance from a data sample (from a population).

A high variance indicates that the data is dispersed, whereas a low variance indicates that the data is tightly clustered around the mean.

Look at the statistics.pvariance() method to calculate the variance of an entire population.

Syntax:

statistics.variance(data, xbar)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

xbar: This is Optional. The arithmetic mean of the given data. If omitted (or set to None), the mean is calculated automatically.

Note: It should be noted that if the data has fewer than two values, StatisticsError is returned.

Return Value:

Returns a float value representing the given data’s sample variance.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The variance of the given list items [10, 20, 40, 15, 30] =  145

Example2:

Input:

Given list = [3, 2, 5, 6, 1, 1, 3]

Output:

The variance of the given list items [3, 2, 5, 6, 1, 1, 3] = 3.6666666666666665

statistics.variance() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.variance() method that computes the variance of the given list items.
  • Store it in another variable.
  • Print the variance of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.variance() method that
# computes the variance of the given list items.
# Store it in another variable.
rslt = statistics.variance(gvn_lst)
# Print the variance of the given list items.
print("The variance of the given list items", gvn_lst, "= ", rslt)

Output:

The variance of the given list items [10, 20, 40, 15, 30] =  145

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.variance() method that computes the variance of the given list items.
  • Store it in another variable.
  • Print the variance of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.variance() method that
# computes the variance of the given list items.
# Store it in another variable.
rslt = statistics.variance(gvn_lst)
# Print the variance of the given list items.
print("The variance of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 3 2 5 6 1 1 3
The variance of the given list items [3, 2, 5, 6, 1, 1, 3] = 3.6666666666666665

Python statistics.variance() Method with Examples Read More »

Python statistics.pvariance() Method with Examples

statistics.pvariance() Method in Python:

The statistics.pvariance() method computes the variance of a whole population.

A high variance indicates that the data is dispersed, whereas a low variance indicates that the data is tightly clustered around the mean.

Examine the statistics.variance() method to determine the variance from a sample of data.

Syntax:

statistics.pvariance(data, xbar)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

xbar: This is Optional. The arithmetic mean of the given data (can also be a second moment around a point that is not the mean). If omitted (or set to None), the mean is calculated automatically.

Note: It is important to note that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a  float value representing the given data’s population variance.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The variance of the given list items [10, 20, 40, 15, 30] =  116

Example2:

Input:

Given list = [1, 6, 9, 7]

Output:

The variance of the given list items [1, 6, 9, 7] = 8.6875

statistics.pvariance() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.pvariance() method that computes the variance of a whole population.
  • Store it in another variable.
  • Print the variance of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.pvariance() method that
# computes the variance of a whole population.
# Store it in another variable.
rslt = statistics.pvariance(gvn_lst)
# Print the variance of the given list items.
print("The variance of the given list items", gvn_lst, "= ", rslt)

Output:

The variance of the given list items [10, 20, 40, 15, 30] =  116

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.pvariance() method that computes the variance of a whole population.
  • Store it in another variable.
  • Print the variance of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.pvariance() method that
# computes the variance of a whole population.
# Store it in another variable.
rslt = statistics.pvariance(gvn_lst)
# Print the variance of the given list items.
print("The variance of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 1 6 9 7
The variance of the given list items [1, 6, 9, 7] = 8.6875

Python statistics.pvariance() Method with Examples Read More »

Python statistics.stdev() Method with Examples

statistics.stdev() Method in Python:

Statistics.stdev() computes the standard deviation from a sample of data.

The standard deviation measures how evenly distributed the numbers are.

A high standard deviation indicates that the data is dispersed, whereas a low standard deviation indicates that the data is tightly clustered around the mean.

Tip: Unlike variance, the standard deviation is expressed in the same units as the data.

The standard deviation is equal to the square root of the sample variance.

Look at the statistics.pstdev() method to calculate the standard deviation of an entire population.

Syntax:

statistics.stdev(data, xbar)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

xbar: This is Optional. The arithmetic mean of the given data. If omitted (or set to None), the mean is calculated automatically.

Note: It should be noted that if the data has fewer than two values, StatisticsError is returned.

Return Value:

Returns a float value representing the given data’s standard deviation.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  12.041594578792296

Example2:

Input:

Given list = [-2, 5, 8, 9, 0, -1]

Output:

The standard deviation of the given list items [-2, 5, 8, 9, 0, -1] = 4.792355023020171

statistics.stdev() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.stdev() method that computes the standard deviation from a sample of data(here given list).
  • Store it in another variable.
  • Print the standard deviation of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.stdev() method that
# computes the standard deviation from a sample of data(here given list).
# Store it in another variable.
rslt = statistics.stdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  12.041594578792296

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.stdev() method that computes the standard deviation from a sample of data(here given list).
  • Store it in another variable.
  • Print the standard deviation of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.stdev() method that
# computes the standard deviation from a sample of data(here given list).
# Store it in another variable.
rslt = statistics.stdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = -2 5 8 9 0 -1
The standard deviation of the given list items [-2, 5, 8, 9, 0, -1] = 4.792355023020171

Python statistics.stdev() Method with Examples Read More »

Python statistics.pstdev() Method with Examples

statistics.pstdev() Method in Python:

The statistics.pstdev() method computes the standard deviation from a population as a whole.

The standard deviation measures how evenly distributed the numbers are.

A high standard deviation indicates that the data is dispersed, whereas a low standard deviation indicates that the data is tightly clustered around the mean.

Tip: Unlike variance, the standard deviation is expressed in the same units as the data.

Tip: The standard deviation is equal to the square root of the sample variance.

Look at the statistics.stdev() method to calculate the standard deviation from a sample of data.

Syntax:

statistics.pstdev(data, xbar)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

xbar: This is Optional. The arithmetic mean of the given data (can also be a second moment around a point that is not the mean). If omitted (or set to None), the mean is calculated automatically.

Note: It is important to note that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the given data’s population standard deviation.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  10.770329614269007

Example2:

Input:

Given list = [3, 4, 5, 1, 3, 2]

Output:

The standard deviation of the given list items [3, 4, 5, 1, 3, 2] = 1.2909944487358056

statistics.pstdev() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.pstdev() method that computes the standard deviation from a population as a whole.
  • Store it in another variable.
  • Print the standard deviation of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.pstdev() method that
# computes the standard deviation from a population as a whole.
# Store it in another variable.
rslt = statistics.pstdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  10.770329614269007

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.pstdev() method that computes the standard deviation from a population as a whole.
  • Store it in another variable.
  • Print the standard deviation of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.pstdev() method that
# computes the standard deviation from a population as a whole.
# Store it in another variable.
rslt = statistics.pstdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 3 4 5 1 3 2
The standard deviation of the given list items [3, 4, 5, 1, 3, 2] = 1.2909944487358056

Python statistics.pstdev() Method with Examples Read More »

Python statistics.mode() Method with Examples

statistics.mode() Method in Python:

Statistics.mode() computes the mode (central tendency) of a numeric or nominal data set.

The mode of a set of data values is the most frequently occurring value. It is the most likely value at which the data will be sampled. A mode of a continuous probability distribution is frequently defined as any value x at which its probability density function has a local maximum value, implying that any peak is a mode.

Syntax:

statistics.mode(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float or nominal value that represents the given data’s mode.

Examples:

Example1:

Input:

Given list = ['hello', 'this', 'is', 'btechgeeks', 'hello']

Output:

The mode of the given list items ['hello', 'this', 'is', 'btechgeeks', 'hello'] =  hello

Explanation:

Here 'hello' occurs most frequently. Hence the mode is 'hello'.

Example2:

Input:

Given list = [3, 4, 2, 5, 6, 3, 3, 1, 3]

Output:

The mode of the given list items [3, 4, 2, 5, 6, 3, 3, 1, 3] = 3

statistics.mode() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.mode() method that computes the mode (which occurs most frequently) of the given list items.
  • Store it in another variable.
  • Print the mode of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks', 'hello']
# Pass the given list as an argument to the statistics.mode() method that
# computes the mode (which occurs most frequently) of the given list items.
# Store it in another variable.
rslt = statistics.mode(gvn_lst)
# Print the mode of the given list items.
print("The mode of the given list items", gvn_lst, "= ", rslt)

Output:

The mode of the given list items ['hello', 'this', 'is', 'btechgeeks', 'hello'] =  hello

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.mode() method that computes the mode (which occurs most frequently) of the given list items.
  • Store it in another variable.
  • Print the mode of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.mode() method that
# computes the mode (which occurs most frequently) of the given list items.
# Store it in another variable.
rslt = statistics.mode(gvn_lst)
# Print the mode of the given list items.
print("The mode of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 3 4 2 5 6 3 3 1 3
The mode of the given list items [3, 4, 2, 5, 6, 3, 3, 1, 3] = 3

Python statistics.mode() Method with Examples Read More »

Python statistics.median_high() Method with Examples

statistics.median_high() Method in Python:

The statistics.median_high() method computes the data set’s high median. Before calculating the high median, the data is also sorted in ascending order using this method.

Note: It should be noted that if the number of data values is odd, it will return the exact middle value. If there are an even number of data values, it returns the larger of the two middle values.

Syntax:

statistics.median_high(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the data’s high median (middle value).

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30, 13, 17]

Output:

The high median of the given list items [10, 20, 40, 15, 30, 13, 17] =  17

Example2:

Input:

Given list = [2, 1, 3, 5, 7, 8]

Output:

The high median of the given list items [2, 1, 3, 5, 7, 8] = 5

statistics.median_high() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median_high() method that computes the high median of the given list items.
  • Store it in another variable.
  • Print the high median of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30, 13, 17]
# Pass the given list as an argument to the statistics.median_high() method that
# computes the high median of the given list items.
# Store it in another variable.
rslt = statistics.median_high(gvn_lst)
# Print the high median of the given list items.
print("The high median of the given list items", gvn_lst, "= ", rslt)

Output:

The high median of the given list items [10, 20, 40, 15, 30, 13, 17] =  17

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.median_high() method that computes the high median of the given list items.
  • Store it in another variable.
  • Print the high median of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median_high() method that
# computes the high median of the given list items.
# Store it in another variable.
rslt = statistics.median_high(gvn_lst)
# Print the high median of the given list items.
print("The high median of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 2 1 3 5 7 8
The high median of the given list items [2, 1, 3, 5, 7, 8] = 5

Python statistics.median_high() Method with Examples Read More »