Skip to content

Instantly share code, notes, and snippets.

View Chap2Q5.py
l = ['Welcome','to','ComputerTutor','Python','Revision']
m = 0
w = None
for i in range(len(l)):
if len(l[i])>m:
word=l[i]
m=len(l[i])
print('Longest word present in list is:', word)
View Chap2Q4.py
l = input('Enter a List: ').split() # input().split() returns the list
l = l[len(l)-1:] + l[:-1] # last element being added at
#the start with the list slice that doesn’t contain the last element
print('Rotated list is::',l)
View Chap2Q3.py
s = input('Enter a sentence : ')
number_of_words = 1
number_of_characters = len(s)
al_num = 0
for i in s:
if i.isalnum():
al_num += 1
if i == ' ': # there is a space means there is another word
number_of_words += 1
print('Number of words are', number_of_words)
View Chap2Q3.py
s = input('Enter a sentence : ')
number_of_words = 1
number_of_characters = len(s)
al_num = 0
for i in s:
if i.isalnum():
al_num += 1
if i == ' ': # there is a space means there is another word
number_of_words += 1
print('Number of words are', number_of_words)
View Chap2Q2.py
L = list(map(int, input('Enter list of integers : ').split()))
M = list(map(int, input('Enter list of integers : ').split()))
if len(L) == len(M):
N = [L[i]+M[i] for i in range(len(L))]
print(N)
View Chap2Q1.py
number = input("Enter Phone number: ")
if len(number)==12:
if number[3]=='-' and number[7]=='-':
if(number[:3]+number[4:7]+number[8:]).isdigit():
print('Phone number is valid')
else :
print("Phone number is invalid")
View Chap5Q2.py
def ath(f1,f2):
l=f1.readlines()
for line in l:
if 'Athlectics' in line:
f2.write(line)
f2.write('\n')
f1 = open('/content/Athletics_data.txt', 'r')
f2 = open('/content/Sports_data.txt', 'w')
ath(f1,f2)
View Chap5Q3.py
f=open('/content/Contacts.csv','r')
for line in f:
x=line.split()
print(x)
View Chap5Q4.py
f = open('Poem.txt', 'r')
c_to=0
c_the=0
for line in f:
c_to += line.count('to')
c_the += line.count('the')
print(c_to)
print(c_the)
View Chap5Q5.py
f = open('Article.txt','r')
c_up=0
ch = f.read(1)
while ch:
ch=f.read(1)
if ch.isupper():
c_up+=1
print("Number of Uppercase alphabets is/are:",c_up)