Skip to content

Instantly share code, notes, and snippets.

## Checking the standard deviation of above list
## Here printing upto 3 decimal places.
print('%.3f' %st.stdev(l))
#[Output]:
#32.496
## Checking the variance.
print('%.3f' %st.variance(l))
l1 = [1,2,3,4]
print(l1)
#[Output]:
[1, 2, 3, 4]
print(l1[0])
print(l1[1])
#[Output]:
## Lists are heterogenous in nature.
l3 = ['a' ,1, 'hello']
print(l3)
#[Output]:
#['a', 1, 'hello']
## Lists are mutable in nature.
l2 = l1
## len() method
## Checking previously defined l3.
print(len(l3))
#[Output]:
3
## in operator
if 'a' in l3:
print('Yes, this element is in the list')
## Checking the index of '1'
l3.index(1)
#[Output]:
#1
## Here we start searching beyond the index in which '1' lies so it throws ValueError
l3.index(1,2)
#[Output]:
## Append() method
## It added one more new element '7' in the last.
l4.append(7)
print(l4)
#[Output]:
[1, 2, 3, 2, 3, 4, 5, 6, 7]
## We can also add sublist as an element in the original list as we saw earlier, list is ## heterogenpus in nature.
l4.append([1,2])
## pop() method
l4.pop(5)
print(l4)
#[Output]:
#[1, 2, 3, 2, 3, 4, 5, 6, 7, [1, 2], 'a', 1, 'hello']
l4.pop()
print(l4)
## sort() method
l5 = [4,5,1,2,8,9,1]
## It will sort the list in ascending order.
l5.sort()
print(l5)
#[Output]:
#[1, 1, 2, 4, 5, 8, 9]
l5 = [4,5,1,2,8,9,1]
import csv
with open('student_details.csv' , 'r') as csv_file1:
file1 = csv.reader(csv_file1)
## It will print the csv reader object.
print(file1)
## It will print each line as a list
for line in file1:
print(line)
with open('student_details.csv' , 'r') as csv_file1:
## Reading csv file
file1 = csv.reader(csv_file1)
## Now we are creating new file to write a content.
## If this file doesn't exist already then it will create on its own.
## Opening in write mode.
with open('student_details2.csv' , 'w') as csv_file2:
## Using a 't' delimiter to store values.
file2 = csv.writer(csv_file2, delimiter='-')
## Reading line from file1 and then writing in a new created csv file.