R-A
Posts: 33
Joined: Sun Sep 11, 2016 4:59 pm
Contact: Website

please explain this code?

Sun Jun 24, 2018 1:15 pm

Hi,

I am working through the Pyton code on the Raspberry Documentation site and got stuck with the following piece of code:

Code: Select all

     {i: i ** 3 for i in range(12)}
Can someone please explain to me in layman's terms, why would the output come to

Code: Select all

{0: 0,
 1: 1,
 2: 8,
 3: 27,
 4: 64,
 5: 125,
 6: 216,
 7: 343,
 8: 512,
 9: 729,
 10: 1000,
 11: 1331}

I'm using a Raspberry Pi 3 Model B with 7" LCD Touch screen and Mini USB Wireless Keyboard

Heater
Posts: 9361
Joined: Tue Jul 17, 2012 3:02 pm

Re: please explain this code?

Sun Jun 24, 2018 1:30 pm

This is saying that the number "i", whatever it is, will become "i" to the power 3, ("**" is the power operator). That is to say i becomes i cubed. Or i becomes i * i * i.

Example, if i = 2 it will become 2 * 2 * 2 which is 8.

Example, if i = 10 it will become 10 * 10 * 10 which is 1000.

Now, the "for" part says do this for all values of i in the range 12. Which actually means the 12 numbers from 0 to 11.

Hence the output lists all the number 0 to 11 on the left and their cubes on the right.

Yes it's insane and incomprehensible. Which is why I don't use Python.

pcmanbob
Posts: 3987
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: please explain this code?

Sun Jun 24, 2018 1:48 pm

Heater wrote:
Sun Jun 24, 2018 1:30 pm

Yes it's insane and incomprehensible.
Well it can't be that insane and incomprehensible , you were able to work out what it did and provide a good explanation. :lol: :lol:
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Heater
Posts: 9361
Joined: Tue Jul 17, 2012 3:02 pm

Re: please explain this code?

Sun Jun 24, 2018 2:07 pm

pcmanbob,
Well it can't be that insane and incomprehensible, you were able to work out what it did and provide a good explanation
Thanks.

I guess I have spent enough decades fighting with all kind of programming languages that I know what kind of gibberish to expect from programming language designers :)

I have no idea about Python and have only ever tried to use it once. Gave up after finding the white space block delimiting insanity and the snail like performance.

Given the source line in question and not the output I might never have guessed what it outputs.

Whilst we are at it:

The curly braces, "{ ... : ...}" are a Python "dictionary". For more about dictionaries see here: https://www.python-course.eu/dictionaries.php

User avatar
DougieLawson
Posts: 33068
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: please explain this code?

Sun Jun 24, 2018 2:15 pm

Apart from language designers implementing that sort of junk, the folks who use it without clear "I'm insane so I wrote this." comments are the ones who need to meet the wrath of my educational baseball bat.

If you use weird arcane crap like that you should at least explain why it solves your programming problem.

If you don't you are destined to become a perlmonger.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

hippy
Posts: 3190
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: please explain this code?

Sun Jun 24, 2018 3:04 pm

pcmanbob wrote:
Sun Jun 24, 2018 1:48 pm
Heater wrote:
Sun Jun 24, 2018 1:30 pm

Yes it's insane and incomprehensible.
Well it can't be that insane and incomprehensible , you were able to work out what it did and provide a good explanation. :lol: :lol:
Exactly. Python does have some strange ways of how one can choose to do things but what language doesn't ?

One doesn't have to do it that way. I wouldn't because I switch between so many languages I like my code to be obvious when I cannot remember everything about a language but it is an efficient way of doing it. It is entirely meaningful to anyone who has a reasonable working understand of Python. This is the long, slow equivalent ...

Code: Select all

dictionary = { }
for i in range(0,12):
  dictionary[i] = i ** 3

R-A
Posts: 33
Joined: Sun Sep 11, 2016 4:59 pm
Contact: Website

Re: please explain this code?

Sun Jun 24, 2018 3:54 pm

Heater wrote:
Sun Jun 24, 2018 1:30 pm
This is saying that the number "i", whatever it is, will become "i" to the power 3, ("**" is the power operator). That is to say i becomes i cubed. Or i becomes i * i * i.

Example, if i = 2 it will become 2 * 2 * 2 which is 8.

Example, if i = 10 it will become 10 * 10 * 10 which is 1000.

Now, the "for" part says do this for all values of i in the range 12. Which actually means the 12 numbers from 0 to 11.

Hence the output lists all the number 0 to 11 on the left and their cubes on the right.

Yes it's insane and incomprehensible. Which is why I don't use Python.
Thanx, this is what I wanted to find out / confirm. Although I can do the math and see what it does, I just wanted to know why.

So, different question: If you want to use a Pi a IoT device to read sensor info and store it in a DB (I am a Linux sysadmin, but not a developer) which coding, or programming language would be best to use, especially with various different sensors and modules?

i.e. I want to read and store temperature sensor data (currently use DS18B20' & Python code, to store data on emoncms.org), read battery / solar panel / AC voltages & ampere and store it in emoncms.org or InfluxDB as well. Operate a garage door, build an alarm system with PIR and motion sensors, etc.

I know a bit of HTML, PHP, MySQL, BASH coding, but not a full fledged programmer.

My brother is a programmer and his research lead to "python is too slow for production use". Arduino, which I also use a lot work with C / C# code, so I guess I need to go that route?
I'm using a Raspberry Pi 3 Model B with 7" LCD Touch screen and Mini USB Wireless Keyboard

User avatar
DougieLawson
Posts: 33068
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: please explain this code?

Sun Jun 24, 2018 4:03 pm

Unless your program is time critical then python is a good choice. It's easy to learn, there's lots of library functions, it's interpreted (edit & run, rather than edit, compile, link & run). It is slow but that won't matter.

The only downside of python is the totally insane left-hand whitespace as a syntactic anomaly. That's the feature of python that cause more bugs than anything else. There's also the python2 vs python3 conundrum - but as a new developer writing new code you shoult use python3 exclusively.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Heater
Posts: 9361
Joined: Tue Jul 17, 2012 3:02 pm

Re: please explain this code?

Sun Jun 24, 2018 4:30 pm

R-A,

So let me try and gather your requirements:
...use a Pi a IoT device...
That implies a web server or web client or perhaps some other network connectivity.
..read sensor info..
That implies to me interfacing with devices via serial ports, I2C, SPI, general GPIO bit banging etc.
...and store it in a DB..
Again this is generally a network connection to a DB. Even if it is on the same machine.
I know a bit of HTML...
That hints that you want a web page interface and hence a web server someplace.

The obvious answer here is Javascript running under node.js.

JS and node.js makes it very easy to create a web server in a few lines of code. That same program can easily be made to juggle with database connections and sensor monitoring and whatever you have.

Here is node.js:
https://nodejs.org/en/

You will need the Express module to make the web server part easier:
https://expressjs.com/

You can push real-time sensor data to your web pages with websockets. This is made very easy with the socket.io module:
https://socket.io/

For your database you may well be using mysql:
https://www.npmjs.com/package/mysql

There are other database client modules available.

For GPIO, PWM etc there is pigpio:
https://github.com/fivdi/pigpio

For interfacing with serial ports there is node serialport:
https://www.npmjs.com/package/serialport

As for performance, JS is a couple of times slower than C but 10's of times faster than Python.

JS has the same rapid "edit and run" development cycle as Python.

JS has the advantage of being a long time, internationally standardized language not just a random hack like Python.

R-A
Posts: 33
Joined: Sun Sep 11, 2016 4:59 pm
Contact: Website

Re: please explain this code?

Mon Jun 25, 2018 7:50 am

DougieLawson wrote:
Sun Jun 24, 2018 4:03 pm
Unless your program is time critical then python is a good choice. It's easy to learn, there's lots of library functions, it's interpreted (edit & run, rather than edit, compile, link & run). It is slow but that won't matter.

The only downside of python is the totally insane left-hand whitespace as a syntactic anomaly. That's the feature of python that cause more bugs than anything else. There's also the python2 vs python3 conundrum - but as a new developer writing new code you shoult use python3 exclusively.
In what sense is "time critical" an issue? If I want to sample temperature, weather and solar data, say every minute, from at least 30 sensors +(doing some research) is that considered time sensitive?

I have played with a bit of C or C# code on Arduinos, so yes, the compiling time is a bit of a nuisance, but I could get used to it if it means the program will run better in the long run.
I'm using a Raspberry Pi 3 Model B with 7" LCD Touch screen and Mini USB Wireless Keyboard

R-A
Posts: 33
Joined: Sun Sep 11, 2016 4:59 pm
Contact: Website

Re: please explain this code?

Mon Jun 25, 2018 8:22 am

Heater wrote:
Sun Jun 24, 2018 4:30 pm
R-A,

So let me try and gather your requirements:
...use a Pi a IoT device...
That implies a web server or web client or perhaps some other network connectivity.
Yes, I would primarily upload data to another web server hosted on the internet, though a small server on the Pi to manage stuff (i.e. enable / disable sensors, change values of certain items, etc) would be beneficial as well.

Heater wrote:
Sun Jun 24, 2018 4:30 pm
..read sensor info..
That implies to me interfacing with devices via serial ports, I2C, SPI, general GPIO bit banging etc.
Yes, I use I2C and GPIO right now, and my inverter connects via USB. Still trying to get a SPI LCD working
Heater wrote:
Sun Jun 24, 2018 4:30 pm
...and store it in a DB..
Again this is generally a network connection to a DB. Even if it is on the same machine.
Yes, I know. I'm a Linux sysadmin.
Heater wrote:
Sun Jun 24, 2018 4:30 pm
I know a bit of HTML...
That hints that you want a web page interface and hence a web server someplace.


The obvious answer here is Javascript running under node.js.

JS and node.js makes it very easy to create a web server in a few lines of code. That same program can easily be made to juggle with database connections and sensor monitoring and whatever you have.

Here is node.js:
https://nodejs.org/en/

You will need the Express module to make the web server part easier:
https://expressjs.com/

You can push real-time sensor data to your web pages with websockets. This is made very easy with the socket.io module:
https://socket.io/

For your database you may well be using mysql:
https://www.npmjs.com/package/mysql

There are other database client modules available.

For GPIO, PWM etc there is pigpio:
https://github.com/fivdi/pigpio

For interfacing with serial ports there is node serialport:
https://www.npmjs.com/package/serialport

As for performance, JS is a couple of times slower than C but 10's of times faster than Python.

JS has the same rapid "edit and run" development cycle as Python.

JS has the advantage of being a long time, internationally standardized language not just a random hack like Python.
And herein lies the issue. I am not a developer and don't really have time to learn every language possible.
I'm using a Raspberry Pi 3 Model B with 7" LCD Touch screen and Mini USB Wireless Keyboard

jahboater
Posts: 2728
Joined: Wed Feb 04, 2015 6:38 pm

Re: please explain this code?

Mon Jun 25, 2018 8:38 am

R-A wrote:
Mon Jun 25, 2018 7:50 am
I have played with a bit of C or C# code on Arduinos, so yes, the compiling time is a bit of a nuisance, but I could get used to it if it means the program will run better in the long run.
I didn't see which Pi you have, but if you have the new Pi3+, compile times are reasonable:-

With the common medium-high optimization (-O2), a tiny program takes 0.19 sec to build, but a much larger program (around 15,000 lines in one source file - unusual) takes 11.6 seconds to compile. For a quick compile with no optimization this large module takes 2.9 seconds to build. Not as fast as an x86 desktop, but usable IMHO. For small test programs compile time is negligible.

And if your program has multiple source files, then "make" will use all four CPU cores at once to build things very rapidly.

There is usually no need to "link" the program, the compiler does that for you.

R-A
Posts: 33
Joined: Sun Sep 11, 2016 4:59 pm
Contact: Website

Re: please explain this code?

Mon Jun 25, 2018 9:00 am

jahboater wrote:
Mon Jun 25, 2018 8:38 am
R-A wrote:
Mon Jun 25, 2018 7:50 am
I have played with a bit of C or C# code on Arduinos, so yes, the compiling time is a bit of a nuisance, but I could get used to it if it means the program will run better in the long run.
I didn't see which Pi you have, but if you have the new Pi3+, compile times are reasonable:-

With the common medium-high optimization (-O2), a tiny program takes 0.19 sec to build, but a much larger program (around 15,000 lines in one source file - unusual) takes 11.6 seconds to compile. For a quick compile with no optimization this large module takes 2.9 seconds to build. Not as fast as an x86 desktop, but usable IMHO. For small test programs compile time is negligible.

And if your program has multiple source files, then "make" will use all four CPU cores at once to build things very rapidly.

There is usually no need to "link" the program, the compiler does that for you.
I have a Pi 2B and a Pi3 B. Waiting to get the Pi 3B+ once it's available in our country.
I'm using a Raspberry Pi 3 Model B with 7" LCD Touch screen and Mini USB Wireless Keyboard

Heater
Posts: 9361
Joined: Tue Jul 17, 2012 3:02 pm

Re: please explain this code?

Mon Jun 25, 2018 10:17 am

R-A,
In what sense is "time critical" an issue? If I want to sample temperature, weather and solar data, say every minute, from at least 30 sensors +(doing some research) is that considered time sensitive?
Depends on your application.

If you want to make a first person shooter game you have 16ms per frame to figure out what to display next.

If you want to predict tomorrow's weather from today's sensor readings it's probably good if you can do it done before tomorrow!

In your case 30 sensor readings every minute is only 1 every 2 seconds. I suspect even Python can do that.
I have played with a bit of C or C# code on Arduinos, so yes, the compiling time is a bit of a nuisance, but I could get used to it if it means the program will run better in the long run.
I would not worry about compile time unless you expect your program to become huge. Even the, as pointed out above, when using make to build your program from 100's of modules it will only recompile the ones you have changed since the last build so things go pretty quickly.
And herein lies the issue. I am not a developer and don't really have time to learn every language possible.
I do sympathise. Even developers don't have time to learn every language possible, they are busy developing.

However, given that you are comfortable with C and/or C#, Javascript and node.js will be a lot less effort to get into than Python which is a very different language.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 10 guests