Different types in a list
Some programming languages don’t allow having different types of variables (like integers and strings) in a single list. Yet, Python is very flexible and allows having several types in a single list:
different = [
'first',
33,
33.4,
'second',
88.9,
'third'
]
print(type(different[0]), type(different[1]), type(different[2]))
# <class 'str'> <class 'int'> <class 'float'>
In this code snippet, the first 3 elements of the
different
list are string, int, and float.Lists can contain other types like boolean values, floating-point values, and even other lists (nested lists).
Challenge
Define a list of 8 values that are read from the input. The even indices would be integers (starting from 0) and the odd ones would be strings.
The output of the program should contain the sum of the numbers on the first line and the strings separated by a space on the second line.
Input | Output |
10
first
20
second
7
third
-3
hello | 34
first second third hello |
Hint: Writing the output with an
f-string
might be very convenient.Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB