Python Strings and Lists

In [3]:
s = "abcdefghijklm"
In [4]:
len(s)
Out[4]:
13
In [5]:
s[7]
Out[5]:
'h'
In [7]:
s[7:12]
Out[7]:
'hijkl'
In [8]:
s[7:12] + s[2:5]
Out[8]:
'hijklcde'
In [9]:
a = [10,20,30,40,50,60]
In [10]:
len(a)
Out[10]:
6
In [11]:
a[2:4]
Out[11]:
[30, 40]
In [12]:
a[2:4] + [90,100]
Out[12]:
[30, 40, 90, 100]
In [17]:
t = 'x"y"z'
u = "def"
v = '''lmn
opq
rst'''
In [18]:
len(v)
Out[18]:
11
In [19]:
t+u+v
Out[19]:
'x"y"zdeflmn\nopq\nrst'
In [20]:
print(t+u+v)
x"y"zdeflmn
opq
rst
In [21]:
b = ["John", "Jimmy","Alice"]
In [22]:
len(b)
Out[22]:
3
In [23]:
len(b[0])
Out[23]:
4
In [24]:
HANGMANPICS = ['''

  +---+
  |   |
      |
      |
      |
      |
=========''', '''

  +---+
  |   |
  O   |
      |
      |
      |
=========''', '''

  +---+
  |   |
  O   |
  |   |
      |
      |
=========''', '''

  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''

  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========''', '''

  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========''', '''

  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========''']
In [25]:
len(HANGMANPICS)
Out[25]:
7
In [27]:
len(HANGMANPICS[0])
Out[27]:
59
In [29]:
len(HANGMANPICS[2])
Out[29]:
59

For Loop

In [36]:
x = list(range(17,46))
In [37]:
print(x)
[17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45]
In [41]:
for x in range(5,10):
    print(x*x*x)
125
216
343
512
729
In [42]:
for x in b:
    print(x)
John
Jimmy
Alice
In [43]:
for x in HANGMANPICS:
    print(len(x))
59
59
59
59
59
59
59
In [44]:
## Accumulator Pattern of Programming
picLengths = []
for x in HANGMANPICS:
    picLengths.append(len(x))  # accumulate length of picture into picLengths
## end of pattern
print(picLengths)
[59, 59, 59, 59, 59, 59, 59]
In [ ]: