You are on page 1of 5

Lists, tuples, sets, dictionaries

1 – Show the result using example of Formatting Output.


Numbers to check: 987.456123 | 4512.789032145

There are multiple ways to format output in python. The old way is to use the
string modulo %, and the new way is with a format method function.
Input:
number= 1234.56789
# format EXAMPLE
print("number = %.1f"% (number))
print("number = %.2f"% (number))
print("number =",format(number,'.1f'))
print("number =",format(number,'8.1f'))
print("number =",format(number,'8.2f'))
print("number =",format(number,'8.3f'))

Output:

2 - Execute the following code and show the result.


Input:
numLaptops = 7
laptopCost = 599.50
price = numLaptops* laptopCost
print("Total cost of laptops: $", price)

3 – Replace the last line of code with the following:

print("Total cost of laptops: $%.2f" % price)


What changed? Show the result.
3.1 – Replace the last line of code with the following:
print("Total cost of laptops: $", format(price,'.2f'))
What changed? Show the result.

4 – Execute the following code and show the result.


number = 15
if number > 10:
# Calculate square
print('result: ', number * number)

4.1 – Use if-else statements solve the following task.


Expected output:
PYTHON LIST is an ordered sequence of items.

The list can be created using either the list constructor or using square brackets [].

 Using list() constructor: In general, the constructor of a class has its class
name. Similarly, Create a list by passing the comma-separated values inside
the list().
 Using square bracket ([]): In this method, we can create a list simply by
enclosing the items inside the square brackets.

Example:

# Using list constructor

my_list1 = list((1, 2, 3))

print(my_list1)

# Output [1, 2, 3]

# Using square brackets[]

my_list2 = [1, 2, 3]

print(my_list2)

# Output [1, 2, 3]

# with heterogeneous items

my_list3 = [1.0, 'Jessa', 3]

print(my_list3)
# Output [1.0, 'Jessa', 3]

# empty list using list()

my_list4 = list()

print(my_list4)

# Output []

# empty list using []

my_list5 = []

print(my_list4)

# Output []

Length of a List
In order to find the number of items present in a list, we can use the len() function.
my_list = [1, 2, 3, 4, 5, 10, 15]
print(len(my_list))
# output ?? Show the result

Example:
my_list = [100, 40, 'Intro', 3.14, 'ITP 2']
# accessing 2nd element of the list
print(my_list[1]) # 20
# accessing 5th element of the list
print(my_list[4]) # 'Emma'

As seen in the above example we accessed the second element in the list by
passing the index value as 1. Similarly, we passed index 4 to access the 5th
element in the list.
5 - Reverse a list

Input:

2, programming, to, Introduction

Example (Solution 1: list function reverse()


list1 = [1, 2, 3, 4, 5]
list1.reverse()
print(list1)
Solution 2: Using negative slicing
list1 = [5, 4, 3, 2, 1]
list1 = list1[::-1]
print(list1)

6 - Concatenate two lists index-wise


Use the zip() function. This function takes two or more iterables (like list, dict,
string), aggregates them in a tuple, and returns it.

Example:
list2 = ["py", "w", "ar", "24"]
list3 = [i + j for i, j in zip(list1, list2)]

Expected output:

7 – Solve and show the result


Input:
numbers = [2, 4, 8, 10, 12]

Output:

You might also like