User:Orso.b.schmid: Difference between revisions

From Vectorworks Developer
Jump to navigation Jump to search
(comments of list creation)
m (encode in py wiki code wrapper)
Line 6: Line 6:
Open the terminal on your Mac and write
Open the terminal on your Mac and write
  python
  python
try snippets there or in an editor of your choice


== Lists ==
== Lists ==


Lists are powerful in Python, below some fascinating lists manipulations. They remind me of Applescript:
Lists are powerful in Python, below some fascinating lists manipulations. They remind me of Applescript:
months = "Jan Feb Mar Apr May Jun Jul"
<code lang="py">
months = months.split() # no splitter defined and it will use the empty space --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months = "Jan Feb Mar Apr May Jun Jul"
months2 = "Jan, Feb, Mar, Apr, May, Jun, Jul" # overwrite a copy
months = months.split() # no splitter defined and it will use the empty space --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months2.split(', ') # use comma and empty space as splitter --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months[2] # note that the index is 0-based --> 'Mar'
months.append('Jul') # append adds an item to a list --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months2 = "Jan, Feb, Mar, Apr, May, Jun, Jul" # overwrite a copy
months.pop() # pop fetches the last item of a list --> 'Jul'
months2.split(', ') # use comma and empty space as splitter --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
', delirious '.join(months) # --> 'Jan, delirious Feb, delirious Mar, delirious Apr, delirious May, delirious Jun, delirious Sep'
months.append('Jul') # append adds an item to a list --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
'-'.join(months[1:3]) # --> 'Feb-Mar'
months.pop() # pop fetches the last item of a list --> 'Jul'
', delirious '.join(months) # --> 'Jan, delirious Feb, delirious Mar, delirious Apr, delirious May, delirious Jun, delirious Sep'
'-'.join(months[1:3]) # --> 'Feb-Mar'
del months[2] # --> ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Jul']
months = {1: 'Jan', 2: 'Feb', 3: 'Mar'} # --> {1: 'Jan', 2: 'Feb', 3: 'Mar'}
</code>

Revision as of 13:15, 17 May 2015

Ciao,

I am Orso, an Italian Vectorscripter since many years. I feel very comfortable with VS but will now switch over to Python for even more power. I will try to share here comments, problems -and solutions- from the point of view of a non-programmer. --Orso.b.schmid (talk) 08:13, 17 May 2015 (EDT)

Open the terminal on your Mac and write

python

try snippets there or in an editor of your choice

Lists

Lists are powerful in Python, below some fascinating lists manipulations. They remind me of Applescript:

months = "Jan Feb Mar Apr May Jun Jul"
months = months.split() # no splitter defined and it will use the empty space --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months[2] # note that the index is 0-based --> 'Mar'
months2 = "Jan, Feb, Mar, Apr, May, Jun, Jul" # overwrite a copy
months2.split(', ') # use comma and empty space as splitter --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months.append('Jul') # append adds an item to a list --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months.pop() # pop fetches the last item of a list --> 'Jul'
', delirious '.join(months) # --> 'Jan, delirious Feb, delirious Mar, delirious Apr, delirious May, delirious Jun, delirious Sep'
'-'.join(months[1:3]) # --> 'Feb-Mar'
del months[2] # --> ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Jul']
months = {1: 'Jan', 2: 'Feb', 3: 'Mar'} # --> {1: 'Jan', 2: 'Feb', 3: 'Mar'}