Exercise 1
x = (1, 2, (3, 'John', 4), 'Hi')

x[-1][-1]=‘i’

x[0:1]=(1,)

Exercise: odd tuples

Write a procedure calledoddTuples, which takes a tuple as input, and returns a new tuple as output, where every other element of the input tuple is copied, starting with the first one. So if test is the tuple('I', 'am', 'a', 'test', 'tuple'), then evaluating

oddTupleson this input would return the tuple('I', 'a', 'tuple')

def oddTuples(aTup):
    '''
    aTup: a tuple

    returns: tuple, every other element of aTup. 
    '''
    # Your Code Here
    a_Tup = ()
    for i in range(len(aTup)):
        if i%2 == 0:
            a_Tup = a_Tup + (aTup[i],)
    return a_Tup

tuple 不可以用tuple.append(),同时aTup[i]是字母,不是tuple,所以需要(aTup[i],)进行转换。

how to add value to a tuple?
a_Tup = a_Tup + (aTup[i],)
Exercise 4
>>> aList = [0, 1, 2, 3, 4, 5]
>>> bList = aList
>>> aList[2] = 'hello'

# blist is a copy of alist
>>> aList == bList
>>> aList is bList
Exercise 5

Python 2 vs Python 3

There were both syntactic changes between Python 2 and 3 (hence in Python 3 you have to use print(...) with the things to be printed in round brackets, while in Python 2 the brackets are not used), and semantic changes (so -3/2 in Python 2 yields -2 while in Python 3 it yields -1.5)

Should you try to continue using a Python 2.7 interpreter, I suspect you'll be on a sinking ship!!!

results matching ""

    No results matching ""