In [1]:
odds = [1, 3, 5, 7]
print 'odds are:', odds


odds are: [1, 3, 5, 7]

In [2]:
print 'first number:', odds[0], 'last number:', odds[-1]


first number: 1 last number: 7

In [3]:
for number in odds:
    print number


1
3
5
7

In [4]:
for number in '1357':
    print number


1
3
5
7

In [5]:
names = ['Newton', 'Darling', 'Plaatje']
print 'names are originally:', names
names[1] = 'Darwin'
print 'names are now:', names


names are originally: ['Newton', 'Darling', 'Plaatje']
names are now: ['Newton', 'Darwin', 'Plaatje']

In [6]:
string = 'Hello'
print 'string is now:', string
string[0] = 'h'
print 'string is now:', string


string is now: Hello
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-76ec8d945ec2> in <module>()
      1 string = 'Hello'
      2 print 'string is now:', string
----> 3 string[0] = 'h'
      4 print 'string is now:', string

TypeError: 'str' object does not support item assignment

In [7]:
string = 'Hello'
print 'string is now:', string
string = 'hello'
print 'string is now:', string


string is now: Hello
string is now: hello

In [9]:
odds = [1,3,5,7]
print 'odds are:', odds
odds.append(9)
print 'odds are:', odds


odds are: [1, 3, 5, 7]
odds are: [1, 3, 5, 7, 9]

In [10]:
del odds[0]
print 'odds is now:', odds
odds.reverse()
print 'odds is now:', odds


odds is now: [3, 5, 7, 9]
odds is now: [9, 7, 5, 3]

In [12]:
odds = [1,3,5,7]
primes = odds
primes.append(2)
print primes
print odds


[1, 3, 5, 7, 2]
[1, 3, 5, 7, 2]

In [15]:
odds = [1,3,5,7]
#primes = odds[0:4]
primes = odds[:]
primes.append(2)
print primes
print odds


[1, 3, 5, 7, 2]
[1, 3, 5, 7]

In [17]:
mystring = 'hello'
mylist = list(mystring)
print mylist
print type(mylist)


['h', 'e', 'l', 'l', 'o']
<type 'list'>

Typecasting


In [27]:
num = 53
num_str = str(num)
num = num + 5
num_str = num_str + '5'
print num, num_str
num_float = float(num)
print num_float
num_list = [num]
print num_list


58 535
58.0
[58]

In [26]:
float(5) / 2


Out[26]:
2.5

In [37]:
odds = [1,3,5,7,9,11]
# this gives you nothing
reversed_odds = odds.reverse()
print reversed_odds
odds = [1,3,5,7,9,11]
reversed_odds = odds[::-1]
print reversed_odds
print odds[1::2]
print range(1,12,2)
print range(11,0,-2)


None
[11, 9, 7, 5, 3, 1]
[3, 7, 11]
[1, 3, 5, 7, 9, 11]
[11, 9, 7, 5, 3, 1]

In [47]:
odds = range(11,0,-2)
num = odds[0]
#odds.append(num)
#odds = odds + [num]
odds += [num]
del odds[0]
print odds


[9, 7, 5, 3, 1, 11]

In [49]:
odds = range(11, 0, -2)
#num = odds.pop(0)
#odds.append(num)
odds.append(odds.pop(0))
print odds


[9, 7, 5, 3, 1, 11]

In [ ]: