Design a site like this with
Get started

[Code] Python and Houdini Python (1)

Learning Python, so here is jotted-down notes that might be useful


* 1 – What Python is and includes

  • Python shell in Houdini
  • Variable functions: set
  • Class – Instances – OOP
  • inside an object, a function is called a ‘method’
  • OOP is like group together functions that make sense to each object specifically
  • Python Source Editor saves into the hip file
  • make a habit of writing ‘import hou’ into Python Editor everytime

=> x = object.method(argument)

* 2 – Python Data types, capital sensitive

  • Numeric: float, integer
  • String: stored in quotes ” or “”, nested quotes go from single to double quotes
  • Arrays: list (square brackets, order matters), tuple (round brackets, cannot change later), set (curly brackets, duplicate allowed in a set, unordered), dictionary (curly brackets, has a key and a value pairing)
    For example:
    mylist = [1, 5, 6]
    mytuple = (3, 6, 9)
    myset = {25, 10,15}
    mydict = {‘age’: 20, ‘name’: ‘Huy’}
    => Arrays in Python can have mixed datatype
  • Bool: True, False
  • Null: None
  • Class & Functions & Modules are also date types
  • Convert data types: float(), int(), str()

* 3 – Maths

  • Type casting affects mathematical functions
  • Power: 3**2 <=> 3^2
  • Floor division: 10.0 // 3 = 3.0, divide then drop the fraction
  • Order: BODMAS – Brackets, Orders, Divisions, Multiplications, Addition, Subtractions

* 4 – Logic

  • access and check statements > return a Bool
  • Special keywords for logic: ‘and’, ‘or’, ‘not’, ‘is’,…
  • ‘is’ => used for checking two things are the same object
  • id(variable) => check for footprint
  • Class has the same footprint no matter what variable they’re stored in, but the same instance of class has different footprints in different variables
  • If functions: if: – elif: – else:

* 5 – Arrays

  • tuple: round brackets, cannot change later, only way to change is overwrite
  • list: square brackets, order matters, can change values,
    functions include append, add, extend, del, remove, pop, sort, reverse, slicing a range of values, copy,…
  • set: curly brackets, duplicate not allowed in a set, unordered
    can loop over set, or check items
    functions include union (merge 2 arrays but not change original set), update (merge 2 arrays and change original set), functions have 2 version that either keep or update the original version
    difference (check difference from set A – B) != symmetric_difference (check difference from A & B)
    can add but not append (set has no order)
    can remove or discard a value from set, pop removes a random item from set
  • dictionary: curly brackets, has “a key and a value” pairing
    key should be string, value can be anything,
    new key’s value will overwrite old value
    create dictionary: feed info into dict, or initialize empty dict and append values to it
    can only check keys but not values
    functions include update, del, pop, clear
  • Arrays in Python can have mixed datatype
  • Array types can be converted interchangeably
  • Use square brackets to access data within arrays
  • For examples:
    mylist = [‘foo’, 6, 8.4, 5.7, 1, 1, 1]
    mytuple = (3, 6, 9, 12)
    myset = {25, 10,15}
    mydict = {‘age’: 20, ‘name’: ‘Huy’}

    mytuple[0] => 3
    mytuple[-1] => 9
    mytuple.index(6) => 1, searched the array and find the first index that has the value
    mytuple.count(6) => 1 (found 1 instance of 6 in this tuple)
    9 in mytuple => True

    mylist.index[2] = 3.86 => set index no. 2 of this list
    mylist.append(‘else’) => added at the end
    mylist.insert(4, ‘four’) => insert ‘four’ before index no. 4
    mylist.append(mytuple) => array of a nested tuple inside a list
    mylist.extend(mytuple) => added each item in the tuple to list
    del mylist[-2] => remove an index
    mylist.remove(5) => remove the first occurence of 5 in mylist
    mylist.pop() => removes last item from the list, and print the pop value
    mylist.sort() => sort aplhanumerically (number to strings, integer to float, then alphabetical for strings)
    mylist.sort(reverse=1) => reverse the order
    mylist.sort(key=……) => insert sorter function
    mylist.reverse => reverse the list, but not sort it
    mylist[3:5] => slice mylist[start:end]
    y = mylist[:] => copy the list to variable y, they will have different footprints

    myset2 = myset.union(myset) => copy the set
    myset.isdisjoint(mytuple) => returns True if 2 arrays have nothing in common
    myset.issuperset() => check if one is superset of the other, similar with .issubset to check subset

    mydict.fromkeys(mylist) => populate the tags with values from mylist, but my initialize empty dict in advance
    mydict[‘car’] => Ferrari => access values in dict by the tag name
    mydict.get(‘top_speed’) => access values but not report error for non-existence keys
    mydict.items() => list of Dict’s (key, value) pairs as tuples
    mydict.keys() => return keys only
    mydict.values() => return values only
    mydict[‘colour’] = (1.0, 0.0, 0.0) => set new, or overwriting value of the already existent ‘colour’ key (no duplicates allowed)

=> unchangeable: tuple;
order matters: list
unordered, no duplicates: set and dictionary;
has tags: dictionary

  • list comprehensions: single line list not stored anywhere
    For example: [x*2 for x in range(10) if x > 5] => [12, 14, 16, 18]

* 6 – Loops


  • Definition: create a variable to store the item that is being iterated through an array, apply a block of code to each of those item. In next loop, the variable changes to next item.
  • Loop through dictionary: loop over keys or values by .keys() or .values() functions, or .items() will go over list of tuples
    For examples:
    for k in mydict.keys()
    for v in mydict.values()
    for k, v in mydict.items()
  • Loop in range(num) or range(start, stop, step/increment)
  • break: exit loop completely
    continue: skip the rest of block then go back to loop
  • else: after successfully completed for/while loop, do block in else

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: