Learning python

Hi!
I decided to learn python! Is Processing.py a viable learning tool? How hard is it to switch from processing.py to normal python?

Is it recommended? Are there any resources for learning python? Could I use processing.py while watching python tutorials? Are there any differences? A friend recommended Caleb Curry (youtube) so would his tutorials help?

1 Like

Welcome @CodeMasterX!

In my opinion (your mileage may vary) it is a viable path.

I have a few recommendations but knowing if you are already familiar with another programming language might help me calibrate better what to recommend…

For a great Processing.Py introduction I recommend @tabreturn’s https://tabreturn.github.io/#processing-reverse (or, maybe, long shot, my own github.com/villares/material-aulas if you can read Portuguese). Also, check out https://py.processing.org/tutorials/

For a general (standard) Python intro for someone who has never programmed before I like to recommend: https://greenteapress.com/wp/think-python/
UPDATE: I have seen you profile this might be a bit too introductory for you. You might enjoy the other non-Processing resource automatheboringstuff.com

It is good that you are aware that there are some differences and peculiarities to learning “standard” Python (Python 3 usually the “CPython implementation”). The main difference and peculiarity is that Processing Python mode is based on a particular Python 2 interpreter (called Jython) that can talk to Java and the Processing ecosystem of libraries, but you can’t use most of the modern Python 3 libraries. You can read more about it here: https://py.processing.org/tutorials/python-jython-java/

You might also try the new and very cool Python 3 “implementation in progress” of the Processing ideas! https://github.com/p5py/p5 (here at the forum: https://discourse.processing.org/c/p5py/27)

Good luck, have fun and welcome to the lovely Processing + Python world.

Cheers,
Alexandre

4 Likes

I’d agree with everything @villares has said.

I took a quick look over the code in the Caleb Curry Github repo, that’s referenced in the descriptions of his Python videos. I assume the video tutorials follow the code in those files. Most of it should work fine in Processing.py.

There are some exceptions – the print() function won’t ordinarily accept an end argument, and floating-point division won’t automatically apply to integers. However, you can add the following two lines to the top of any Processing.py script to get those features working like Caleb’s examples:

from __future__ import print_function
from __future__ import division

Additionally, add an object to the parentheses wherever you define a new class:

# this --
class Book():
# becomes this --
class Book(object):

Anything that uses Tkinter won’t work in Processing.py, but that’s only relevant for one or two of the final lessons from what I can tell. Tkinter is a GUI library for Python, which I suspect won’t be all that useful to you. You can likely replicate that functionality with Processing GUI libraries (like controlP5, etc.)

3 Likes

would I be able to simulate mouse clicks and button presses?

there is a bug where this happens
image

whenever I press enter, it stays at the same length. I have to use BACKSPACE to get to

function call() {
   print("aaaa")
}

it stays like that if I use ‘(’,’[’,’{’

is there a way to fix it?

Hi @CodeMasterX:

Was the above done in Python Mode? If so, you’ll need to do this instead:

def setup():
    print("aaaa")

but how do I know when the function ends?

def setup():
   aa
   bbb
   cc
   dd
x = 3

def abc():
   asd

or what do conditional sentences ( if - else) look like?

if(boolean):
   print("aaa")
   else:
      print("aaa")

Here’s some example code for Python Mode:

from random import shuffle
def setup():
    size(640, 640)
    noLoop()
    background(239)
    rectMode(CENTER)
    fill(255, 255, 255, 127)
    strokeWeight(2)
    stroke(0, 0, 127)
    
def draw():
    translate(width / 2, height / 2)
    CompositeSquare(500, 500, 4).render()
    
class CompositeSquare:
    def __init__(self, w, h, level):
        self.w = w
        self.h = h
        self.level = level
    def render(self):
        push()
        rotate(random(PI / 4) - PI / 8)
        rect(0, 0, self.w, self.h)
        if self.level > 1:
            coeffs = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
            shuffle(coeffs)
            for i in range(4):
                push()
                translate(coeffs[i][0] * self.w / 4, coeffs[i][1] * self.h / 4)
                CompositeSquare(self.w / 2, self.h / 2, self.level - 1).render()
                pop()
        pop()

In Python, functions begin with def. Block headers end with a colon. Code contents are defined by their indentation. So, when the indentation decreases, we’ve left the code block.

For an example of a boolean expression, here’s an if block header:

        if self.level > 1:

In these 3 links below you’re gonna find the same sketch written in Python Mode, Java Mode & p5.js flavors so you can compare their syntax: :link:

3 Likes