Sunday, January 15, 2012

Google Python Class Day 1 Part 1 - Very Rough Notes

Google Python Class Day 1 Part 1 - Very Rough Notes

Youtube Video

## Dir & Help (high tech way to get more info..)

import sys

dir(sys.argv)
help(sys.argv)
help(sys.exit)
help(len)

## And it turns out the easy way is to use Google ;)
-------------------------------
## REM
# As usual comment/rem works till the end of the line
-------------------------------
## " & '
## You can use either (note the \ if you want to use quotes inside quotes)
>>> a = 'hello'
>>> a = "hello"
>>> "I \"love\" this exercise"
'I "love" this exercise'
>>>
>>> a = "isn't"
>>> a
"isn't"
>>>
-------------------------------
## +
## Puts things together to make a bigger string
>>> a + ' yay!'
"isn't yay!"
>>>
-------------------------------
## To get a length of a string
>>> len(a)
5
>>>
-------------------------------
## To make lowercase run a method (in this case 'lower') on an object (in this case 'a')
>>> a = 'Hello'
>>> a.lower()
'hello'
## Or
>>> b = 'YAY!'
>>> b.lower()
'yay!'
>>>
## Note: The original string is unchanged (as below)
>>> b
'YAY!'
>>>
-------------------------------
## To look Inside of a string
>>> a[0]
'H'
>>> a[1]
'e'
>>> a[2]
'l'
>>> a[3]
'l'
>>> a[4]
'o'
>>> a[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
-------------------------------
## Format String - Another form of a + to put something together %s (% sign combined with something and substitue values to make bigger string, you could use the +)
>>> 'Hi %s I have %d donuts' % ('Nico', 42)
'Hi Nico I have 42 donuts'
>>>
-------------------------------
## Conventional Indexing into something
>>> a = "Hello"
>>> a[0]
'H'
>>> a[1]
'e'
>>> len(a)
5
>>>
-------------------------------
## Referring to sub-part of an element using the colon :
## Start at one and go up to but not including 3 (very pythonic feature.. in other words, using short crisp syntax to express what you want to do)
## aka a Slice (slice syntax)
>>> a[1:3]
'el'
>>>
>>>
>>> a[1:5]
'ello'
>>> a[1:4]
'ell'
>>> a[1:]
'ello'
>>> a[:]
'Hello'
>>>
Thid one is basically removing the first letter (as leaving blank displys the complete string)
>>> a[1:]
'ello'
>>>
## You can also use negative numbers

Figure 1 - Positive and Negative Numbering
H   e  l   l   o
0   1  2  3  4
-5 -4 -3 -2 -1

>>> a[-0:]
'Hello'
>>> a[-1:]
'o'
>>> a[-2:]
'lo'
>>> a[-3:]
'llo'
>>> a[-4:]
'ello'
>>> a[-5:]
'Hello'
>>>
This is the SAME AS
>>> a[-4:-2]
'el'
THIS
>>> a[1:3]
'el'
>>>

0 comments: