23 March 2022
If you are using PI in python, use this:
import math
In the program, use math.pi
24 March 2022
If there is a problem where the input() or print() is not working in spite of the code running fine, check the indentation.
Use this for statistics and probability puzzles: https://brainstellar.com/
Check Codibility (MS uses) and Cocubes (JPMC uses) too
25 March 2022
To add more zeros after the decimal places, use format():
Example:
print(format(200,'.2f')
Replace the 2 in 2f with the number of zeros you need. For example, if you need 5 zeros, use.5f.
1-May-2022
n = int(input())
arr = list(map(int, input().split()))
for i in range(len(arr)):
print(i, type(i))
Here the type(i) is an integer
Output:
5
2 3 6 6 5
0 <class 'int'>
1 <class 'int'>
2 <class 'int'>
3 <class 'int'>
4 <class 'int'>
-------------------XXXXXX-----------------------------
for i in enumerate(arr):
print(i, type(i))
Here the type(i) is a tuple
Output:
5
2 3 6 6 5
(0, 2) <class 'tuple'>
(1, 3) <class 'tuple'>
(2, 6) <class 'tuple'>
(3, 6) <class 'tuple'>
(4, 5) <class 'tuple'>
String operations
Use split() function to split a string to list
Use join function to join the list into a string
def split_and_join(line):
listline = line.split(" ")
newline = "-".join(listline)
return newline
if __name__ == '__main__':
line = input()
result = split_and_join(line)
print(result)
##########################
#Create Dictionary:
d = {}
d = dict()
#With elements:
csk = {'MS Dhoni':[40,'India',4000], 'Jadeja':[34,'India',2500], 'Bravo':[39, 'West Indies',2300]}
print(csk)
print(type(csk))
#Another way to create dictionary:
csk = dict({'MS Dhoni':[40,'India',4000], 'Jadeja':[34,'India',2500], 'Bravo':[39, 'West Indies',2300]})
print(csk)
print(type(csk))
#Another way:
#csk = ([('MS Dhoni':[40,'India',4000]),('Jadeja':[34,'India',2500]),('Bravo':[39, 'West Indies',2300])])
#Accessing elements
csk['MS Dhoni']
csk.get('MS Dhoni')
#Adding elements: new entry
csk['Bravo'] = [39, 'WI', 2000]
#another way to add element
csk.update({'Shivam Dube':[26,'India',1400]})
csk.update({'a':20})
#Update the element:
csk['MS Dhoni'][2] += 50
print(csk)
# Delete element
csk.pop('a')
print(csk)
#Viewing
#Get all keys
csk.keys()
csk.values()
csk.items()
#Iterate a dictionary:
for k,v in csk.items():
print(k,":",v)
#Nested dictionary
ipl = {
'csk':{'MS Dhoni': [40, 'India', 4000], 'Jadeja': [34, 'India', 2500], 'Bravo': [39, 'West Indies', 2300]},
'rr':{'samson': [28,'India',3000]},
'srh':{'kane':[32,'NewZealand',2500]}
}
print(ipl)
print(ipl['csk'])
print(ipl['csk']['MS Dhoni'])
print(ipl['csk']['MS Dhoni'][2])
#SETS
# unordered
# contains unique elements
#create sets
s = set()
print(type(s))
#with elements
s = {1,2,3,4,5}
#accessing elements
for i in s:
print(i)
#adding elements
s.add(6)
s.update({9, 10}) # you can use any datatype like list or tuple or dictionary
#Delete elements from set
s.discard(10) #doesnt display error if the element being removed doesnt exist
s.remove(5) #displays error if the element being removed doesnt exist
#Union
a = {1,2,3,4,5}
b = {5,6,7,8}
a.union(b) # or a|b
#displays {1,2,3,4,5,6,7,8}
#Intersection
a.intersection(b) #or a&b
#Difference
a.difference(b) # or a-b
#displays elements of A only minus intersection
#symmetric difference
a.symmetric_difference(b) #a^b
#intersection doesnt appear .. only elements of a and b