In this session, we continue our discussion of Python and present the five core elements of any Python script:
While learning a new programming language is always going to be difficult to some extent, one of the most difficult parts of getting started is not knowing the extent of what you don't know. So even if you're just getting started, it can be helpful to get the whole lay of the land, and at least understand the whole breadth of what you can do with Python at a high level while also digging in and getting practice with the hands-on demos.
Most of these core five elements share many similarities to concepts you have already encountered in Grasshopper, such as variables, data types, loops, and conditionals. The concept of creating your own functions to wrap up a set of operations into one statement is also similar to creating custom 'Clusters' in Grasshopper. Classes may be a bit more challenging to wrap your head around at first, but they are the key to 'Object-Oriented Programming', which is a very powerful concept once you get the hang of it. The good news is, once you understand these five basic elements, there will not be many new large concepts to tackle as you learn, only learning new techniques and getting more and more familiar with the language as you practice.
¶Sessions in this webinar series:
|1||Intro to Python|
|2||Elements of Python|
In the first lesson, we cover variables and discuss the basic data types supported by Python including integers (whole numbers), floats (decimal numbers), strings (text), and booleans (True/False). We also review how you can work with data stored in variables through different kinds of operators. Finally, we cover two types of data structures for storing multiple data items in a variable: lists and dictionaries. These two structures provide different ways of storing and working with data, and can even work together to organize data in very complex and flexible ways.
¶Loops and conditionals
In this lesson, we cover loops and conditionals, which are two basic structures in Python the let you control the order in which code is run in your script. Conditionals allow you to run or not run certain lines of code based on variables meeting certain conditions. Loops allow you to run lines of code a number of times, for example, to apply operations to all items inside of a list. Together, loops and conditionals are the easiest way to start to add complexity to your scripts and build up more complex logic in your generative models.
In this lesson, we cover functions in Python, and how they can be used to create a structure for performing a particular operation in code, making it easier to run the same process multiple times within your script. We cover simple functions built into Python, specialized functions that can be imported from external libraries such as the math library, as well as the basic code structure for defining your own custom functions in your script.
In this lesson, we cover classes, and how they can be used to encapsulate a set of variables and functions that describe the behavior of a particular element in your code. Classes can be hard to wrap your head around at first, but they are the key to a concept called "Object Oriented Programming" or OOP, which focuses on defining behaviors within objects and then uses those objects to accomplish the goals of the script. We cover basic classes like strings and lists which are included with Python, importing specialized classes from libraries such as Rhino Common, and the basic code structure for defining custom classes in your scripts.
¶Demo: Custom functions
In this lesson, we extend the basic attractor point demo from the previous lesson by adding a custom function to wrap the process of creating a circle with a radius based on the distance of its center from another point. Defining this process as a function makes it easier to apply this process multiple types in the code, for example, if we wanted to use a second attractor point to create an additional circle around each point.
¶Demo: Custom classes
In this lesson, we extend the attractor point demo further by creating a custom class definition that encapsulates the logic for creating a circle based on an attractor point. This allows us to break the logic between instantiating the object, calculating the radius, and producing the circle geometry into different methods, providing a good way to extend this logic in the future.
¶Demo: Two attractors
In this lesson, we finish our demo by showing how easy it is to add an additional attractor point to our model once we've defined the logic for creating the circles within a custom class.