Python Forum

Full Version: nframes = params[:4]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am using the following code in wave file processing:

f = wave.open("file.wav", "rb")
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]

but i have not understood the working of "nframes = params[:4]"
what does "params[:4]" means?
[:4] is used for what?

Please guide
If I was to take a guess I would say that it means assign the 4th entry to nframes from the supplied f.getparams. So if you had 1 2 3 4 as the input it would assign 4 to nframes.

Regards
Hello,

I have got it
Thank you
That is slicing notation. It will give you everything from index 0, up to, but not including index 4. in other words - first 4 elements of params.
(Jan-18-2019, 10:22 AM)vipinv23 Wrote: [ -> ]nchannels, sampwidth, framerate, nframes = params[:4]

As buran already answered, it's slicing combined with unpacking.

What it does can be explained with following code:

>>> a = [1, 2, 3, 4, 5]
>>> a[:4]
[1, 2, 3, 4]
>>> one, two, three, four = a[:4]
>>> one
1
>>> two
2
>>> three
3
>>> four
4
Hello All,

Thank you for your replies.

Now I am able to related to [:4] means