# Learning python

**URL:** <https://discourse.processing.org/t/learning-python/26492>\
**Category:** Processing.py\
**Created:** [December 24, 2020, 12:34pm UTC](https://discourse.processing.org/t/learning-python/26492 "2020-12-24T12:34:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 24, 2020, 12:34pm UTC](https://discourse.processing.org/t/learning-python/26492/1 "2020-12-24T12:34:02Z")

</div>

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)](https://www.youtube.com/c/CalebTheVideoMaker2/videos) so would his tutorials help?

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [December 24, 2020, 1:37pm UTC](https://discourse.processing.org/t/learning-python/26492/2 "2020-12-24T13:37:28Z")

</div>

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](https://tabreturn.github.io/#processing-reverse) (or, maybe, long shot, my own [github.com/villares/material-aulas](http://github.com/villares/material-aulas) if you can read Portuguese). Also, check out [https://py.processing.org/tutorials/](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/](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](http://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/](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](https://github.com/p5py/p5) (here at the forum: [https://discourse.processing.org/c/p5py/27](https://discourse.processing.org/c/p5py/27))

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

Cheers,  
Alexandre

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [December 24, 2020, 10:44pm UTC](https://discourse.processing.org/t/learning-python/26492/3 "2020-12-24T22:44:05Z")

</div>

I’d agree with everything @villares has said.

I took a quick look over the code in the Caleb Curry [Github repo](https://github.com/CalebCurry/python), 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:

```python
from __future__ import print_function
from __future__ import division

```

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

```python
# 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.)

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 25, 2020, 11:33am UTC](https://discourse.processing.org/t/learning-python/26492/4 "2020-12-25T11:33:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 25, 2020, 12:19pm UTC](https://discourse.processing.org/t/learning-python/26492/5 "2020-12-25T12:19:15Z")

</div>

there is a bug where this happens  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/26e40269e72d8dadcf98335598ae752d77bd7b5f.png)

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

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

```

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

is there a way to fix it?

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 25, 2020, 12:40pm UTC](https://discourse.processing.org/t/learning-python/26492/6 "2020-12-25T12:40:02Z")

</div>

Hi @CodeMasterX:

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

```auto
def setup():
    print("aaaa")

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 25, 2020, 12:45pm UTC](https://discourse.processing.org/t/learning-python/26492/7 "2020-12-25T12:45:01Z")

</div>

but how do I know when the function ends?

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

def abc():
   asd

```

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

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

```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 25, 2020, 12:53pm UTC](https://discourse.processing.org/t/learning-python/26492/8 "2020-12-25T12:53:18Z")

</div>

Here’s some example code for Python Mode:

```auto
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:

```auto
        if self.level > 1:

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 25, 2020, 8:04pm UTC](https://discourse.processing.org/t/learning-python/26492/9 "2020-12-25T20:04:15Z")

</div>

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: 🔗

> [@Issues with smooth 2D movement in python](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556/4):
>
> That’s a very old sketch of mine. innocent BtW, I did a followup w/ 2 players instead of 1 both for Java Mode & p5js flavors: sunglasses And now I’ve decided to do a Python Mode version as well. Check it out: snake #!/bin/python2 """ Players Movement (v1.1.0) by GoToLoop (2020/Mar/11) Discourse.Processing.org/t/issues-with-smooth-2d-movement-in-python/18556/4 GoToLoop.Trinket.io/sites/players-movement Trinket.io/processing/62fab051ed Reddit.com/r/processing/comments/bshqa8/ ho…

> [@Vertex, loop and draw along a vertex path](https://discourse.processing.org/t/vertex-loop-and-draw-along-a-vertex-path/4545/4):
>
> And while I was at it, I did a Python Mode version too: snake “Knot3D.pyde”: """ \* 3D Knot (2017/Dec) \* Daniel Shiffman \* https://YouTu.be/r6YMKr1X0VA \* \* Mod GoToLoop (v1.0.3) (2018/Oct/16) \* https://OpenProcessing.org/sketch/608725 (pjs) \* https://OpenProcessing.org/sketch/608726 (p5js) \* \* https://Discourse.Processing.org/t/ \* vertex-loop-and-draw-along-a-vertex-path/4545/4 """ """ \* http://PaulBourke.net/geometry/knots/ \* Knot 4 (1992/Oct): \* \* r(beta) = 0.8 + 1.6 \* sin(6 \*…

> [@Reducing points in polygon](https://discourse.processing.org/t/reducing-points-in-polygon/13590/3):
>
> I’ve made a Python Mode version too: snake RamerDouglasPeucker.pyde: """ \* Ramer-Douglas-Peucker Algorithm (v1.2.3) \* GoToLoop & JeffThompson (2019/Aug/27) \* \* https://Discourse.Processing.org/t/reducing-points-in-polygon/13590/3 \* \* https://en.Wikipedia.org/wiki/Ramer-Douglas-Peucker\_algorithm \* https://Karthaus.nl/rdp/js/rdp2.js \* \* https://Bl.ocks.org/GoToLoop/a5db257be4d7756a00220a3e97066dd5 \* https://www.OpenProcessing.org/sketch/747172 """ from Calc import reducePolygon from…
