# Python Intro #01 | Basic Grammar

### Contents

- Basic Grammar
- Variables and Types
- Substitution
- Conditional Branch
- List
- Iteration
- Rhinoscriptsyntax

- Practice – Matrix of Points
- Nested Iteration

### Basic Grammar

#### Variables and Types

Programs are series of procedure to deal with bunch of variables. Variables are the main elements in computer programs and is sometimes compared to boxes to store data. Every variable have its data type. There are several several data types in python and other programming languages. Followings are popular variables in Python:

- bool – True/False variables
- int – Integer variables
- float – floating-point arithmetic
- str – Text variables

#### Substitution

“=” is a relational operator that indicates right-hand side and left-hand side are equal in mathematics, but is a operator to substitute left-hand side for right-hand side in programming.

Therefore we can write a code following:

a = x a = a+1

#### Conditional Branch

To branch processes in programming, use “if… elif… else…” syntax. There are several relational operators:

- > – larger than
- < – smaller than
- == – equal to
- != – not equal to
- >= – larger than or equal to
- <= – smaller than or equal to

if x>5 : a = "x is higher than 5" elif x<5 : a = "x is lower than 5" else : a = "x is 5"

The space before lines next to the “if”, “elif”, “else” lines are necessary to define paragraph in program.

#### List

Lists are variables that can store multiple values. We can declare lists and pick certain value from them like following:

x = [0,1,2,3,4,5,6,7,8,9] a = x[4]

As for the list of numbers, we can declare like following:

x = range(5,10) a = x

Lists in python are variable length list and are able to be added or removed .

- .append(val) – add “val” to the last value in the list
- .remove(N) – remove “N”th value in the list

x = [] x.append(10) x.append('text') x.remove(0) a = x[0]

#### Iteration

To iterate processes, we can use “for (variable) in (list)” syntax.

In each loop, variable is substituted from value in list. In other words, process iterates same times as the number of values in list.

a = [] for num in [0,1,2,3,4,5,6,7,8,9] : a.append(num)

In general, iteration is coded like following:

a = 0 for i in range(10) : a = a + i

#### Rhinoscriptsyntax

To use Rhino function in python, you can use rhinoscriptsyntax. Using rhinoscriptsyntax, you need to import the library. You can import the library by following:

import rhinoscriptsyntax as rs

There are bunch of functions in rhinosctiptsyntax. Followings are examples of them.

- rs.AddPoint(x, y, z) – draw a point with x,y,z coordinates
- rs.AddPolyline(list of points) – draw a polyline with a list of points
- rs.AddCircle(center point, radius) – draw a circle with center point and radius

You can draw a circle like following:

import rhinoscriptsyntax as rs a = rs.AddCircle(rs.Point(0,0,0), x)

For more information, you can check

Help -> Help for rhinoscriptsyntax

At the menu bar of Grasshopper Python Script Editor

### Practice – Matrix of Points

#### Nested Iteration

a = [] for i in range(10): for j in range(10): a.append(rs.AddPoint(i,j,0))