Skip to content

Instantly share code, notes, and snippets.

@ajtazer
Created March 31, 2021 16:52
Show Gist options
  • Save ajtazer/635c24fcc996f6fde7e47d9af3955d14 to your computer and use it in GitHub Desktop.
Save ajtazer/635c24fcc996f6fde7e47d9af3955d14 to your computer and use it in GitHub Desktop.
Python Class 12 Computer Science Important Practicals Programs (CBSE and Other Boards) P.S. You Ask for Other Programs Too :-)
#Opening with 'WITH' method
with open('boi.txt','w') as f:
#Write mode can create file
#As long as code is written in this block
#There is no need to Close File
#File will be automatically closed for
#code out of this block.
f.writelines(['This Function\n',
'Takes Argument or Input\n',
'in List Like This'])
#Now Text Is Written in File Next code should be out of this
#Block to close file.
#Opening File again to read with
#"Primitive File Open Statement"
f=open('boi.txt','r')
#Read Mode Cant Create File
#Opened as 'f' variable
file=f.read()
#Storing Data read on Variable 'file'
print(file)
f.close()
#Closed File on Variable 'f'
#1 Read()-> reads whole file as string
#2 Readline()-> reads a line as string
#3 Readlines()-> reads whole file with wast lines as elements of list.
#Author: Tazer
stack=[] #Defining Empty Stack
while True: # For a never ending loop
print('''
Enter the Number from below:-
1)Add Value to Stack
2)Remove Value From Stack
3)Display Stack
Enter to Exit
''')
x=input('==>')
if x=='':
break
elif x=='1':
value=input('Enter Value to ADD:')
stack.append(value)
print(value,'added to stack Successfully')
elif x=='2':
z=input('1)Remove by INDEX Value\n 2)Remove by Element Value\n ==>')
if z=='1':
index=int(input('Enter INDEX:'))
print('Element Removed=',stack.pop(index))
elif z=='2':
element=input('Enter ELEMENT:')
stack.remove(element)
else:
print('Some Error Occured.')
elif x=='3':
print('Stack:',stack)
else:
print('Enter Only From the Given Options')
#Author: Tazer
def Counter(file_path,word):
count=0
with open(file_path,'r') as f:
for line in f:
list_of_words=line.split()
for text in list_of_words:
if text==word:
count+=1
return count
print('Number of Times "to" occured:',Counter('SampleText.txt','to'))
print('Number of Times "the" occured:',Counter('SampleText.txt','the'))
#Author: Tazer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment