Practice Code

#Data Structure - List

#Declare

list = [1, 3, 5, 7, 9]

print(list);

print("\n\n");


#Check length of list

print(len(list));

print("\n\n");


#Index of list

print(list[4]);

print("\n\n");


#Negative index of list

print(list[-1]);

print("\n\n");


#Add data

list.append(11);

print(list);

print("\n\n");


#Delete specific data

list.remove(5);

print(list);

print("\n\n");


#Delete specific position

list.pop(0);

print(list);

print("\n\n");


#Sublist

print(list[0:3]);

print(list[-2:]);

print("\n\n");


#CopyList

print("List Copy Example.");

ori_list = [1, 3, 5, 7]

new_list = ori_list;

new_list.append(9);


print("original list : ", ori_list);

print("new list : ", new_list);

cpy_list = ori_list[:];

cpy_list.append(11);

print("original list : ", ori_list);

print("cpy list : ", cpy_list);

print("\n\n");


#Concatenate list

listA = [1, 2, 3];

listB = [4, 5, 6];

listC = listA + listB;

print(listC);

print(id(listA), id(listB), id(listC));

print("\n\n");


#extend list

print(id(listA), listA);

listA.extend(listB);

print(id(listA), listA);

print("\n\n");


#del() function

print(listA);

del listA[0];

print(listA);

del listA[-2:];

print(listA);

del listA;


Result Screen

[1, 3, 5, 7, 9]


5


9


9


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


[1, 3, 7, 9, 11]


[3, 7, 9, 11]


[3, 7, 9]

[9, 11]


List Copy Example.

original list :  [1, 3, 5, 7, 9]

new list :  [1, 3, 5, 7, 9]

original list :  [1, 3, 5, 7, 9]

cpy list :  [1, 3, 5, 7, 9, 11]


[1, 2, 3, 4, 5, 6]

56151576 56151416 56182264


56151576 [1, 2, 3]

56151576 [1, 2, 3, 4, 5, 6]


[1, 2, 3, 4, 5, 6]

[2, 3, 4, 5, 6]

[2, 3, 4]

반응형

'Programming > Python' 카테고리의 다른 글

[Python] print() 함수  (0) 2017.06.07
[Python] Data Structure - Tuple  (0) 2017.03.31
[Python] List - Negative index  (0) 2017.03.31
[Python] 따옴표 출력하기.  (0) 2017.03.31
[Python] NULL  (0) 2017.03.31

+ Recent posts