A shop has a list of items where each item is marked by its price. And he has to to computational analysis on those lists of items. For now suppose there are 4 items each marked with a certain price.

item_list = [

{“name”: “item1”, “price”: 10 },

{“name”: “item2”, “price”: 5 },

{“name”: “item3”, “price”: 7 },

{“name”: “item4”, “price”: 12 }

]

Question: Find the total price one will get if all the items in item list are sold.

Solution:

Algorithm:

  1. Initialise a variable and assign it a value zero
  2. For each dictionary in the list

2.1 Add price to the variable sum

  1. Print the result of the variable sum

Code:

item_list = [

{“name”: “item1”, “price”: 10 },

{“name”: “item2”, “price”: 5 },

{“name”: “item3”, “price”: 7 },

{“name”: “item4”, “price”: 12 }

]

sum = 0

for item in item_list:

sum = sum +item[‘price’]

Here sum = sum + item[‘price’] can also written as : sum+= item[‘price’].

Expression
Description
a += b
a = a + b ; this is used to add a and b and assign it to a
a -= b
a = a – b ; this is used to subtract a and b and assign it to a
a *= b
a = a * b ; this is used to multiply a and b and assign it to a
a /= b
a = a / b ; this is used to divide a and b and assign it to a
a %= b
a = a % b ; this is used to perform a modulo b and assign it to a
a **= b
a = a ** b ; this is used to perform a exponent b and assign it to a
a //= b
a = a // b ; this is used to perform floor division between a and b and assign it to a

Now a shop has a list of items where each item is marked by its price and number of products. And shopkeeper has to to computational analysis on those lists of items. For now suppose there are 4 items each marked with a certain price and number of items.

Recommended Post   Python Fundamentals

Question: Find the total price of the number of items sold in order to find the total revenue of the shopkeeper.

Solution:

item_list = [

{“name”: “item1”, “price”: 10,”quantity”: 12},

{“name”: “item2”, “price”: 5 ,”quantity”: 12},

{“name”: “item3”, “price”: 7 ,”quantity”: 12},

{“name”: “item4”, “price”: 12 ,”quantity”: 12}

]

Solution:

Algorithm:

  1. Initialise a variable and assign it a value zero
  2. For each dictionary in the list

2.1 Add price*quantity to the variable sum

  1. Print the result of the variable sum

Code:

sum = 0

for item in item_list:

sum +=item[‘price’] * item[‘quantity’]

TRY IT OUT: To check how much understanding you have of If/Else clause , Let’s find the sum of price of only ‘item1’ and ‘item2’ in whole item list.

Algorithm:

  1. Initialise a variable and assign it a value zero
  2. For each dictionary in the list

2.1 if item ==’item1’ or item==’item2’

2.1.1 Add price*quantity to the variable sum

  1. Print the result of the variable sum

Code:

item_list = [

{“name”: “item1”, “price”: 10,”quantity”: 12},

{“name”: “item2”, “price”: 5 ,”quantity”: 12},

{“name”: “item3”, “price”: 7 ,”quantity”: 12},

{“name”: “item4”, “price”: 12 ,”quantity”: 12}

]

sum = 0

for item in item_list:

if item[‘name’]==’item1′ or item[‘name’]==’item2′:

sum +=item[‘price’] * item[‘quantity’]