Welcome to AR-327 !!


Today's learning objectives:

  • Understand the fundamentals of programming
  • Get familiar with programming vocabulary
  • Get familiar with an IDE
  • Write your first basic script

What is programming?

What is programming?



To answer this question, let's start by looking at some of your assignments:



Here I put some of the assignments done by the students, amongst the ones submitted in advance.

What is programming?

A program is a set of instructions that a computer can execute to achieve a specific task.

Compile a program

What is programming?

A program is a set of instructions that we can write and that a computer can execute.

Compile a program

There are two ways to run a program:

A program can be either compiled into an executable and run at any time by a computer. In that case 1 compiled file can be executed multiple times.

Compile a program

There are two ways to run a program:

A program can also be interpreted immediately. In that case 1 source file needs to be interpreted each time we want to run the program.

Compile a program

Python is an interpreted language

Why programming in Architecture?

Why programming in Architecture?

Parametric design
Parametric Design
Design analysis
Design Analysis
Digital fabrication
Digital Fabrication

Why programming in Architecture?

Programming goals

Where to Program? (IDE)

Where to Program? (IDE)

Developers use an IDE (Integrated Development Environment) to write and test their code.

It is a text editor with additional features to help writing code.

IDE

Why use an IDE ?

		
            #! python3
            def hello_world(a: int) -> None: 
                print( a * "Hello, World!")
        
    

Why use an IDE ?

To get syntax highlighting:

		
            #! python3
            def hello_world(a: int) -> None: 
                print( a * "Hello, World!")
        
    
		
            #! python3
            def hello_world(a: int) -> None: 
                print( a * "Hello, World!")
        
    

Why use an IDE ?

To get intelliSense (code completion):

IntelliSense

Why use an IDE ?

And many other features...

  • Code formatting and linting
  • Debugging tools
  • AI-powered code suggestions
  • Versionning integration
  • Cool extensions and plugins
  • ...


The Rhino IDE is relatively basic but offers the most important features.

Let's open Rhino's Python Editor

in Rhino, type _ScriptEditor in the command line

Let's open Rhino's Python Editor

Rhino Script Editor

Let's open Rhino's Python Editor

Rhino Script Editor

Let's open Rhino's Python Editor

Rhino Script Editor

Excercise: Run your first script (10 min)

And now, let's dive into Python

From Python website:

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.

From Python website:

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.

😬

Let's break it down:

  • It is an interpreted language
  • it can follow the object-oriented paradigm
  • it is a high-level programming language
  • it has dynamic typing and binding
  • it has an extensive standard library

Which python in Rhino?

Rhino uses two versions of Python: IronPython (Python 2.7) and CPython (Python 3.X)
CPython IronPython
We will use CPython (currently Python 3.9) in this course.

Write and run your first Python script (10 min)

  • Open Rhino's Python Editor
  • Type the following code:
  • 		        
                        # My first Python script
                        my_variable = "Hello, World!"
                        print(my_variable)
                    
                
  • Run the script


If you see "Hello, World!" in the terminal, congrats! You just wrote and executed your first Python script! 🎉

Variables in Python 🐍

Variables in Python 🐍

A variable is like a box.
Variable box

Variables in Python 🐍

A variable is like a box.
In this box we can store data.
Variable box Data symbol Arrow

Variables in Python 🐍

A variable is like a box.
In this box we can store data.
Variable box Data symbol
Data 1 boolean values Data 2 integer and float values Data 3 string values

And how can we create a variable in Python?

Let's look at the code we wrote in the previous exercice:
	    
            # My first Python script
            my_variable = "Hello, World!"
            print(my_variable)
        
    

And how can we create a variable in Python?

Let's look at the code we wrote in the previous exercice:
	    
            # My first Python script
            my_variable = "Hello, World!"
            print(my_variable)
        
    
In python, we always create a variable and immediately assign a value to it (There is no empty box).

And how can we create a variable in Python?

Let's look at the code we wrote in the previous exercice:
	    
            # My first Python script
            my_variable = "Hello, World!"
            my_other_variable = my_variable # What will be the value of my_other_variable?
            print(my_other_variable)
        
    
In python, we always create a variable and immediately assign a value to it (There is no empty box).

Python syntax

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!"
            my-variable = "Hello, World!"
            myVariable = "Hello, World!"
            MyVariable = "Hello, World!"
            my variable = "Hello, World!"
            MYVARIABLE = "Hello, World!"
            Myvariable = "Hello, World!"
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!"
            myVariable = "Hello, World!"
            MyVariable = "Hello, World!"
            my variable = "Hello, World!"
            MYVARIABLE = "Hello, World!"
            Myvariable = "Hello, World!"
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!" # Invalid, algebra symbols are not allowed
            myVariable = "Hello, World!"
            MyVariable = "Hello, World!"
            my variable = "Hello, World!"
            MYVARIABLE = "Hello, World!"
            Myvariable = "Hello, World!"
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!" # Invalid, algebra symbols are not allowed
            myVariable = "Hello, World!" # Valid, camelCase (not common in Python)
            MyVariable = "Hello, World!"
            my variable = "Hello, World!"
            MYVARIABLE = "Hello, World!"
            Myvariable = "Hello, World!"
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!" # Invalid, algebra symbols are not allowed
            myVariable = "Hello, World!" # Valid, camelCase (not common in Python)
            MyVariable = "Hello, World!" # Valid, PascalCase (not common in Python)
            my variable = "Hello, World!"
            MYVARIABLE = "Hello, World!"
            Myvariable = "Hello, World!"
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!" # Invalid, algebra symbols are not allowed
            myVariable = "Hello, World!" # Valid, camelCase (not common in Python)
            MyVariable = "Hello, World!" # Valid, PascalCase (not common in Python)
            my variable = "Hello, World!" # Invalid, spaces are not allowed
            MYVARIABLE = "Hello, World!"
            Myvariable = "Hello, World!"
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!" # Invalid, algebra symbols are not allowed
            myVariable = "Hello, World!" # Valid, camelCase (not common in Python)
            MyVariable = "Hello, World!" # Valid, PascalCase (not common in Python)
            my variable = "Hello, World!" # Invalid, spaces are not allowed
            MYVARIABLE = "Hello, World!" # Valid, all uppercase, but reserved for constants
            Myvariable = "Hello, World!" 
        
    
What can we say about those variable names?

Which rules to write code in Python?

	    
            # My first Python script
            my_variable = "Hello, World!" # Valid, snake_case
            my-variable = "Hello, World!" # Invalid, algebra symbols are not allowed
            myVariable = "Hello, World!" # Valid, camelCase (not common in Python)
            MyVariable = "Hello, World!" # Valid, PascalCase (not common in Python)
            my variable = "Hello, World!" # Invalid, spaces are not allowed
            MYVARIABLE = "Hello, World!" # Valid, all uppercase, but reserved for constants
            Myvariable = "Hello, World!" # Valid, but not following common conventions
        
    
What can we say about those variable names?

Comments in Python

Comments in Python

	    
            # This is a single line comment.
            my_variable = "Hello, World!"
            print(my_variable)
        
    

Comments in Python

	    
            """
            This is a multi-line comment.

            It can span multiple lines and skip lines.
            It is often used for code documentation (docstrings).
            """

            my_variable = "Hello, World!"
            print(my_variable)
        
    

Char and strings

Char and strings

Variable box Data symbol Arrow

Char and strings

A char is a single character (letter, number, symbol) enclosed in quotes.

A string is a sequence of characters enclosed in quotes (" " or ' ').

For example:
        
            my_string1 = 'Hello, World!'
            my_string2 = "Hello, World!"
            my_string3 = "It's a beautiful day!"
            my_string4 = 'He said, "Hello!"'
        
    

Char and strings

We can retrieve a specific character from a string using indexing (starting at 0).
        
            my_string = 'Hello, World!'
            my_char = my_string[7]  # 'W' or ' '?
        
    

Char and strings

We can retrieve a specific character from a string using indexing (starting at 0).
        
            my_string = 'Hello, World!'
            my_char = my_string[7]  # 'W'!
        
    

Char and strings

We sum strings using the + operator (also called concatenation).
        
            my_first_string = 'Hello, World!'
            my_second_string = " How are you?"
            my_summed_string = my_first_string + my_second_string
            print(my_summed_string)
        
    

Char and strings

We sum strings using the + operator (also called concatenation).
        
            my_first_string = 'Hello, World!'
            my_second_string = " How are you?"
            my_summed_string = my_first_string + my_second_string
            print(my_summed_string) # Hello, World! How are you?
        
    

Excercise: Variables and strings (10 min)

  • Open Rhino's Python Editor
  • From the website, go to the "String" page and download the exercice 01 and exercice 02 at the bottom of the page
  • Open it in the editor, and run the script
  • Try to understand what each line of code does
  • Modify the code to print your own message

Numbers

Numbers

We can make the distinction between two types of numbers: integers and floats.

  • Integers are whole numbers (positive or negative) without a decimal point. Examples: -3, 0, 25432372
  • Floats (or floating-point numbers) are numbers that have a decimal point. Examples: -3.14, 0.0, 2.0

Numbers

        
            # Examples of integers and floats
            my_integer = 10
            my_float = 3.14
            print("Integer:", my_integer)
            print("Float:", my_float)
        
    

Numbers

        
            # Examples of integers and floats
            my_integer = 10
            my_float = 10.0
            print("Integer:", my_integer)
            print("Float:", my_float)
        
    

Numbers

        
            # Examples of integers and floats
            my_integer = 10
            my_float = 10.0 # what is really stored in memory: 01000001001000000000000000000000
            print("Integer:", my_integer)
            print("Float:", my_float)
        
    
The decimal 10.0 is stored in memory as 32 bits: 01000001001000000000000000000000.

Numbers

        
            # Examples of integers and floats
            my_integer = 10
            my_float = 10.0 # what is really stored in memory: 01000001001000000000000000000000
            print("Integer:", my_integer)
            print("Float:", my_float)
        
    
with the same amount of memory, we can store a much larger integers than floats.

Number operators

        
            a = 10 + 3      # Addition,
            b = 10 - 3      # Subtraction,
            c = 10 * 3      # Multiplication,
            d = 10 / 3      # Division (float),
            e = 10 // 3     # Division (integer), result is 3
            f = 10 % 3      # Modulus (remainder), result is 1
            g = 10 ** 3     # Exponent, result is 1000
        
    

Excercise: numbers (10 min)

  • Open Rhino's Python Editor
  • From the website, go to the "int-float" page and download the exercice 01 and exercice 02 at the bottom of the page
  • Open it in the editor, and run the script
  • Solve the exercices

Booleans

Booleans

A boolean is a data type that can have one of two possible values: True or False.
Boolean

Booleans

Typically variables that store boolean values start with "is" or "has":
        
            is_processed = True
            has_data = False
            print("Is processed:", is_processed)
            print("Has data:", has_data)
        
    

Booleans and comparison operators

boolean operators are operators that return a boolean value:
        
            a = 10
            b = 3
            is_equal = (a == b)          # False
            is_not_equal = (a != b)      # True
            is_greater = (a > b)         # True
            is_less = (a < b)            # False
            is_greater_equal = (a >= b)  # True
            is_less_equal = (a <= b)     # False
        
    

Control flow

Control flow

Control flow consists in making decisions in our code based on certain conditions:
        
            a = 10
            b = 3
            if CONDITION:
                # Do something if CONDITION is True

            else:
                # Do something else if CONDITION is False

            while CONDITION:
                # Do something while CONDITION is True
        
    

Control flow

Control flow consists in making decisions in our code based on certain conditions:
        
            a = 10
            b = 3
            if a > b:
                print("a is greater than b") # This line is executed
            elif a < b:
                print("a is less than b") # This line is NOT executed
            else:
                print("a is equal to b") # This line is NOT executed
        
    

Control flow

The conditions can be combined using logical operators:
  • and: True if both conditions are True
  • or: True if at least one condition is True
  • not: Inverts the boolean value of a condition
        
            a = 10
            if a > 0 and a % 2 == 0:
                print("Pop-quiz, what would you print here")
            elif a < 0 or a % 2 != 0:
                print("Pop-quiz, what would you print here")
            elif a not in [1, 3, 5, 7, 9]: # PS: this is a list, we will see it next week
                print("Pop-quiz, what would you print here")
        
    

Control flow

The conditions can be combined using logical operators:
  • and: True if both conditions are True
  • or: True if at least one condition is True
  • not: Inverts the boolean value of a condition
        
            a = 10
            if a > 0 and a % 2 == 0:
                print("a is a positive even number")
            elif a < 0 or a % 2 != 0:
                print("a is either negative or odd")
            elif a not in [1, 3, 5, 7, 9]: # PS: this is a list, we will see it next week
                print("a is not a single-digit odd number")
        
    

Control flow

        
            a = 10
            while a > 0:
                print(a)
                a -= 1  # Decrease a by 1
            print("What would you print here?")
        
    

Excercise: Control flow (10 min)

  • Open Rhino's Python Editor
  • From the website, go to the "control flow" page and download the exercice 01 and exercice 02 at the bottom of the page
  • Open it in the editor, and run the script
  • Solve the exercices

Enough for today!

Bye bye

But for next week...

Assignment for next week (due next week)

  • Finish the exercices you didn't manage to finish during the class -> exercice yourself as much as possible, as for any language.
  • The two parts of the assignment are on moodle, due for next Thursday 13:00