iTarbuck
Posts: 2
Joined: Sun Apr 28, 2019 4:36 pm

Learning to code with Pi.

Sun Apr 28, 2019 4:40 pm

Hi all,

I have just got my new Pi 3b+, and I am looking to learn how to write code.

Where should I be looking, what apps should I be using?

Any help would be greatly appreciated.

Thanks! :D

LTolledo
Posts: 1544
Joined: Sat Mar 17, 2018 7:29 am

Re: Learning to code with Pi.

Sun Apr 28, 2019 11:50 pm

For RPi, the "default" is usually coding using python...

Lots of resources at the Programming section of the Forum, go have a peek inside...
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

knute
Posts: 424
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Learning to code with Pi.

Mon Apr 29, 2019 1:20 am

And the best way to learn how to code is to code! It's like playing a musical instrument, practice, practice, practice...

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

Re: Learning to code with Pi.

Mon Apr 29, 2019 5:35 am

iTarbuck,
I have just got my new Pi 3b+, and I am looking to learn how to write code.
Open a terminal window on your Pi and type the command "python" and see what happens:

Code: Select all

$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The ">>>" there is the Python command prompt, an invitation to type in some Python code.

Now type the command 'print ("Hello World")' and see what happens:

Code: Select all

>>> print ("Hello World")
Hello World
>>>
Cool, isn't it? You have just written a line of Python code and it printed out the message you told it to.

Now try something like:

Code: Select all

>>> 3 + 9
12
Even more cool, it does maths for you.

Now try:

Code: Select all

>>> a = 3
>>> b = 9
>>> c = a + b
>>> print (c)
12
Far more cool, you can give names to things, "a", "b", "c" in this case. And then do maths using those names.

Those named "variables" also remember things. Just type "c":

Code: Select all

>>> c
12
Until you change them again. Try:

Code: Select all

>>> c = a * b
>>> print (c)
27
Now for some magic, type this:

Code: Select all

>>> if (a < b) : print ("a is smaller than b")
...
a is smaller than b
>>>
Awesome. Python can make decisions. In this case the number "a" is smaller than "b" so it says so.

Make "a" bigger or equal to "b" and will not:
>>> a = 100
>>> if (a < b) : print ("a is smaller than b")
...
>>>
There we go. You have now started to lean programming in Python. For further details see google for "python tutorials" and such like.

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

Re: Learning to code with Pi.

Mon Apr 29, 2019 6:53 am

I suggest using Python3 by the way.

Also pre-installed and ready to use are C and C++ if you would like to try a faster compiled language.

Type this into a file called "hello.c". The nano editor is simple, but any editor of your choice will work fine.

nano hello.c

Code: Select all

#include <stdio.h>

int
main( void )
{
  puts( "hello world!" );
}
Then compile it:

cc hello.c -o hello

Next run it:

./hello

It should look like this:

Code: Select all

[email protected]:~ $ cc hello.c -o hello
[email protected]:~ $ ./hello
hello world!
[email protected]:~ $ ./hello
hello world!
You have made a new command which you can run as many times as you like!

There are many books on C and Python which you can use to learn programming on the Pi.

As said above, the best way to learn is to actually do it!
It costs nothing and you will not hurt the Pi if you make a mistake.


Return to “General discussion”