Notifications
Clear all

how to code python

80 Posts
9 Users
10 Likes
21 K Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@frogandtoad

I don't think python is hard to learn

Totally agree. It is certainly less cryptic that other languages but then again most of the interpreted languages are close to English. But have the severe speed disadvantage compared to compiled languages,


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @pugwash

@robo-pi

Doctors without Boarders

Is that about an MD who runs a B&B with no guests? ? 

Must be getting late over there!

We should start a new club:

Programmers without Platforms.  ? 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 

As the original poster posed the question as to whether C++ or Python was easier to lean its not too surprising there may be different views expressed.   I think all the views expressed are a worthy contribution even if conflicting.   But we should certainly not get into religious wars on this and I do slap my wrist for banging on too much in this topic - humble apologies to all.

But I hope no-one will mind a small extra contribution.  It was triggered by the very good explanation provided about the question of  passing objects by value or reference in C++ and creating new objects etc. So I thought I might put up a small bit of code using classes in Python in case it may help a beginner deliberating whether to  start to lean C++ or Python by comparing the Python code to the C++ code above about creating a new class object.     Its certainly not a case of one language being better than the other and any beginner will find much good info with a bit of googling to ascertain which language they may wish to start learning.

# Program starts
class MyClass:
     myvariable = "blah"
     def myfunction(self):
          print("This is a message inside the class.")
# instances of the MyClass 'blueprint'
myobject1 = MyClass()
myobject2 = MyClass()

myobject2.myvariable = "yackity"
# Then print out data from the instances
print(myobject1.myvariable)
print(myobject2.myvariable)
print(myobject1.myfunction())
# Program Ends

The printout is

blah
yackity
This is a message inside the class.

That it folks ? 


   
codecage and frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

But have the severe speed disadvantage compared to compiled languages

Indeed, speed is one of the major factors why no other language has been able to remove the likes of C and C++ from their embedded perches 🙂


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

We should start a new club:

Programmers without Platforms.  ? 

A little trivia:  Did you know that interpreted languages (in most cases without a 3rd party), require an O/S to work, where as C and C++ are portable systems programming languages that do not require an O/S to operate at all?


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@byron

As the original poster posed the question as to whether C++ or Python was easier to learn it is not too surprising there may be different views expressed.   I think all the views expressed are a worthy contribution even if conflicting.   But we should certainly not get into religious wars on this and I do slap my wrist for banging on too much in this topic - humble apologies to all.

This appears to be a totally philosophical question as Python and C++ are NOT alternatives. If the fellow wants to do anything with an Arduino, he has no choice but to learn C++.

But if he only wants to use a microprocessor instead of a microcontroller then he has the choice of C++ or Python. As I have stated before Python is considerably less cryptic C++. Ergo, it should be easier to learn.

Since the dawn of public internet around 1993, the most asked question has been "Which programming language should I learn?". My answer has always been "it is horses for courses", i.e. choose the language that satisfies your long term goals.

For example:

PHP or Javascript for the internet.

C++ for microcontrollers

C or Java for computer applications

And I am sure there are a few more languages and categories that could be added to this list.

Religious Wars 

It is a very divisive issue, indeed!


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 
Posted by: @pugwash

C++ for microcontrollers

Can I add micro-python to the microcontroller list ? 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

May add to the Holy Wars please?

There are no war's here, just passionate debate 🙂

Posted by: @robo-pi

It takes more work to do it this way, plus I end up with two tabs for a class instead of just one.

Yes, slightly more at first if you don't know, but now that you know I'd say that the additional time taken is very small, and far outweighs surfing through a 1 page bible of code.

Posted by: @robo-pi

I also see that a lot of the stuff is redundant.   For example, it really doesn't matter whether you define your variables in the header first before using them in the actual code file. 

If you just plonk them in there, then they're just global variables... all depends on how you intend to use them.  For other modules to see them, you may also need to look into extern "C".  There is also a a good reason to separate files, so that the compiler doesn't compile each logical unit file each time, even when it hasn't been modified... especially important for saving build times in very large programs - Not so much for Arduino.

Posted by: @robo-pi

The code for my robot is staring to become complex enough that breaking it up into smaller classes just makes far more sense.    This way if I decide to change this code up a bit I can do it just in the class instead of having to dig around in a huge main program.

Indeed, this is where you'll gain some benefit, good luck! 🙂


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@byron

Posted by: @byron

You would be expected to include the code for your original class. No cheating now tish tish?

LOL, OK... I will provide an example shortly... watch this space 😉


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 
Posted by: @byron
Posted by: @pugwash

C++ for microcontrollers

Can I add micro-python to the microcontroller list ? 

I know absolutely nothing about micro-python. C++ gives me headaches enough!

Here is another statement that is going to harvest a load of criticism!

"I consider Python as a scripting tool rather than a programming language".

The reasons are the following. When vendors that I visited started producing EN10204 documentation for their products in readable PDF format, I could quickly rustle up a Python script using regex functions to check for how many occurrences of a heat/charge number the document contained and on what pages I would find the heat number I was looking for.

Python sped up my documentation review enormously, giving me more time at home than sitting in some damned conference room.

Occasionally I would use TKinter for a GUI but most of my stuff I did in the Terminal window.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 
Posted by: @pugwash

I know absolutely nothing about micro-python. C++ gives me headaches enough!

And your programatic aspirin is 

MicroPython is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller. MicroPython is a full Python compiler and runtime that runs on the micro-controller hardware.

https://micropython.org

?


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @frogandtoad

If you just plonk them in there, then they're just global variables... all depends on how you intend to use them.

I guess you're right there.  I named the variables I wanted to be private starting with an underscore as per convention.   But I guess they won't really be private until you list them in the header under "private:"

Typically when I'm writing functions in a class I'll often name variables with the same name I used in the calling program.

For example if I use "words" as generic string variable and call a function say,  PlayWithWords(words);

I'll often define the function as void PlayWithWords(String words){}

In other words I'll use the same variable name in both the calling program and in the function.   Even though the compiler treats them as separate variables within their own scope.  Having the same name doesn't really matter.   And it makes it easier for me in some ways.

But now I'm starting to use the convention, "PlayWithWords(words);  To call the function, but at the function end I'll  use void PlaywithWords(String _words){}   Using the underscore to show that while it's basically the same data it's actually a different variable.   And as you point out, making _words private insures that it is indeed limited to that scope.

I tend to be a lone coder.   I just write code for myself and seldom need to work with other programmers.  But I imagine that when working in a group everyone needs to get on the same page.  I'd most likely need a lot of training to be able to work with other programmers.  I'm just used to doing things my own way.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 
Posted by: @byron
Posted by: @pugwash

I know absolutely nothing about micro-python. C++ gives me headaches enough!

And your programatic aspirin is 

MicroPython is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller. MicroPython is a full Python compiler and runtime that runs on the micro-controller hardware.

https://micropython.org

?

I think I'll give it a body swerve as my interests lie elsewhere! But like @robo-pi I like to potter around in glorious isolationist bliss. I too am a loner! ? 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

"I consider Python as a scripting tool rather than a programming language".

I consider these 3 amigos an operating system ? 

  • grep
  • sed
  • awk

   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

A man walks into a pet shop. "I am looking for something different!" he tells the pet shop owner. The owner says "Come into the back room, we have just got a fresh delivery".

In the back room, there are three cages marked $1000, $2000 and $10000.

The man asks "what is in the $1000 cage", the owner replies "These are coding Chimps" and "What can they do?" "HTML, PHP and some Javascript".

"OK and what is in the $2000 cage?" "These fellows can do the same as those in the $1000 cage plus C and Java"

"And what is in the $10000 cage" "Oh! This is the Project Manager Chimp, nobody actually knows what he does, but all of the rest of them call him 'Sir!'" 


   
ZeFerby and frogandtoad reacted
ReplyQuote
Page 4 / 6