Assignment 31: dict

  1. Create a dict object where the first N natural numbers are keys and their squares are data values.

  2. Sort a dictionary by its keys in descending order.

  3. Write a Python script to create a dictionary where keys are cricket player names and values are tuples containing:

    total runs,

    All data should be taken from the user.

  4. Write a Python script to find the maximum-size batch code from a dictionary where keys are batch codes and values are batch sizes.

  5. Write a Python script to create a dict object from a list of city names such that alphabets are keys and the value is the list of city names starting with that alphabet.

{key: values, key1:value1}, key and value together is item

dict(),{}

N = int(input())
natural_num = {i:i**2 for i in range(N)}
9
natural_num
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
dict(sorted(natural_num.items(), reverse=True))
{8: 64, 7: 49, 6: 36, 5: 25, 4: 16, 3: 9, 2: 4, 1: 1, 0: 0}
natural_num.values()
dict_values([0, 1, 4, 9, 16, 25, 36, 49, 64])
natural_num.keys()
dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8])
natural_num.items()
dict_items([(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64)])
players_name = ['A','B','C']
corres_scores = ['10', '0', '100']
{players_name[i]:corres_scores [i]
 for i in range(3)}
{'A': '10', 'B': '0', 'C': '100'}
batches = {'C':10, 'B':11, 'A':100}
max(batches.keys()), batches.keys()
('C', dict_keys(['C', 'B', 'A']))
max(batches.values()), batches.values()
(100, dict_values([10, 11, 100]))
max(batches.items()), batches.items()
(('C', 10), dict_items([('C', 10), ('B', 11), ('A', 100)]))
max(batches, key=batches.get)
'A'
batches['A']
100
batches[max(batches, key=batches.get)]
100
list_of_cities = ['Hyderbad', 'Warangal', 'Kolkata']
{cities:cities[0] for cities in list_of_cities}
{'Hyderbad': 'H', 'Warangal': 'W', 'Kolkata': 'K'}

Assignment 32: Functions-1

  1. Write a Python function to calculate sum of two numbers. (TSRS)

  2. Write a Python function to calculate area of a circle. (TSRS)

  3. Write a Python function to calculate average of three numbers. (TSRS)

  4. Write a Python function to calculate compound interest. (TSRS)

  5. Write a Python function to calculate volume of a cuboid. (TSRS)

adding = lambda a,b: a+b

def adding(a,b):
  return a+b
def areas(r):
  return 3.14*r**2

Assignment 34: Functions-3

  1. Write a Python function to print the first N odd natural numbers. (TSRN)

  2. Write a Python function to print the first N prime numbers. (TSRN)

  3. Write a Python function to print all prime numbers between two given numbers. (TSRN)

  4. Write a Python function to print the first N terms of the Fibonacci series. (TSRN)

  5. Write a Python function to print all factors of a given number. (TSRN)

[1,N] –> prime numbers

N = 2
[n
 for n in range(1,N+1)
 if (n>1 and all(n%i!=0 for i in range(2,n)))]
[2]
a = 10
[n for n in range(1,a) if a%n == 0]
[1, 2, 5]
10%4
2
4%10
4

Assignment 35: Functions-4

  1. Write a Python function to calculate LCM of two numbers. (TSRS)

  2. Write a Python function to count words in a string. (TSRS)

  3. Write a Python function to create a list of prime numbers between two given numbers. (TSRS)

  4. Write a Python function to filter words from a text starting from the same alphabet and store them in a list. Then create a dictionary with alphabets as keys and lists of corresponding words as values. Take text as an argument and return the dictionary. (TSRS)

  5. Write a Python function to find all common factors of two given numbers. Return a tuple of such factors. (TSRS)

10, 2 ,10
10, 5, 10
10, 4 , 20
10, 11, 110
lcm = lambda a,b: ([n for n in range(max(a,b),a*b+1) if n%a ==0 and n%b ==0][0])
lcm(10,2)
10
string = "12 siva 13"
def count_num(string):
  return sum([True for n in string if n.isdigit()])
count_num(string)
4
text = "I am sleepy guy"
first_words = text.split()
first_words
['I', 'am', 'sleepy', 'guy']
first_ch = [words[0] for words in first_words]
first_ch
['I', 'a', 's', 'g']
{first_ch[i]:first_words[i] for i in range(len(first_ch))}
{'I': 'I', 'a': 'am', 's': 'sleepy', 'g': 'guy'}
text = "I am sleepy guy and intelligent guy"
first_words = text.split()
first_words
['I', 'am', 'sleepy', 'guy', 'and', 'intelligent', 'guy']
first_ch = [words[0] for words in first_words]
first_ch
['I', 'a', 's', 'g', 'a', 'i', 'g']
{first_ch[i]:first_words[i] for i in range(len(first_ch))}
{'I': 'I', 'a': 'and', 's': 'sleepy', 'g': 'guy', 'i': 'intelligent'}
first_ch_st = set(first_ch)
first_ch_st, first_ch, first_words
({'I', 'a', 'g', 'i', 's'},
 ['I', 'a', 's', 'g', 'a', 'i', 'g'],
 ['I', 'am', 'sleepy', 'guy', 'and', 'intelligent', 'guy'])
{first_ch_key:[first_words_wd
               for first_words_wd in first_words
               if first_ch_key==first_words_wd[0]]
 for first_ch_key in first_ch_st
 }
{'I': ['I'],
 'g': ['guy', 'guy'],
 'a': ['am', 'and'],
 's': ['sleepy'],
 'i': ['intelligent']}
"siva".startswith("s")
True
{first_ch_key:[first_words_wd
               for first_words_wd in first_words
               if first_words_wd.startswith(first_ch_key)]
 for first_ch_key in first_ch_st
 }
{'I': ['I'],
 'g': ['guy', 'guy'],
 'a': ['am', 'and'],
 's': ['sleepy'],
 'i': ['intelligent']}