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), type(different), type(different)) # <class 'str'> <class 'int'> <class 'float'>
In this code snippet, the first 3 elements of the
differentlist are string, int, and float.
Lists can contain other types like boolean values, floating-point values, and even other lists (nested lists).
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 will 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.
10 first 20 second 7 third -3 hello
34 first second third hello
Hint: Writing the output with an
f-stringmight be very convenient.
Time limit: 1 seconds
Memory limit: 512 MB
Output limit: 1 MB