Welcome to the Simplilearn Community

Want to join the rest of our members? Sign up right away!

Sign Up

Data Science with Python | Aug 9-Sep 27| Nishant Saraswat

Revathi_17

Administrator
Simplilearn Support
Customer
Hi All,

Welcome to Simplilearn!

Let us have a discussion about the courses under our learning path, explore the course here, and try to resolve all our queries related to the same.

Happy Learning!

Regards,
Team Simplilearn
 
1 write a python program to find the number is even or odd

Solution:

x=int(input("Enter a Number "))

if x%2 == 0:
print(x, " is an Even Number")
else:
print(x,"is an Odd Number")

2 write a python program to check number is vowels or nconsonants

Solution :

inp=input("Enter a character")
if inp.lower() =='a' or inp.lower() == 'e' or inp.lower() == 'i' or inp.lower() == 'o' or inp.lower() == 'u':
print(inp," is a vowel")
else:
print(inp," is a consonant")

3 Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn
Sample value of n is 5
Expected Result : 615

Solution:

n=input("Enter a number")
a = int(n)
b= int(n+n)
c=int(n+n+n)
d=a+b+c
print(d," is n+nn+nnn")


4 write a python program : accept 3 numbers from user and check the greater number

Solution:

a=int(input("Enter a number"))
b=int(input("Enter a number"))
c=int(input("Enter a number"))
if a>b and a>c:
print(a,"is greater than",b,"and",c)
elif b>a and b>c:
print(b,"is greater than",a,"and",c)
elif c>a and c>b:
print(c,"is greater than",a,"and",b)
else :
print("All Number are equal")

5 accept a number from user and check its 1 digit number or 2 digit number or 3 digit or 4 digit and if it is more then 4 digit number print more then 4 digit number like 5,6,7 any

2 => 1 digit
23 => 2 digit
456 => 3 digit
4567 => 4 digit
5678888=> its more then 4 digit

Solution:
inp=input("Enter a string")
x=len(inp)
print(inp,"is",x,"digits")
 

Nikhil C R

Active Member
Questions - Assignment 1

1. Write a python program to find the number is even or odd

a = int(input('Enter the number: '))
if a % 2 == 0 :
print('Number is Even')
else:
print('Nmber is ODD')


2. Write a python program to check number is vowels or nconsonants

letter = input("Enter a letter to check its vowel or consonant : ")
letter = letter.lower()
if (letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u'):
print('Its Vowel')
else:
print('Its a consonant')


3. Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn

n = input("Enter the number to compute (n+nn+nnn): ")
x = int(n)
xx = int(n+n)
xxx = int(n+n+n)
compute_num = x+xx+xxx
print(compute_num)


4. Write a python program : accept 3 numbers from user and check the greater number

print('Enter the 3 numbers')
n1 = int(input('enter the first number: '))
n2 = int(input('enter the second number: '))
n3 = int(input('enter the thrid number: '))
if (n1> n2) and (n1>n3):
print('n1 is greatest number')
elif (n2> n1) and (n2>n3):
print('n2 is greatest number')
else:
print('n3 is greatest number')


5. Accept a number from user and check its 1 digit number or 2 digit number or 3 digit or 4 digit and if it is more then 4 digit number print more then 4 digit number like 5,6,7 any

number = input('Enter the number to check digits: ')
digit = len(number)
if digit < 4:
print('digit', digit)
else:
print('Its more than 4 digit')
 

nishi4ca

Active Member
Alumni
Questions - Assignment 1

1. Write a python program to find the number is even or odd

a = int(input('Enter the number: '))
if a % 2 == 0 :
print('Number is Even')
else:
print('Nmber is ODD')


2. Write a python program to check number is vowels or nconsonants

letter = input("Enter a letter to check its vowel or consonant : ")
letter = letter.lower()
if (letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u'):
print('Its Vowel')
else:
print('Its a consonant')


3. Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn

n = input("Enter the number to compute (n+nn+nnn): ")
x = int(n)
xx = int(n+n)
xxx = int(n+n+n)
compute_num = x+xx+xxx
print(compute_num)


4. Write a python program : accept 3 numbers from user and check the greater number

print('Enter the 3 numbers')
n1 = int(input('enter the first number: '))
n2 = int(input('enter the second number: '))
n3 = int(input('enter the thrid number: '))
if (n1> n2) and (n1>n3):
print('n1 is greatest number')
elif (n2> n1) and (n2>n3):
print('n2 is greatest number')
else:
print('n3 is greatest number')


5. Accept a number from user and check its 1 digit number or 2 digit number or 3 digit or 4 digit and if it is more then 4 digit number print more then 4 digit number like 5,6,7 any

number = input('Enter the number to check digits: ')
digit = len(number)
if digit < 4:
print('digit', digit)
else:
print('Its more than 4 digit')
good
 

Nitheesh Lal N

Active Member
Hi Nikhil,

I would like to suggest an easier answer for question number 3

n = int(input("Enter your number: "))
print("Your desired value as per n + n + nnn is : ",n*123)
#n+nn+nnn can be taken as n*(1+11+111)=n*123
 
Hi Nishant,

Just managed to upload the solution file for the tasks... please do provide your inputs... Thanks

Cheers

Praddy
 

Attachments

  • Tasks_from_LIVE_Class_Aug21.pdf
    102.6 KB · Views: 14
list1=[23,56,67,12,89,55,21,98,50,44,77]

find all even or odd numbers and add them into new separate list .
Solution:

list1=[23,56,67,12,89,55,21,98,50,44,77]

evenlist=[]
oddlist=[]

for inp in list1:
if inp%2==0:
evenlist.append(inp)
else:
oddlist.append(inp)

print(evenlist,oddlist)



Write a program to swap first value for list with last value.

Solution :

list2=[23,56,67,12,89,55,21,98,50,44,77]
lv1=list2[0]
lv2=list2[-1]

list2[0] = lv2
list2[-1] =lv1

print(list2)


Write a Python program to get unique values from a list

list3=[23,56,67,12,89,55,21,98,50,44,77,23,89]
unique=[]
for inp in list3:
if inp not in unique:
unique.append(inp)

print(unique)


Write a Python program to check whether a list contains a sublist

for item in list1:
if type(item) == list:
print("Contains Sublist")
else :
print("Doesnot Contains Sublist")
 
Last edited:

Nishant,

Small help... As you said Tuple is immutable, but we have other option of changing the contents of tuple by converting the tuple to list and make the necessary changes to the list and convert the same list back to the tuple. So when you have this option of doing this then why cannot we use list of tuple.. what is the advantage of having tuple.. Can you please let me know

Regards,
M.Ramana Murthy.
 
Dear Nishant Sir,

Please find listed below, the approach I followed to solve the tasks. Please provide your inputs. Thanks

Cheers

Praddy

TASKS:
1. Write a Python program to combine two dictionary adding values for common keys. d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400} Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})
2. Write a Python program to create a dictionary from a string. Note: Track the count of the letters from the string. Sample string : 'welcome' Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

TASK 1 SOLUTION:

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d1.update(d2)
d1

Output:
{'a': 300, 'b': 200, 'c': 300, 'd': 400}

TASK 2 SOLUTION:

string='welcome'

Step 1 : Split the word into a list of individual characters
def split(word):
return [char for char in word]
k=split(string)
k

Output:
['w', 'e', 'l', 'c', 'o', 'm', 'e']

Step 2 : Create another list using k with their indices
v=[]
for i in range(len(k)):
v.append(i)
v

Output:
[0, 1, 2, 3, 4, 5, 6]

Step 3 : Merged the 2 lists into a dictionary using the zip() function
def merge(list1, list2):
merged_list = dict(zip(list1, list2))
return merged_list
d3=merge(k,v)
d3

Output:
{'w': 0, 'e': 6, 'l': 2, 'c': 3, 'o': 4, 'm': 5}
 

Parul_34

Member
TASKS:
1. Write a Python program to combine two dictionary adding values for common keys. d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400} Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})
2. Write a Python program to create a dictionary from a string. Note: Track the count of the letters from the string. Sample string : 'welcome' Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

solution-1
d1={'a': 100, 'b': 200, 'c':300}
d2= {'a': 300, 'b': 200, 'd':400}
d3={}
for x in d1.keys():
for y in d2.keys():
if x==y:
d3.setdefault(x,d1[x]+d2[y])
d3.setdefault(y,d2[y])
d3.setdefault(x,d1[x])
d3

solution-2
d1={}
str="welcome"
for i in str:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u'):
d1.setdefault(i,2)
else:
d1.setdefault(i,1)

print(d1)
 

Parul_34

Member
Sir, you said we can use the list() to convert tuple to list but it is showing error and the same error is showing for dictionary to list
 

Nitheesh Lal N

Active Member
Hello Nishant Sir,

I have attached the assignment for yesterday as a pdf file. My codes were not properly displayed here on the website with some characters missing.
 

Attachments

  • Code.pdf
    11 KB · Views: 4

_59191

Member
Good Day Nishant Sir,
Below are my solutions for the questions provided for yesterday's class.
Seeking your valuable feedback.

Thank you.

Regards
Amit

# Write a Python program to combine two dictionary adding values for common keys.
# d1 = {'a': 100, 'b': 200, 'c':300}
# d2 = {'a': 300, 'b': 200, 'd':400}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

Python:
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
d3.update(d2)
d3.update(d1)

print("D1: ",d1)
print("D2: ",d2)
print("D3: ",d3)

OUTPUT:
D1: {'a': 100, 'b': 200, 'c': 300}
D2: {'a': 300, 'b': 200, 'd': 400}
D3: {'a': 100, 'b': 200, 'd': 400, 'c': 300}

# Write a Python program to create a dictionary from a string.
# Note: Track the count of the letters from the string.
# Sample string : 'welcome'
# Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}
Python:
st=input("Enter a string...: ")
stdict={}
for x in st:
    if x in stdict.keys():
        stdict[x]=stdict[x]+1
    else:
        stdict.update({x:1})
print("Character dictionary: ",stdict)

OUTPUT:
Enter a string...: This is a new string
Character dictionary: {'T': 1, 'h': 1, 'i': 3, 's': 3, ' ': 4, 'a': 1, 'n': 2, 'e': 1, 'w': 1, 't': 1, 'r': 1, 'g': 1}
 
Last edited:
Write a Python program to combine two dictionary adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})


d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
for i, j in d1.items():
for x, y in d2.items():
if i == x:
d3=(j+y)
else:
d3=j

for x,y in d2.items():
for i,j in d1.items():
if x==i:
continue
else:
d3[x]=y

print(d3)



Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : 'welcome'
Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

st = input("Enter a string: ")
dict1 = {}
for ch in st:
if ch in dict1:
dict1[ch] += 1
else:
dict1[ch] = 1
print(dict1)
 

Nikhil C R

Active Member
#Write a Python program to combine two dictionary adding values for common keys.

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

for x, y in d1.items():
for m,n in d2.items():
if x==m:
d3[x] = (y+n)

print(d3)

output -- {'a': 400, 'b': 400}

# HOW to print keys C and D in above program


# Write a Python program to create a dictionary from a string.
#Note: Track the count of the letters from the string.
#Sample string : 'welcome'
#Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

1st I split the welcome string -

str1 = 'welcome'
items11 = []
n = 1
for i in range(0, len(str1), n):
items11.append(str1[i:i+n])

print(items11)

Output - ['w', 'e', 'l', 'c', 'o', 'm', 'e']


Then i counted the occurence of each word -

count = []
for i in range(len(str1)):
count.append(str1.count((str1)))

print(count)


output - [1, 2, 1, 1, 1, 1, 2]

Then i merged items11 and count using zip

dict(zip(items11, count)) - That gave me ---> {'w': 1, 'e': 2, 'l': 1, 'c': 1, 'o': 1, 'm': 1}
 
Last edited:

Nikhil C R

Active Member
#Write a Python program to combine two dictionary adding values for common keys.

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

for x, y in d1.items():
for m,n in d2.items():
if x==m:
d3[x] = (y+n)

print(d3)

Output - {'a': 400, 'b': 400}

# HOW to get keys C and D in above program ??


# Write a Python program to create a dictionary from a string.
#Note: Track the count of the letters from the string.
#Sample string : 'welcome'
#Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

1st I split the welcome string -

str1 = 'welcome'
items11 = []
n = 1
for i in range(0, len(str1), n):
items11.append(str1[i:i+n])

print(items11)

Output - ['w', 'e', 'l', 'c', 'o', 'm', 'e']


Then i counted the occurence of each word -

count = []
for i in range(len(str1)):
count.append(str1.count((str1)))

print(count)


output - [1, 2, 1, 1, 1, 1, 2]

Then i merged items11 and count using zip

dict(zip(items11, count)) - That gave me ---> {'w': 1, 'e': 2, 'l': 1, 'c': 1, 'o': 1, 'm': 1}
Ignore the indentation here - somehow this community post is ignoring the spaces in every new line
 

_59191

Member
Ignore the indentation here - somehow this community post is ignoring the spaces in every new line
Hi Nikhil,
Use the code tag to format the code properly including the tabs and color coding. This is available on the top menu while write the post. Click on the "Three dots next to the "4 box" icon" and select "Code", type your code, change the language to "Python".
 

nishi4ca

Active Member
Alumni
Good Day Nishant Sir,
Below are my solutions for the questions provided for yesterday's class.
Seeking your valuable feedback.

Thank you.

Regards
Amit

# Write a Python program to combine two dictionary adding values for common keys.
# d1 = {'a': 100, 'b': 200, 'c':300}
# d2 = {'a': 300, 'b': 200, 'd':400}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

Python:
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
d3.update(d2)
d3.update(d1)

print("D1: ",d1)
print("D2: ",d2)
print("D3: ",d3)

OUTPUT:
D1: {'a': 100, 'b': 200, 'c': 300}
D2: {'a': 300, 'b': 200, 'd': 400}
D3: {'a': 100, 'b': 200, 'd': 400, 'c': 300}

# Write a Python program to create a dictionary from a string.
# Note: Track the count of the letters from the string.
# Sample string : 'welcome'
# Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}
Python:
st=input("Enter a string...: ")
stdict={}
for x in st:
    if x in stdict.keys():
        stdict[x]=stdict[x]+1
    else:
        stdict.update({x:1})
print("Character dictionary: ",stdict)

OUTPUT:
Enter a string...: This is a new string
Character dictionary: {'T': 1, 'h': 1, 'i': 3, 's': 3, ' ': 4, 'a': 1, 'n': 2, 'e': 1, 'w': 1, 't': 1, 'r': 1, 'g': 1}
Dear in Question one we wants to add the value if keys are same
 

nishi4ca

Active Member
Alumni

Nishant,

Small help... As you said Tuple is immutable, but we have other option of changing the contents of tuple by converting the tuple to list and make the necessary changes to the list and convert the same list back to the tuple. So when you have this option of doing this then why cannot we use list of tuple.. what is the advantage of having tuple.. Can you please let me know

Regards,
M.Ramana Murthy.
But your original Object of tuple will never change.. it will create a new object.
 

_59191

Member
Dear in Question one we wants to add the value if keys are same
Sorry Sir.. Missed that part.
Here is the updated code:
Python:
# Write a Python program to combine two dictionary adding values for common keys.
# d1 = {'a': 100, 'b': 200, 'c':300}
# d2 = {'a': 300, 'b': 200, 'd':400}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
d3.update(d2)
for x in d1.keys():
    if x in d3.keys():
        d3[x]=d3[x]+d1[x]
    else:
        d3.setdefault(x,d1[x])

print("D1: ",d1)
print("D2: ",d2)
print("D3: ",d3)

Below is the output
Code:
D1:  {'a': 100, 'b': 200, 'c': 300}
D2:  {'a': 300, 'b': 200, 'd': 400}
D3:  {'a': 400, 'b': 400, 'c': 300, 'd': 400}
 
Last edited:

nishi4ca

Active Member
Alumni
Sorry Sir.. Missed that part.
Here is the updated code:
Python:
# Write a Python program to combine two dictionary adding values for common keys.
# d1 = {'a': 100, 'b': 200, 'c':300}
# d2 = {'a': 300, 'b': 200, 'd':400}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
d3.update(d2)
for x in d1.keys():
    if x in d3.keys():
        d3[x]=d3[x]+d1[x]
    else:
        d3.setdefault(x,d1[x])

print("D1: ",d1)
print("D2: ",d2)
print("D3: ",d3)

Below is the output
Code:
D1:  {'a': 100, 'b': 200, 'c': 300}
D2:  {'a': 300, 'b': 200, 'd': 400}
D3:  {'a': 400, 'b': 400, 'c': 300, 'd': 400}
Good
 

_59191

Member
# Question 1: read abc.txt and write to xyz.txt
Python:
with open("abc.txt","r") as abc:
    data=abc.read()
with open("xyz.txt","w") as xyz:
    xyz.write(data)

# Question 2: Cound lines, words and characters in abc.txt
Python:
with open("abc.txt","r") as abc:
    linedata=abc.readlines()
lines=len(linedata)
words=0
for x in linedata:
    words=words+len(x.split())
characters=0
for x in linedata:
    characters=characters+len(x)

print("Lines: ",lines)
print("Words: ",words)
print("Chars: ",characters)
 

Nitheesh Lal N

Active Member
Hello Nishant Sir,

I have attached the assignment for yesterday as a pdf file. My codes were not properly displayed here on the website with some characters missing.
Python:
# Hello Nishant Sir,
# I experienced some difficulties with formatting issues when posting code on this website.
# So I had uploaded an attachment file. One of the members posted the solution for the issue, so I have posted the solution as a code again.

[CODE=python]# 1. Write a Python program to combine two dictionary adding values for common keys.

# d1 = {'a': 100, 'b': 200, 'c':300}
# d2 = {'a': 300, 'b': 200, 'd':400}
# Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
for i in d1:
    if i in d2:
        d3[i] = d1[i] + d2[i]
    else:
        d3[i] = d1[i]
        for j in d2:
                if j in d1:
                    continue
                else:
                    d3[j] = d2[j]
print(d3)


## 2. Write a Python program to create a dictionary from a string.
# Note: Track the count of the letters from the string.
# Sample string : 'welcome'
# Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

str = (input("Enter your string : "))
dict={}
for i in str:
    n=str.count(i)
    dict[i] = n
print(dict)
 
Hi Nishant,

Please find the below solutions:

1) Copy file content of one file to another file


readfile = open("C:\\Users\\Desktop\\test.txt","r")
writefile = open("C:\\Users\\Desktop\\xyz.txt","w")

writefile.writelines(readfile.readlines())

readfile.close()
writefile.close()



2) To fetch Number of lines,words and Charaters from the files :

readfile = open("C:\\Users\\Desktop\\test.txt","r")
con=readfile.readlines()
#print(con)
i=len(con)
cnt_word =0
cnt_char=0
print("Number of lines : ",i)
readfile.seek(0)

for inp in con:
word = inp.split(" ")
cnt_word=cnt_word+len(word)
# print(word)

for cnt in word:
cnt_char=cnt_char+len(cnt)

print("No of Words : ", cnt_word)
print("No of Characters : ",cnt_char)


readfile.close()



Regards,
M Ramana Murthy.
 

Nikhil C R

Active Member
Assingment on Text file -

1. Copy data from abc.txt to xyz.txt -

with open("C:/Users/Nikhil/Downloads/TXT/abc.txt", "r") as abc:
file_abc = abc.read()
with open("C:/Users/Nikhil/Downloads/TXT/xyz.txt", "w") as xyz:
xyz.write(file_abc)


2. abc.txt => Fine how many line,words,char

Number of lines -

abc = open(r"C:\Users\Nikhil\Downloads\TXT\abc.txt", "r")
lines = len(abc.readlines())
print("number of lines in abc txt file is ", lines)


Number of chacrater -

abc = open(r"C:\Users\Nikhil\Downloads\TXT\abc.txt", "r")
abc_file = abc.read()
charcter_count = len(abc_file)
print('number of characters in txt file ', charcter_count)


Number of words -

abc = open(r"C:\Users\Nikhil\Downloads\TXT\abc.txt", "r")
abc_file = abc.read()
words = abc_file.split() #split the data using a space - this will eliminate the space and are not counted
number_of_words = len(words)
print('number of words in txt file ', number_of_words)
 
Last edited:

Nitheesh Lal N

Active Member
1. Write a program to copy the contents of file from abc.txt => xyz.txt

Python:
with open("C:/Users/nithe/Downloads/abc.txt","r") as f:
    with open("C:/Users/nithe/Downloads/xyz.txt","w") as d:
        temp = f.read()
        d.write(temp)

2. Write a code to find the following in abc.txt => how many line,words,char

Python:
with open("C:/Users/nithe/Downloads/abc.txt","r") as f:
    print("The number of characters is : ",len(f.read()))
    f.seek(0)
    print("The number of lines is : ",len(f.readlines()))
    f.seek(0)
    words = 1
    for x in (f.read()):
        if x=="\n" or x==" ":
            words = words + 1
    print("The number of words is : ",words)
 
Hello Nishant, please check my assignment.

In question 10 and 12, how to get one desired output.

# Q1- write a python program to find the number is even or odd

x = int(input("Enter a number"))
if x%2==0:
print("Even")
else:
print("odd")

Output
Enter a number 9
odd



# Q2-write a python program to check character is vowels or consonants

char = input("Enter a character")
if char.lower()=="a" or char.lower()=="e" or char.lower()=="i" or char.lower()=="o" or char.lower()=="u":
print("it is a vowel")
else:
print("it is a consonant")

Output
Enter a character A
it is a vowel


#Q3- Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn

n = int(input("Enter a value"))
y=n**2
z=n**3
result=n+y+z
print(result)

Output
Enter a value 6
258


#Q4- Write a Python program that accepts an string (n) and computes the value of n+nn+nnn

n =(input("Enter a value"))
x=(n+n)
y=(n+n+n)
result = int(n)+int(x)+int(y)
print(result)

Output
Enter a value 5
615


#Q6- write a python program : accept 3 numbers from user and check the greater number

x = int(input("Enter First number"))
y = int(input("Enter second number"))
z = int(input("Enter third number"))
if x!=y and y!=z and z!=x:
if x>y and x>z:
print("First number is greater")
elif y>z and y>x:
print("second number is greater")
else:
print("Third number is greater")
else:
print("Please enter different value")

Output
Enter First number 2
Enter second number 3
Enter third number 4

Third number is greater


#Q7- accept a number from user and check its 1 digit number or 2 digit number or 3 digit or 4 digit and if it is more than 4 digit number print digit number like 5,6,7 any

x=input("Enter a number")
print("Number of digit {}".format (len(x)))

Output
Enter a number 456768

Number of digit 6


#Q8- find all even or odd numbers and add them into new separate list

list1=[23,56,67,12,89,55,21,98,50,44,77]
even=[]
odd=[]
for x in list1:
if x%2==0:
even.append(x)
else:
odd.append(x)
print(even)
print(odd)

Output
[56, 12, 98, 50, 44]
[23, 67, 89, 55, 21, 77]


#Q9- Write a program to swap first value for list with last value

even = [56, 12, 98, 50, 44]
even[0],even[-1]=even[-1],even[0]
print(even)

Output

[44, 12, 98, 50, 56]



# Q10- Write a Python program to check whether a list contains a sublist

list3=[44, 12, 98, [50, 56]]
for item in list3:
if type(item)==list:
print("list contains a sublist")
else:
print("no sublist")


Output
no sublist
no sublist
no sublist
list contains a sublist


#Q11- Write a Python program to combine two dictionary adding values for common keys ; Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d3 = {}
d3.update(d2)
for x in d1.keys():
if x in d3.keys():
d3[x]=d1[x]+d3[x]
else:
d3.setdefault(x,d1[x])
print(f"d3 = {d3}")

Output

d3 = {'a': 400, 'b': 400, 'd': 400, 'c': 300}


# Q12 - Write a Python program to create a dictionary from a string.
#Sample string : 'welcome'
#Expected output: {'w': 1, 'e':2,'l':1,'c':1,''o':1,'m':1}

st=str(input())
my_dict={}
for x in st:
n=st.count(x)
my_dict[x]=n
print(my_dict)

Output
welcome


{'w': 1}
{'w': 1, 'e': 2}
{'w': 1, 'e': 2, 'l': 1}
{'w': 1, 'e': 2, 'l': 1, 'c': 1}
{'w': 1, 'e': 2, 'l': 1, 'c': 1, 'o': 1}
{'w': 1, 'e': 2, 'l': 1, 'c': 1, 'o': 1, 'm': 1}
{'w': 1, 'e': 2, 'l': 1, 'c': 1, 'o': 1, 'm': 1}
 
Hello, In problem 2, during counting character program consider space also even use of strip() (character count should be 21).

#Assingment on Text file -

#prob 1. Copy data from abc.txt to xyz.txt

with open("abc.txt","r") as abc:
with open("xyz","w") as xyz:
xyz.write(abc.read())


#2. abc.txt => Find how many line,words,char, file contains "Hello welcome to DS world"

with open("xyz","r") as file1:

l=w=c=0
for i in file1:
l=l+i.count(i)
w=w+len(i.split())
c=c+len(i.strip())
print(l,w,c)

Output
1 5 25
 

Nikhil C R

Active Member
Hi,

How can i extract Integers values from HTML tags -

<span class="st_number">7,553</span>,
<span class="st_number">19,98,603</span>,
<span class="st_number">51,919</span>,
<span class="st_number">5,82,750</span>,
<span class="st_number">7,25,549</span>,
<span class="st_number">62,051</span>,
<span class="st_number">10,03,995</span>,
<span class="st_number">10,657</span>,
<span class="st_number">14,37,217</span>,
<span class="st_number">1,72,846</span>

Please advise....
 
Hii guys
i am not able to open beautiful soup library in my jupiter notebook

code: from bs4 import Beautifulsoup
error im getting:

ImportError Traceback (most recent call last)
<ipython-input-10-eab54f05eca9> in <module>
----> 1 from bs4 import Beautifulsoup

ImportError: cannot import name 'Beautifulsoup' from 'bs4' (C:\Users\Public\Downloads\anaconda3\lib\site-packages\bs4\__init__.py)

what i have to do?
 

Nikhil C R

Active Member
Hii guys
i am not able to open beautiful soup library in my jupiter notebook

code: from bs4 import Beautifulsoup
error im getting:

ImportError Traceback (most recent call last)
<ipython-input-10-eab54f05eca9> in <module>
----> 1 from bs4 import Beautifulsoup

ImportError: cannot import name 'Beautifulsoup' from 'bs4' (C:\Users\Public\Downloads\anaconda3\lib\site-packages\bs4\__init__.py)

what i have to do?
While importing use import BeautifulSoup - you are typing letter 's' as small. type like this - BeautifulSoup (Case sensitive)
 

AAHELI CHANDA

Member
Alumni
hello all, I am unable to connect with sql. Even when the following database is available in SQL, it is showing the following:
FATAL: database "Employeesdb" does not exist
 

AAHELI CHANDA

Member
Alumni
Hii AAHELI u are using postgresql right?
can you guide me what steps u done to connect to database?
#THE WORKING FOR CONNECTING TO A DATABASE WILL BE DONE IN SHELL PROMPT.
#DOWNLOAD PSYCOPG2 BY TYPING 'pip install psycopg2' in SQL SHELL PROMPT.
#In SQL SHELL PROMPT, type localhost,postgres,5432,user=postgres, password=your password .
#Create a database: type CREATE DATABASE;.
#Connect to the database: '\c database_name;'.
#CREATE SCHEMA: CREATE SCHEMA schema_name;.
#CREATE TABLE: SAME FORMAT AS IN SQL;.
#INSERT VALUES: INSERT INTO table_name VALUES(Add values);.
#We have to put ';' to EXECUTE a code.
 
Hi Nishant Sir - Have been working on the Walmat Project (Project 4). Stuck on converting Date to numeric and Linear regression task within the project. Given variables do not help predict sales at all... Can you please help? Thanks
 

_59191

Member
Hi Nishant Sir,
Attached are the code and the writeup for the work i was able to do on project # 1 (NYC Complaints - NYC311).
I need your help with the last point about statistical analysis as i am new to this. Also, the task 3 is a little tough for me.

Regards,
Amit
 

Attachments

  • project1-code.pdf
    97.4 KB · Views: 19
  • Project1-writeup.pdf
    29.8 KB · Views: 15
Last edited:

AAHELI CHANDA

Member
Alumni
Sir, in the test we this question "In machine learning, for the train_test_split method, what is the default split of the train dataset and test dataset?"
I have selected the ans 30% test and 70% train, they said the answer is incorrect and that the correct ans is 25%test and 75% train. Which one is correct?
 
Top