Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:14 13 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Python....

     Page 1 of 2    
Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9593
Posted: 12:51am 13 Mar 2015
Copy link to clipboard 
Print this post

I am about to start having a play around with Python on the Raspberry Pi 2, and wondered if we have any members here who are adept at programming in Python, who will be able to help me if I post questions on this language?

I know there are plenty of web resources, but TBS is much closer to home.

Was not MMEDIT written in Python?(compiled for Windoze)
Smoke makes things work. When the smoke gets out, it stops!
 
mindrobots
Newbie

Joined: 21/05/2014
Location: United States
Posts: 32
Posted: 09:29am 13 Mar 2015
Copy link to clipboard 
Print this post

I've dabbled in Python and written a few small things. I wander away and need to refresh my brain as to how it differs from the last thing I used. I do like it and do keep coming back to it.

I'm sure your knowledge will shortly surpass mine but I'd be happy to field questions - teaching is the best way to learn something!!

Rick

EDIT: The 12 hour time delay may give me time to research your questions and look smarter than I really am!! Edited by mindrobots 2015-03-14
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 11:32am 13 Mar 2015
Copy link to clipboard 
Print this post

  Grogster said  
Was not MMEDIT written in Python?(compiled for Windoze)


No.

MMEdit is written in Liberty Basic and the next version will be written in PureBasic.

The first thing you will notice with Python is the indenting - it has to be right.

I know sod all about Python so wont be much help.
There are good resources on the PI forum and MagPi has well written articles.
http://www.raspberrypi.org/magpi/

Jim
VK7JH
MMedit
 
cwilt
Senior Member

Joined: 20/03/2012
Location: United States
Posts: 147
Posted: 02:16pm 13 Mar 2015
Copy link to clipboard 
Print this post

I don't claim to be an expert but will help when I can. My python knowledge is not on rpi, but pcduino.
 
hitsware
Guru

Joined: 23/11/2012
Location: United States
Posts: 535
Posted: 02:36pm 13 Mar 2015
Copy link to clipboard 
Print this post

  cwilt said   I don't claim to be an expert but will help when I can. My python knowledge is not on rpi, but pcduino.


How about Python version of


PRINT "hello world"
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9593
Posted: 03:13pm 13 Mar 2015
Copy link to clipboard 
Print this post

Thanks for the replies, fellas, and to Jim for clarifying that MMEDIT was NOT written in Python - not sure where that came from.....

Anyhoo....

Once my Pi2 arrives, I will have a play. There are lots of videos on YouTube etc, of Python programming examples, so I will try all this stuff first, but was interested to know if any members here program in Python, so I could put up questions if I had any.
Smoke makes things work. When the smoke gets out, it stops!
 
mbramwel

Regular Member

Joined: 10/07/2013
Location: Canada
Posts: 42
Posted: 10:33am 16 Mar 2015
Copy link to clipboard 
Print this post

I have coded in many languages including php under Linux. I was using php for command line apps/utilities. php is not just for web pages (which is what most people claim when you say php).

I wanted to learn python so I could port my php programs to python. I thought it can't be that difficult, they are using the rPi to teach python to kids.

I signed up on the free site: http://www.codecademy.com/

It took about 2 weeks of evenings to go through the lessons. I thought the site did a good job. It works by showing you a small example of how something works and asks you to create a small 1 to 5 line python script to use that new function. It then runs it and determines if you got it right.

If you post command line stuff, I can help. I am not interested in GUI or Graphics in python.

My only real complaint about python is the spacing/indentation. Many languages use a symbol to signal start/end of a block. Python uses indentation and complains if you get it wrong.

There is LOTS of code libraries for python to do just about anything. I recommend learning python if you have not already done so.

I have ported some of my apps from php to python. Generally speaking, my python versions run much faster than php. Many of my programs are reading large arrays and performing calculations & lookups. Python has fast methods of searching through arrays.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9593
Posted: 12:43pm 16 Mar 2015
Copy link to clipboard 
Print this post

Thanks for your post, and the link to codecademy. I will sign up there.
Smoke makes things work. When the smoke gets out, it stops!
 
cwilt
Senior Member

Joined: 20/03/2012
Location: United States
Posts: 147
Posted: 02:10pm 16 Mar 2015
Copy link to clipboard 
Print this post

  hitsware said  
  cwilt said   I don't claim to be an expert but will help when I can. My python knowledge is not on rpi, but pcduino.


How about Python version of


PRINT "hello world"


Your a natural.

Put that line in a file named hello.py and then execute python hello.py
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2437
Posted: 02:14pm 16 Mar 2015
Copy link to clipboard 
Print this post

those who are interested might like to have a look here:

http://www.brython.info/tests/console.html?lang=en

this is an online python 3 implementation (BRowser pYTHON) that is pretty neat for doing quick-and-dirty testing of small bits of code. it is quite cut down, and runs within your browser using some clever java script.

you should bear in mind that there are two branches of python: the 'classic' 2.7 and the current 3.x branch. the language did a bit of a split when version 3 came out, as 3.x versions are not completely backwards compatible in their syntax. the 2.7 branch is likely to stay around indefinitely with occasional maintenance releases, and some folks do seem to prefer it. i like 2.7, but would (reluctantly) have to recommend 3.x for newcomers.

regarding the lack of block markers - in my opinion that is the one major failing of the language. i live in hope that when a version 4 appears the developers may see sense and introduce begin ... end markers, doing away with all this silly use of white-space as part of the code.


cheers,
rob :-)
 
hitsware
Guru

Joined: 23/11/2012
Location: United States
Posts: 535
Posted: 02:50pm 16 Mar 2015
Copy link to clipboard 
Print this post

  cwilt said  
  hitsware said  
  cwilt said   I don't claim to be an expert but will help when I can. My python knowledge is not on rpi, but pcduino.


How about Python version of


PRINT "hello world"


Your a natural.

Put that line in a file named hello.py and then execute python hello.py


Geez ..... Then Python's just the same as Basic
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9593
Posted: 03:37pm 16 Mar 2015
Copy link to clipboard 
Print this post

  robert.rozee said   the 2.7 branch is likely to stay around indefinitely with occasional maintenance releases, and some folks do seem to prefer it. i like 2.7, but would (reluctantly) have to recommend 3.x for newcomers.


Why?

Is 3.x easier to learn then 2.7?
Smoke makes things work. When the smoke gets out, it stops!
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2437
Posted: 04:01pm 16 Mar 2015
Copy link to clipboard 
Print this post

  Grogster said  
  robert.rozee said   the 2.7 branch is likely to stay around indefinitely with occasional maintenance releases, and some folks do seem to prefer it. i like 2.7, but would (reluctantly) have to recommend 3.x for newcomers.

Why? Is 3.x easier to learn then 2.7?

i'd not say any easier to learn, but as 2.7 ages support on new platforms is likely to trail off. 10 years from now, if you sit down at a python console, it is far more likely to be version 3.x or later.

as an example, on 2.7 the following works:
>>> print "hello world"

while on 3.1 the syntax required is:
>>> print ("hello world")


if you leave out the brackets, it will not work on 3.x and throw up an error message. this caught me out first time i tried using brython. the 3.x branch seems to aim to force stricter formatting (with brackets) of all function calls, bringing it more in line with C syntax.

i'm not a python expert, others may be able to better comment on the differences.

cheers,
rob :-)
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 04:08pm 16 Mar 2015
Copy link to clipboard 
Print this post

I hate the indent syntax !!
The only Konstant is Change
 
cwilt
Senior Member

Joined: 20/03/2012
Location: United States
Posts: 147
Posted: 01:17am 17 Mar 2015
Copy link to clipboard 
Print this post

Rob is correct. If you have the choice you should learn 3.+. There is a passing of the baton now and support is split between the two. I still run into cases where a module has not been converted to 3.

The whitespace keeps you on your toes. Seriously, even the most basic text editor that handles python should auto indent for you, but you still must pay attention.

I am resisting the move away from 2.7, but it is inevitable. Resistance is futile.
 
mbramwel

Regular Member

Joined: 10/07/2013
Location: Canada
Posts: 42
Posted: 08:08am 17 Mar 2015
Copy link to clipboard 
Print this post

Personally, I learned 2.x a few months ago. I like it a lot.

There are *small* differences between 2.x and 3.x

Very small differences. For me, the fact that 2.x seemed to have the best libraries and 3rd party support with 3.x being planned (eventually) made me pick 2.x.

I agree that 2.x probably will cease to exist in 10 years. But 10 years is a LONG time in computer-land.

oddly enough, it is the print command that will probably be the item that hits you with 2.x to 3.x

I have some php to python notes, I'll post in a new message



 
mbramwel

Regular Member

Joined: 10/07/2013
Location: Canada
Posts: 42
Posted: 08:43am 17 Mar 2015
Copy link to clipboard 
Print this post

When I started to look at python, I quickly felt it was just like all the other languages. The syntax was so close to php, maybe I could do a simple search/replace and quickly convert the php code to python.

example:

php mystring = "hello " . "world";
python mystring = "hello " + "world"

as a test, I did a search replace of " . " to " + "
I also dropped all trailing ;

A few other hints were compiled and I posted them here:
www.foxhollow.ca/php2python


To speed up my php to python app conversion, I cheated. Python allows us to make 1 line functions that look like the php command.

Example: My php used the trim function
python has something that does the same thing so I made a simple function with the php name but uses the python command...


def trim(mystring):
return mystring.strip()



Everywhere in my php script that used the trim command, now works aok in the python script. By seeding a few functions at the top of my php script, plus some search/replace, I converted many of my php programs to python in minutes.

another example of a python function to emulate php function:

# ************************************************************
# PHP: strpos()
# ************************************************************
def strpos(haystack, needle):
return (needle in haystack)



Granted I am talking about php -> python here, not basic -> python,
but I hope I am giving you some ideas on how to do things.

We could also come up with a bunch of basic-to-python functions so conversion of apps would be super easy.

note: line numbers and "goto" will be tough
"gosub xxx" is easy because all python functions are basically the same thing as a gosub, the difference is "gosub name" is now just "name"

Edited by mbramwel 2015-03-18
 
mbramwel

Regular Member

Joined: 10/07/2013
Location: Canada
Posts: 42
Posted: 10:55am 17 Mar 2015
Copy link to clipboard 
Print this post

A comment about python2 versus python3, especially the print command.

I decided in the first few days to not use print unless I really needed it.

2 reasons:
[1] it is different in python2 & python3
[2] it always prints a carriage return & line feed.

In basic, we could do this:


print "hello ";
print "world"


The above would display "hello world" (without the quotes of course)

In python, we cannot end with the ";"

Instead of print, I use the sys function.


root@web1:/opt/markware/python# cat helloworld.py
import sys

sys.stdout.write("Hello ")
sys.stdout.write("World\n");

root@web1:/opt/markware/python# python2 helloworld.py
Hello World
root@web1:/opt/markware/python# python3 helloworld.py
Hello World
root@web1:/opt/markware/python#


Note that the above code works aok in both python2 and python3
 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 08:20am 19 Mar 2015
Copy link to clipboard 
Print this post

  mbramwel said  In python, we cannot end with the ";"


In Python 2, ending a print statement with a comma "," will insert a space. Handy for debugging!

So
print "hello",
print "world"
print 42

will output
hello world
42


I have not yet used Python 3 in anger. The main reason for this is risking not being able to use a library. That said, I dabble.
 
mindrobots
Newbie

Joined: 21/05/2014
Location: United States
Posts: 32
Posted: 12:08pm 19 Mar 2015
Copy link to clipboard 
Print this post

In Python3:


print("Hello",end=" ") # append space instead of newline
print("world!")
print(42)


will give you the same results as the Python2 example. with end=, you can override the way Python terminates a line. "\n" is the default.

print became a real function in Python3 in Python2 is was kind of like a statement/keyword/function - that explains the need for () and also how it behaves differently.

Google and StackOverflow are your friends!

I almost know enough to be dangerous.

Rick

 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025