top of page
Search

# Python Programming Practice Examples | Get Help In Python Programming | Realcode4you

Program 1.

Generate a list containing 50 random integers between 10 to 100. Write a program which removes all the values which are divisible by 3 from the list . Then it subtracts 1 from all the remaining values and then removes all the values divisible by 5 from the list.

Example with a smaller list of random integers :

starting list with random integers = [23,34,45,56,17,18,23,10]

after removing all the values divisible by 3 = [23,34,56,17,23,10]

subtracting 1 from each of the value = [22,33,55,16,22,9]

final output after removing values divisible by 5 = [22,33,16,22,9]

Way to create a list with random integers

```import numpy as np
x=np.random.randint(10,101,50)```

since these values are being generated randomly , your solution will not match exactly with the solution that anyone else comes up with [in terms of values that you see as outcome]

Program 2.

Consider the following dictionary

`{'a':'e','b':'\$','c':'q','d':'p','j':'n'}`

Write a program which takes input any string and then replaces all the letters which are keys in the above dictionary with their corresponding values in the dictionary.

```example input = 'abjected'
output = 'e\$neqtep'```

Program 3.

Write a program which takes input a list of strings and removes strings until all the remaining strings are of equal lengths . Assume that in the input list , there will be at-least two strings of the same length . For simplicity , assume that list will not contain equal length strings for multiple lengths.

`example input =['abc','abcd','acbde','acbgeh','qwert'] `

output = ['abcde','qwert']

you can identify for what length you have multiple strings by first creating a list containing lengths of the string and then look at count of the lengths in that list . any value which has count more than 1 , thats your target length.

From the input list you can remove all other strings and keep only that string which correspond to that length .

=> you can get unique values in a list x by using set(x)

=> you can get count of a value , say `y` in list `x` using `x.count(y) `

Program 4.

Write a function which tells you whether you can take jugs of capacity 3 and 5 litres and fill another jug of capacity x completely with water using just these two . For simplicity assume that you can only pour into the jug of capacity x . You can not take any water out. And you start with jug of capacity x being empty. It is acceptable if you dont use any one of the jugs [of capacity 3 and 5 litres]

=> function will take argument x , the capacity of the jug to be filled

=> The problem is same as finding if there exist positive integer solutions for a and b in the equation 3a+5b=x where is a known integer

=>one of the strategies to find this out can be as per following example [dont be intimidated by the example, i have given multiple elaborate examples to explain possible scenarios to you ]

=> this does not imply that this is the only possible way to solve this , feel free to use your own ideas if you have one

=> you can check if a number x is integer or not using x==int(x)

Program 5.

Use dictionary comprehension to create a dictionary with keys : t1,t2,t3.......t100 which takes random integers from the range [1,100] as their corresponding values. Write a function which takes this dictionary as input and returns number of unique values from all key:value pairs in that dictionary .

`example input with a smaller dictionary : {'t1':2, 't2':45, 't3':34, 't4':2} `

output = 3

Program 6.

Imagine a circle and two squares: a smaller and a bigger one. For the smaller one, the circle is a circumcircle and for the bigger one, an incircle.

Create a function, that takes an integer (radius of the circle) and returns the difference of the areas of the two squares

Program 7.

A number is said to be Harshad if it's exactly divisible by the sum of its digits. Create a function that determines whether a number is a Harshad or not. Input to the function will be an integer.

Program 8

Write a function which takes an integer n as input and returns sum of all even numbers in the first n terms of a fibonacci series .

```example input = 7
fibonacci series for the same will be : 1,1,2,3,5,8,13 ```

output=2+8=10

Additional info : Each term in the fibonacci series is sum of previous two. First two terms of the series are 1 . For simplicity , you can assume that input n will always be greater than 4.

Program 9

Write a function which takes input an english sentence of any length , and a set containing some stop words. Function then removes all stop words from the sentence and returns a dictionary where keys are all unique words in the sentence and values are their count in the sentence .

Additional info : For simplicity assume that there will be no punctuations in the input sentence, simply words separated by single white spaces. Assume that all string inputs will be lowercase.

Program 10

10. consider this list of addresses

['H-73, MDT, Powai , Mumbai' , '1604, SS, Hyderabad' ,'B block 73, Adyar, Chennai'] Extract cities from each address, use list comprehension .

Hints :

• Write list comprehension to convert this to list of lists [ use split function on strings]

• Extract last element of each of these individual list using index -1, using list comprehension

Program 11

11. given a list , create a dictionary with counts of unique elements that it contains input list = ['a','b','a','a','b','c','c','a','b'] output dictionary = {'a':4,'b':3,'c':2}

Hints :

• iterate over the list using a for loop

• for creating an empty dictionary : dict_name={}

• for adding a new element to dictionary: dict_name['element']=value

• for getting keys of dictionary : dict_name.keys()

• for checking if an element is in the dictionary : element in dict_name.keys()

• for incrementing the value assigned to an element in a dictionary : dict_name['element']+=1