...
Code Block |
---|
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"] |
Tuple
Info |
---|
The tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. |
Code Block |
---|
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d"; |
Set
Info |
---|
Unordered collections of unique elements |
Code Block |
---|
Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
Set(['Janice', 'Jack', 'Sam'])
Set(['Jane', 'Zack', 'Jack'])
Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack']) |
Dictionary
Info |
---|
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. |
Code Block |
---|
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} |
Get last name from full name by split()
The function can be easily implemented by string method
Code Block |
---|
actor = {"name": "John Cleese", "rank": "awesome"}
def get_last_name():
return actor["name"].split()[1]
get_last_name()
print("All exceptions caught! Good job!")
print("The actor's last name is %s" % get_last_name()) |
Split string as list
Code Block |
---|
sentence = "the quick brown fox jumps over the lazy dog"
words = sentence.split()
print(words) |
Filter positive numbers only - 1
Code Block |
---|
numbers = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]
newlist = []
for number in numbers:
if number>0:
newlist.append(number)
print(newlist) |
Filter positive numbers only - 2
Code Block |
---|
numbers = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]
newlist = [int(x) for x in numbers if x > 0]
print(newlist) |
Create word list from a sentence with no duplicate entries
set() removes all the duplicate entries in the array
Code Block |
---|
strings = "my name is Chun Kang and Chun is my name"
r = set(strings.split())
print(r) |
...
Split string as list
Code Block |
---|
sentence = "the quick brown fox jumps over the lazy dog"
words = sentence.split()
print(words) |
Filter positive numbers only - 1
Code Block |
---|
numbers = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]
newlist = []
for number in numbers:
if number>0:
newlist.append(number)
print(newlist) |
Filter positive numbers only - 2
Code Block |
---|
numbers = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]
newlist = [int(x) for x in numbers if x > 0]
print(newlist) |
Create word list from a sentence with no duplicate entries
set() removes all the duplicate entries in the array
Code Block |
---|
strings = "my name is Chun Kang and Chun is my name"
r = set(strings.split())
print(r) |
Tuple
Info |
---|
The tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. |
Code Block |
---|
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d"; |
Set
Info |
---|
Unordered collections of unique elements |
Code Block |
---|
Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
Set(['Janice', 'Jack', 'Sam'])
Set(['Jane', 'Zack', 'Jack'])
Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack']) |
Find overlapped entries from two arrays
Code Block |
---|
a = set([ "Seoul", "Pusan", "Incheon", "Mokpo" ])
b = set([ "Seoul", "Incheon", "Suwon", "Daejeon", "Gwangjoo", "Taeku"])
print(a.intersection(b))
print(b.intersection(a)) |
The result will be like below
Result |
---|
{'Seoul', 'Incheon'} {'Seoul', 'Incheon'} |
Find different elements from two arrays based on "symmetric_difference" method
Code Block |
---|
a = set([ "Seoul", "Pusan"Jake", "IncheonJohn", "MokpoEric" ])
b = set([ "SeoulJohn", "Incheon", "Suwon", "Daejeon", "Gwangjoo", "Taeku"Jill"])
print(a.intersectionsymmetric_difference(b))
print(b.intersectionsymmetric_difference(a)) |
The result will be like below
Find different elements from two arrays based on "
...
difference" method
Code Block |
---|
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
print(a.symmetric_difference(b))
print(b.symmetric_difference(a)) |
The result will be like below
Find different elements from two arrays based on "
...
union" method
Code Block |
---|
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
print(a.differenceunion(b))
print(b.difference(a)) |
The result will be like below
...
Print out a set containing all the participants from event A which did not attend event B
Code Block |
---|
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
print(a.union(b)) |
The result will be like below
...
{'John', 'Eric', 'Jake', 'Jill'}
(set(a).difference(set(b))) |
Dictionary
Info |
---|
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. |
Code Block |
---|
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} |
Get last name from full name by split()
The function can be easily implemented by string method
Code Block |
---|
actor = {"name": "John Cleese", "rank": "awesome"}
def get_last_name():
return actor["name"].split()[1]
get_last_name()
print("All exceptions caught! Good job!")
print("The actor's last name is %s" % get_last_name( |
Print out a set containing all the participants from event A which did not attend event B
Code Block |
---|
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
print(set(a).difference(set(b))) |
Generator
Random number generation
...