Categories
Python Answers

How to calculate an integer square root in Python?

Sometimes, we want to calculate an integer square root in Python.

In this article, we’ll look at how to calculate an integer square root in Python.

How to calculate an integer square root in Python?

To calculate an integer square root in Python, we can use the math.isqrt function.

For instance, we write

n = 1000
root = math.isqrt(n)

to call math.isqrt with n to return the integer square root of n.

It’s either the exact square root or the biggest integer such that root squared is less than or equal to n.

isqrt is available since Python 3.8.

Conclusion

To calculate an integer square root in Python, we can use the math.isqrt function.

Categories
Python Answers

How to access a function variable outside the function without using “global” with Python?

Sometimes, we want to access a function variable outside the function without using "global" with Python.

In this article, we’ll look at how to access a function variable outside the function without using "global" with Python.

How to access a function variable outside the function without using "global" with Python?

To access a function variable outside the function without using "global" with Python, we can add an attribute to the function.

For instance, we write

def hi():
    # ...
    hi.bye = 100
    sigh = 10

hi()
print(hi.bye)

to add the bye attribute to the hi function.

We can do this since functions are objects in Python.

And then we get the value with hi.bye.

Conclusion

To access a function variable outside the function without using "global" with Python, we can add an attribute to the function.

Categories
Python Answers

How to plot dates on the x-axis with Python’s matplotlib?

Sometimes, we want to plot dates on the x-axis with Python’s matplotlib.

In this article, we’ll look at how to plot dates on the x-axis with Python’s matplotlib.

How to plot dates on the x-axis with Python’s matplotlib?

To plot dates on the x-axis with Python’s matplotlib, we convert the date strings to datetime objects with strptime.

For instance, we write

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

dates = ['01/02/2022','01/03/2022','01/04/2022']
x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]
y = range(len(x))

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(x,y)
plt.gcf().autofmt_xdate()

to convert the date strings in the dates list to datetime objects with strptime and put them in the x list.

Then we call set_major_formatter to DateFormatter to format the dates in the x-axis.

And we call set_major_locator with DayLocator to put the dates on the x-axis in the right location.

Then we call plot to plot iterables x and y.

And we call autofmt_xdate to format the dates with DateFormatter.

Conclusion

To plot dates on the x-axis with Python’s matplotlib, we convert the date strings to datetime objects with strptime.

Categories
Python Answers

How to fix items in JSON object are out of order using Python json.dumps?

Sometimes, we want to fix items in JSON object are out of order using Python json.dumps.

In this article, we’ll look at how to fix items in JSON object are out of order using Python json.dumps.

How to fix items in JSON object are out of order using Python json.dumps?

To fix items in JSON object are out of order using Python json.dumps, we can call json.dumps with a ordered dict.

For instance, we write

from collections import OrderedDict

s = json.dumps(OrderedDict([("a", 1), ("b", 2)]))

to create an ordered dict with the OrderDict class.

And then we call json.dumps with the ordered dict to create a JSON string that has the same order as the items are listed in the list we created the OrderedDict with.

Conclusion

To fix items in JSON object are out of order using Python json.dumps, we can call json.dumps with a ordered dict.

Categories
Python Answers

How to play a sound with Python?

Sometimes, we want to play a sound with Python.

In this article, we’ll look at how to play a sound with Python.

How to play a sound with Python?

To play a sound with Python, we can use the pyglet module.

To install it, we run

pip install pyglet

Then, we write

import pyglet

music = pyglet.resource.media('music.mp3')
music.play()

pyglet.app.run()

to call pyglet.resource.media to open the music.mp3 file.

Then we call music.play to play the file.

Conclusion

To play a sound with Python, we can use the pyglet module.