Skip to content

Instantly share code, notes, and snippets.

print('My module is working!')
def print_name(l):
for e in l:
print(e)
## importing the custom module
import my_module
#[Output]:
My module is working!
## Importing system library to check where it will search for modules either standard or
## custom.
import sys
l = ['a','b','c']
## with simply open() method, we are trying to open the file and modify it.
## Created one file already in my cwd. If you want to fetch from other directory, just ## pass the path
file = open('file1.txt','r')
## It will print the file object.
print(file)
#[Output]:
<_io.TextIOWrapper name='file1.txt' mode='r' encoding='cp1252'>
## Here opening the file with "with" keyword and it will automatically close the file after before coming out of the with.
with open('file1.txt','r') as f:
print(f.read())
#[Output]:
1. "My name is XYZ".
2. "Currently I am persuing Bachelor's of Technology".
3. "I have done several projects on Software Development".
## Checking if file is closed.
with open('file1.txt','r') as f:
## It will print each line and make the pointer to point at next line.
print(f.readline())
## It is telling the current position of the pointer
print(f.tell())
## Using seek we can modify the current pointer by passing the position as paramter in seek.
f.seek(0)
## Checking if pointer comes to 0 or not.
print(f.tell())
## Printing out the line again to check what will it prints.
## If file is not created then we can also create it like that.
## It will automatically generate new file in the current directory.
with open('file2.txt','w') as f1:
pass
#If we will go to our file then it will be empty right now.
## Writing to the created file.
with open('file2.txt','w') as f1:
f1.write("Hello")
## Assking for inputing and cursor will stop you have to enter some value
name = input('what is your name? ')
## After entering this particular statement will run.
print("hello" , name )
#[Output]:
#what is your name? branson
#hello branson
## Here t will be considered and it will add a tab on its own.
## Simply using print
name = input('what is your name?t')
print(name)
#[Output]:
#what is your name? branson
#branson
## If we want to repeat a string string or number multiple times then we can do like this.
print('number! '*3)
l = ['a' ,'b', 'c']
## It is printing in new line each entry
for e in l:
print(e)
#[Output]:
#a
#b
#c
import statistics as st
l = [2,3,4,1,7,8,2,3,4,5,1,8,5,6,8,2]
## printing the mean of the data.
print('Mean of data is {}'.format(st.mean(l)))
## printing the median of the data.
print('Median of data is {}'.format(st.median(l)))
## Printing the mode of the data, but here it will generate error because here in this ## data two value are most occuring,hence not able to handle.
print('Mode of the data is {}'.format(st.mode(l)))