def square(n):
result = n*n
return result
print(square(7.36))
54.1696
with open("data.txt","r") as f:
contents = f.read()
print(len(contents))
print(contents)
lines = contents.split('\n')
print(lines)
f.close()
45 This is line 1 This is line 2 This is line 3 ['This is line 1', 'This is line 2', 'This is line 3', '']
with open("data.txt","r") as f:
line1 = f.readline()
line2 = f.readline()
print(len(contents))
#print(line1,end='')
print(line2)
f.close()
45 This is line 1 This is line 2
s = "20,30,40"
values = s.split(',')
print(values)
['20', '30', '40']
xs = []
xs.append(10)
xs.append(20)
xs.append(30)
print(xs)
[10, 20, 30]
# Tuple structure
ts = (10,20,30)
print(ts[0])
10
count = [(10,'a'),(5,'b'),(12,'c'),(6,'d')]
print(len(count))
print(count[0])
print(count[0][1])
count.sort(reverse=True)
print(count)
4 (10, 'a') a [(12, 'c'), (10, 'a'), (6, 'd'), (5, 'b')]
names = ["John","Alan","Zack"]
names.sort()
print(names)
names.sort(reverse=True)
print(names)
['Alan', 'John', 'Zack'] ['Zack', 'John', 'Alan']
def candidates(ballots):
result = []
for ballot in ballots:
if ballot not in result:
result.append(ballot)
return result
candidates(["raj","raj","raj","alex"])
['raj', 'alex']
candidates([10,20,10,30,10,20,90,10])
[10, 20, 30, 90]
xs = [10,20,30]
sum = 0
for x in xs:
sum = sum + x
return sum
xs = [10,20,30]
10 in xs
True
10 not in xs
False