Basic Python Concepts

?
# SPLIT THE FOLLOWING STRING INSIDE THE LIST BY WHITE SPACE


list = ["90 50 52 5 75"]
def split_list():
return [word for line in list for word in line.split()]

print(split_list())
1 of 11
# SPLIT THE FOLLOWING STRING INSIDE THE SECOND LIST BY WHITE SPACE

list = [["90 50 52 5 75"], ["44 22 12 9 8"]]
def split_list():
return [word for line in list[1] for word in line.split()]

print(split_list())
2 of 11
# SPLIT THE FOLLOWING STRING INSIDE THE SECOND LIST ITEM BY WHITE SPACE

list = ["90 50 52 5 75", "55 80 99 20 60"]
def split_list():
return [word for line in list[1:2] for word in line.split()]

print(split_list())
3 of 11
# RETURN A LIST OF STRING NUMBERS AS A LIST OF INTEGERS

my_list = ["90", "50", "52", "5", "75"]
def split_list():
return list(map(int, my_list))

print(split_list())
4 of 11
# RETURN THE SECOND LIST OF STRING NUMBERS AS A LIST OF INTEGERS

my_list = [["90", "50", "52", "5", "75"],["45", "25", "19", "4", "12"]]
def split_list():
return list(map(int, my_list[1]))

print(split_list())
5 of 11
# COUNT THE ELEMENTS IN THE 2ND LIST

my_list = [["90", "50", "52", "5", "75"],["45", "25", "19", "4", "12", "14"]]
def split_list():
return len(my_list[1])

print(split_list())
6 of 11
# JOIN THE TWO LISTS TOGETHER

my_list = ["90", "50", "52", "5", "75"]
my_list1= ["45", "25", "19", "4", "12", "14"]
def join_lists():
return my_list + my_list1

print(join_lists())
7 of 11
REPLACE THE SPACES IN THE STRING BELOW WITH "-"

string = "40 50 60 80 90"
def replace_dots(string):
return string.replace(" ","-")

print(replace_dots(string))
8 of 11
# JOIN THE FOLLOWING LIST TOGETHER AS A SINGLE STRING

my_list = ["Hello", "world"]
def join_lists():
return " ".join(my_list)

print(join_lists())
9 of 11
# ADD "20" TO THE LIST

my_list = ["9", "8"]
def add_item():
return my_list + ["20"]


print(add_item())
10 of 11
# ADD "20" TO THE LIST USING "APPEND"

my_list = ["9", "8"]
def add_item():
trip= "20"
my_list.append(trip)
return my_list
print(add_item())
OR
def add_item():
my_list.append("20")
return my_list
11 of 11

Other cards in this set

Card 2

Front

# SPLIT THE FOLLOWING STRING INSIDE THE SECOND LIST BY WHITE SPACE

list = [["90 50 52 5 75"], ["44 22 12 9 8"]]

Back

def split_list():
return [word for line in list[1] for word in line.split()]

print(split_list())

Card 3

Front

# SPLIT THE FOLLOWING STRING INSIDE THE SECOND LIST ITEM BY WHITE SPACE

list = ["90 50 52 5 75", "55 80 99 20 60"]

Back

Preview of the front of card 3

Card 4

Front

# RETURN A LIST OF STRING NUMBERS AS A LIST OF INTEGERS

my_list = ["90", "50", "52", "5", "75"]

Back

Preview of the front of card 4

Card 5

Front

# RETURN THE SECOND LIST OF STRING NUMBERS AS A LIST OF INTEGERS

my_list = [["90", "50", "52", "5", "75"],["45", "25", "19", "4", "12"]]

Back

Preview of the front of card 5
View more cards

Comments

No comments have yet been made

Similar Computing resources:

See all Computing resources »See all Python resources »