Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,147,936 members, 7,799,171 topics. Date: Tuesday, 16 April 2024 at 04:28 PM

Python 3: Exercises And Solutions. - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Python 3: Exercises And Solutions. (8833 Views)

Salesforce Rest API Example Using Python 3 / Consume Kibana Rest API Using Python 3 / How To Write A Guessing Game Program In Python 3 (2) (3) (4)

(1) (2) (3) (4) (5) (6) (Reply) (Go Down)

Re: Python 3: Exercises And Solutions. by OlamigokePhilip: 10:48am On Apr 25, 2020
jayphe:


Exercise 1


Write a python program that obtains the domain name (thapar.edu) from the URL

http://www.thapar.edu/index.php/about-us/mission

Welldone @jayphe. Great initiative.

Just a passing comment for anyone who might need this..

In my own opinion.. Whenever you find yourself in situations where you "need" to manipulate strings; Regular Expressions (RegEx) should always come to mind. It really makes it easier and DRY if needed intermittently for multiple strings with marginal differences that could still break your code.

My Answer to Question 1.

import re
link = "http://www.thapar.edu/index.php/about-us/mission"
result = re.sub(r'https?:\/\/(?:www\.)?', '', link)
result.split('/')[0] #returns desired result

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 11:22am On Apr 25, 2020
OlamigokePhilip:


Welldone @jayphe. Great initiative.

Just a passing comment for anyone who might need this..

In my own opinion.. Whenever you find yourself in situations where you "need" to manipulate strings; Regular Expressions (RegEx) should always come to mind. It really makes it easier and DRY if needed intermittently for multiple strings with marginal differences that could still break your code.

My Answer to Question 1.

import re
link = "http://www.thapar.edu/index.php/about-us/mission"
result = re.sub(r'https?:\/\/(?:www\.)?', '', link)
result.split('/')[0] #returns desired result







That's a good suggestion bro! That's good!
Re: Python 3: Exercises And Solutions. by Zabiboy: 12:13pm On Apr 25, 2020
Name = input("enter your name" ))
Matric_No = input("enter your matric no" )
Course_code = input("enter the course code" )
score = int(input("enter your score" ))

If (score > 100):
score = "invalid value"

Elif (score >=95):
score ="Grade A"

Elif (score >=85) & (score<95):
score = "Grade B"

Elif (score >=70) & (score <85):
score = "Grade C"

Elif (score >=60) & (score <70):
score = "Grade D"

Elif (score >=50) & (score < 60):
score = "Grade E"

Else:
score = "Grade F"

Print( Name, Matric_No, Course_code, score )
Re: Python 3: Exercises And Solutions. by Zabiboy: 12:17pm On Apr 25, 2020
[b] Ok guys....concerning my last post on real life applications of python, here are some projects you can try...

1) CONTACT BOOK;
The main objective of this project is to save contact details. It’s important that you set up the commands users can use to enter the contact details. Some features you should implement include the commands to delete contacts, update contact information, and list saved contacts. You can also allow users to list contacts using different parameters, such as alphabetical order or contact creation date..
For those who already know django, this would be easier.

2) SITE CONNECTIVITY CHECKER:
When you visit a URL, you expect to get the requested pages on your browser. But this is not always the case. Sometimes, sites can be down, so you won’t get the desired results. Instead, you’ll be presented with error messages. You can keep trying a site that is down, till it comes up and you get the information you need.

This is where the Site Connectivity Checker project comes in. The Site Connectivity Checker visits a URL and returns the status of the URL: it is either live or not. 

The main objective of this project is to check the status of sites. So, you need to write code for checking the status of a website.....

Those who learnt socket programming in python would find this easier..

3) BULK FILE RENAME TOOL:
Sometimes, you need to name all the files in a directory according to certain conventions. For example, you can name all the files in a directory with File0001.jpg, where the numbers increase based on the number of files in the directory. Doing this manually can be stressful and repetitive.

The Bulk File Rename Tool allows users to rename a large number of files, without having to manually rename files.

The main objective of this project idea is to rename files. So, the application needs to find a way to manipulate the target files. The os, sys, and shutil libraries will be useful for a large part of this project.

Your users will be able to rename all the files in the directory, using naming conventions. Therefore, they should be able to pass in the naming convention of choice. The regex module will help match the required naming patterns, if you understand how regex works.


Lets start with these three...More would be posted as we progress....

GL....
[/b]

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 12:28pm On Apr 25, 2020
Zabiboy:
Name = input("enter your name" ))
Matric_No = input("enter your matric no" )
Course_code = input("enter the course code" )
score = int(input("enter your score" ))

If (score > 100):
score = "invalid value"

Elif (score >=95):
score ="Grade A"

Elif (score >=85) & (score<95):
score = "Grade B"

Elif (score >=70) & (score <85):
score = "Grade C"

Elif (score >=60) & (score <70):
score = "Grade D"

Elif (score >=50) & (score < 60):
score = "Grade E"

Else:
score = "Grade F"

Print( Name, Matric_No, Course_code, score )
Good job bro.
Just a little contribution, you can simplify the score input for more readability.

Elif(85<= score <95)
your return value
Elif(70 <= score < 85) and so on....
Re: Python 3: Exercises And Solutions. by kensmoney(m): 12:58pm On Apr 25, 2020
def Grade():
name = str(input("name:" ))
matric = str(input("matric:" ))
code =str(input("course code:" ))
score=int(input("score:" ))
if score <= 100 and score >=95:
print(name,matric,code, "Grade: A" )
elif score <= 94 and score >=85:
print(name, matric, code, "Grade: B" )
elif score <= 69 and score >=60:
print( name, matric, code,"Grade: D" )
elif score <= 59 and score >=50:
print(name, matric, code, "Grade: E" )
else:
print(name, matric, code, "Grade: F" )



Grade()
Re: Python 3: Exercises And Solutions. by kensmoney(m): 1:05pm On Apr 25, 2020
[quote author=Zabiboy post=88835496] [b] [i][font=serif] Ok guys....concerning my last post on real life applications of python, here are some projects you can try...

1) CONTACT BOOK;
The main objective of this project is to save contact details. It’s important that you set up the commands users can use to enter the contact details. Some features you should implement include the commands to delete contacts, update contact information, and list saved contacts. You can also allow users to list contacts using different parameters, such as alphabetical order or contact creation date..
For those who already know django, this would be easier.

2) SITE CONNECTIVITY CHECKER:
When you visit a URL, you expect to get the requested pages on your browser. But this is not always the case. Sometimes, sites can be down, so you won’t get the desired results. Instead, you’ll be presented with error messages. You can keep trying a site that is down, till it comes up and you get the information you need.

This is where the Site Connectivity Checker project comes in. The Site Connectivity Checker visits a URL and returns the status of the URL: it is either live or not. 

The main objective of this project is to check the status of sites. So, you need to write code for checking the status of a website.....

Those who learnt socket programming in python would find this easier..

3) BULK FILE RENAME TOOL:
Sometimes, you need to name all the files in a directory according to certain conventions. For example, you can name all the files in a directory with File0001.jpg, where the numbers increase based on the number of files in the directory. Doing this manually can be stressful and repetitive.

The Bulk File Rename Tool allows users to rename a large number of files, without having to manually rename files.

The main objective of this project idea is to rename files. So, the application needs to find a way to manipulate the target files. The os, sys, and shutil libraries will be useful for a large part of this project.

Your users will be able to rename all the files in the directory, using naming conventions. Therefore, they should be able to pass in the naming convention of choice. The regex module will help match the required naming patterns, if you understand how regex works.


Lets start with these three...More would be posted as we progress....

Cool projects
Re: Python 3: Exercises And Solutions. by Daddyshome: 1:54pm On Apr 25, 2020
jayphe:

Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

hmm....who else ooo? Let's get to business. Pls, let's try to read the question in details first. If the question asks for the users' input, then we should include an input statement. Zabiboy we need to talk ooo. This one wey I no see you....


Exercise 4
We are doing some 'loop and looping stuffs' today.


Write a Python program that accepts the name of a student, matric number, course code and the score he/she got in the course. E.g
Name: John Alfred
Matric. No.: MUC/CS/014/0882
Course Code: CMP 212
Score: 100

The program then print the result as:
John Alfred, MUC/CS/014/0882, CMP 212, Grade A


Using this grading table.
100-95 A
94-85 B
84-70 C
69-60 D
59-50 E
49-0 F

Corrections, advices, suggestions and opinions are highly welcome!

name = input()
mat_no = input()
course_code = input()
score = int(input())
def grade(score):
if score in range(95, 101):
return 'A'
if score in range(85, 95):
return 'B'
if score in range(70, 85):
return "C"
if score in range(60, 70):
return " D "
if score in range(50, 60):
return "E"
if score in range(0, 50):
return "F"

print('{}, {}, {} Grade {}'.format(name, mat_no, course_code, grade(score)))

3 Likes

Re: Python 3: Exercises And Solutions. by Ourown(m): 4:17pm On Apr 25, 2020
full_name = input("Please enter your full name: " ))
matric_no = input("Please enter your matric no: " )
course_code = input("Please enter the course code: " )
score = int(input(" please enter your score: " ))

If score > 100:
score = "score should be on 100 mark scale"

Elif score >= 95:
score ="A"

Elif 85 =< score > 95:
score = "B"

Elif 70 =< score > 85 :
score = "C"

Elif 60 =< score >70:
score = "grin"

Elif 50 =< score > 60):
score = "E"

Else:
score = " F"

Print( f' Dear {full_name}, with matric no {matric_no}, you have {score} in {course_code}" )

4 Likes

Re: Python 3: Exercises And Solutions. by Zabiboy: 5:27pm On Apr 25, 2020
If anyone is doing any of the projects i listed,
Jzt indicate the one u r doing...
GL

1 Like

Re: Python 3: Exercises And Solutions. by Daddyshome: 7:53pm On Apr 25, 2020
Zabiboy:
If anyone is doing any of the projects i listed,
Jzt indicate the one u r doing...
GL
Working on the first one.
Re: Python 3: Exercises And Solutions. by kensmoney(m): 8:08pm On Apr 25, 2020
Zabiboy:
If anyone is doing any of the projects i listed,
Jzt indicate the one u r doing...
GL

I will implement number 2
Re: Python 3: Exercises And Solutions. by Zabiboy: 8:56pm On Apr 25, 2020
Daddyshome:

Working on the first one.

Aii..
Goodluck
Re: Python 3: Exercises And Solutions. by Zabiboy: 8:57pm On Apr 25, 2020
kensmoney:


I will implement number 2

Good luck
Re: Python 3: Exercises And Solutions. by nwele2017: 9:13pm On Apr 25, 2020
jayphe:

Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

hmm....who else ooo? Let's get to business. Pls, let's try to read the question in details first. If the question asks for the users' input, then we should include an input statement. Zabiboy we need to talk ooo. This one wey I no see you....


Exercise 4
We are doing some 'loop and looping stuffs' today.


Write a Python program that accepts the name of a student, matric number, course code and the score he/she got in the course. E.g
Name: John Alfred
Matric. No.: MUC/CS/014/0882
Course Code: CMP 212
Score: 100

The program then print the result as:
John Alfred, MUC/CS/014/0882, CMP 212, Grade A


Using this grading table.
100-95 A
94-85 B
84-70 C
69-60 D
59-50 E
49-0 F

Corrections, advices, suggestions and opinions are highly welcome!

name=str(input("insert your full name: \n"wink)
matric_number=str(input('\ninsert your matriculation number:\n'))
course_code=str(input('\ninsert your course code:\n'))
score=int(input('\ninsert your score\n '))
def Score_out(number):
if number>=95:
return('Grade A')
elif 85<=number<=94:
return('Grade B')
elif 70<=number<=84:
return('Grad C')
elif 68<=number<=60:
return('Grade D')
elif 50<=number<=59:
return('Grade E ')
elif 0<=number<=49:
return('Grade F')

student_score=str(Score_out(score))
student_details=name.title()+", your matriculation number is, " +matric_number +", "+course_code+', '+ student_score
print(student_details)
Re: Python 3: Exercises And Solutions. by nwele2017: 9:20pm On Apr 25, 2020
jayphe:

Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

hmm....who else ooo? Let's get to business. Pls, let's try to read the question in details first. If the question asks for the users' input, then we should include an input statement. Zabiboy we need to talk ooo. This one wey I no see you....


Exercise 4
We are doing some 'loop and looping stuffs' today.


Write a Python program that accepts the name of a student, matric number, course code and the score he/she got in the course. E.g
Name: John Alfred
Matric. No.: MUC/CS/014/0882
Course Code: CMP 212
Score: 100

The program then print the result as:
John Alfred, MUC/CS/014/0882, CMP 212, Grade A


Using this grading table.
100-95 A
94-85 B
84-70 C
69-60 D
59-50 E
49-0 F

Corrections, advices, suggestions and opinions are highly welcome!

I want to commend what you are Doing here, it will really go a long way in helping python programmers that look down on this little excersis.
Why Nigeria programmers don't get good jobs is because the over look all this small excersis ,there is a lot of Nigeri professional programmers who cannot write a simple syntax that will evaluate the root of a quadratic equation.

1 Like

Re: Python 3: Exercises And Solutions. by Collinsanele: 9:26pm On Apr 25, 2020
nwele2017:


I want to commend what you are Doing here, it will really go a long way in helping python programmers that look down on this little excersis.
Why Nigeria programmers don't get good jobs is because the over look all this small excersis ,there is a lot of Nigeri professional programmers who cannot write a simple syntax that will evaluate the root of a quadratic equation.

I am afraid you are not correct.
Coding is not about syntax but thinking and problem solving.

If you write a program to solve a quadratic equation and it fails, you simply debug it and try again, that's testing, you must not be correct in one go.

And Nigerians have good coders fyi
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 9:29pm On Apr 25, 2020
Nice one,let me get my hand dirty here
Re: Python 3: Exercises And Solutions. by kensmoney(m): 9:34pm On Apr 25, 2020
nwele2017:


I want to commend what you are Doing here, it will really go a long way in helping python programmers that look down on this little excersis.
Why Nigeria programmers don't get good jobs is because the over look all this small excersis ,there is a lot of Nigeri professional programmers who cannot write a simple syntax that will evaluate the root of a quadratic equation.


Can't be true
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 10:46pm On Apr 25, 2020
Zabiboy:
[b] [i][font=serif] Ok guys....concerning my last post on real life applications of python, here are some projects you can try...


2) SITE CONNECTIVITY CHECKER:
When you visit a URL, you expect to get the requested pages on your browser. But this is not always the case. Sometimes, sites can be down, so you won’t get the desired results. Instead, you’ll be presented with error messages. You can keep trying a site that is down, till it comes up and you get the information you need.

This is where the Site Connectivity Checker project comes in. The Site Connectivity Checker visits a URL and returns the status of the URL: it is either live or not. 

The main objective of this project is to check the status of sites. So, you need to write code for checking the status of a website.....

Those who learnt socket programming in python would find this easier..




Import requests

def con_checker(url):
response=requests.get(url)
if response.status_code == 200:
return 'connected'
else: return 'unexpected error'
Re: Python 3: Exercises And Solutions. by Runningwater(m): 11:08pm On Apr 25, 2020
jayphe:


That's a wonderful approach Runningwater Kudos!
Buh u just defined the function. Anyway, I understand. That's gud!
Thanks I like the thread. Keep the good work
Re: Python 3: Exercises And Solutions. by Runningwater(m): 11:18pm On Apr 25, 2020
nwele2017:


I want to commend what you are Doing here, it will really go a long way in helping python programmers that look down on this little excersis.
Why Nigeria programmers don't get good jobs is because the over look all this small excersis ,there is a lot of Nigeri professional programmers who cannot write a simple syntax that will evaluate the root of a quadratic equation.
You're right... Solving programming problems increases problem solving skills. It shows whether you can implement the problem in a programming language.
Re: Python 3: Exercises And Solutions. by kensmoney(m): 12:26am On Apr 26, 2020
this is the link for project two:
plz make sure you enter the correct url cus i did not validate.................... lolz no time


https://kelvineapp.herokuapp.com/checkconnection/
import urllib.request
def Connection(request):
message= None
if request.method == "POST":
base_url = request.POST.get( "url" )
response = urllib.request.urlopen( base_url )
data = response.read()
if data :
print("connted", data)
message = "Website Connection Active"
return render(request, "html/connect.html", {"message": message})


else:
print(" not connted" )
message = "Connection Not Active, or invalid Url "
return render(request, "html/connect.html", {"record": message} )
return render(request, "html/connect.html" )
Re: Python 3: Exercises And Solutions. by jayphe(m): 3:45pm On Apr 26, 2020

Exercise 5
Write a Python program that accepts a string and a character from the user. The program checks the number of times(frequency) the character appeared in the string. E.g. If the user enters "Michael Faraday" as the string and enters 'a' as the character, the program should display:

There are 4 a in Michael Faraday!


Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy
Re: Python 3: Exercises And Solutions. by Zabiboy: 3:51pm On Apr 26, 2020
jayphe:

Exercise 5
Write a Python program that accepts a string and a character from the user. The program checks the number of times(frequency) the character appeared in the string. E.g. If the user enters "Michael Faraday" as the string and enters 'a' as the character, the program should display:

There are 4 a in Michael Faraday!


Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

Hv u seen the projects i wz talking of??

BTW, u said you wan talk to me...
Any Probs?
Re: Python 3: Exercises And Solutions. by jayphe(m): 3:57pm On Apr 26, 2020
Zabiboy:


Hv u seen the projects i wz talking of??

BTW, u said you wan talk to me...
Any Probs?
No problem at all. I go soon reach u sha. And about the project, I go check am now
Re: Python 3: Exercises And Solutions. by jayphe(m): 3:59pm On Apr 26, 2020
Zabiboy I've seen it. And it's a nice challenge. Bring in more!
Re: Python 3: Exercises And Solutions. by Zabiboy: 4:05pm On Apr 26, 2020
jayphe:
Zabiboy I've seen it. And it's a nice challenge. Bring in more!

grin grin....
i dont have much tough project in my head now...
You can google tougher ones....
Try this 3 out..
Someone has done number 2 although i'm yet to review it
Re: Python 3: Exercises And Solutions. by Collinsanele: 5:56pm On Apr 26, 2020
jayphe:

Exercise 5
Write a Python program that accepts a string and a character from the user. The program checks the number of times(frequency) the character appeared in the string. E.g. If the user enters "Michael Faraday" as the string and enters 'a' as the character, the program should display:

There are 4 a in Michael Faraday!


Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

def count_char(string, char):
return "There are "+ str(string.lower().count(char)) + " "+ str(char) + " in " + string



print(count_char(string="Michael Faraday", char='a'))

2 Likes

Re: Python 3: Exercises And Solutions. by kensmoney(m): 9:43pm On Apr 26, 2020
ken="kensmoney is good boy"

def count_char(a,b):
char = b.lower()
stri = a.lower()
counter=0
for a in stri:
if a == char:
counter += 1

print(counter)



count_char(ken, "o" )
#4

1 Like

Re: Python 3: Exercises And Solutions. by scarplanet(m): 10:10pm On Apr 26, 2020
jayphe:


Exercise 1


Write a python program that obtains the domain name (thapar.edu) from the URL

http://www.thapar.edu/index.php/about-us/mission

site = "http://www.thapar.edu/index.php/about-us/mission"
a, b = site.split(sep="//" )
c = b.split(sep="/ " )[0]
d, e, f = c.split(sep="." )
print("{}.{}".format(e, f))

1 Like

Re: Python 3: Exercises And Solutions. by Daddyshome: 10:56pm On Apr 26, 2020
Zabiboy:
[b] Ok guys....concerning my last post on real life applications of python, here are some projects you can try...

1) CONTACT BOOK;
The main objective of this project is to save contact details. It’s important that you set up the commands users can use to enter the contact details. Some features you should implement include the commands to delete contacts, update contact information, and list saved contacts. You can also allow users to list contacts using different parameters, such as alphabetical order or contact creation date..
For those who already know django, this would be easier.

2) SITE CONNECTIVITY CHECKER:
When you visit a URL, you expect to get the requested pages on your browser. But this is not always the case. Sometimes, sites can be down, so you won’t get the desired results. Instead, you’ll be presented with error messages. You can keep trying a site that is down, till it comes up and you get the information you need.

This is where the Site Connectivity Checker project comes in. The Site Connectivity Checker visits a URL and returns the status of the URL: it is either live or not. 

The main objective of this project is to check the status of sites. So, you need to write code for checking the status of a website.....

Those who learnt socket programming in python would find this easier..

3) BULK FILE RENAME TOOL:
Sometimes, you need to name all the files in a directory according to certain conventions. For example, you can name all the files in a directory with File0001.jpg, where the numbers increase based on the number of files in the directory. Doing this manually can be stressful and repetitive.

The Bulk File Rename Tool allows users to rename a large number of files, without having to manually rename files.

The main objective of this project idea is to rename files. So, the application needs to find a way to manipulate the target files. The os, sys, and shutil libraries will be useful for a large part of this project.

Your users will be able to rename all the files in the directory, using naming conventions. Therefore, they should be able to pass in the naming convention of choice. The regex module will help match the required naming patterns, if you understand how regex works.


Lets start with these three...More would be posted as we progress....

GL....
[/b]

Note: I am a beginner, so don't expect clean code. Also, I suck at naming variables grin . It can definitely be improved upon. I can protect the code with trys here and there, to prevent certain errors. I don't know if the list is necessary, but I don't know any way to sort dictionaries by time or maybe I should have stored the contacts using another data type. Criticism is welcome smiley


contacts = dict()
contacts_date = list()


def save_contact(name, number):
contacts[name] = number
contacts_date.append([name, number])
print('contact saved')

def display_contact_alphabetical():
print('Name\tNumbers')
for i, j in sorted(contacts.items()):
print( '{}\t{}'.format(i, j))


def delete(name):
contacts_date.remove([name, contacts[name]])
del contacts[name]
print('contact deleted')

def display_contact_date():
for a, b in contacts_date:
print('{}\t{}'.format(a, b))


def edit_contact_name(old_name, new_name):
tmp = contacts[new_name]
delete(old_name)
save_contact(new_name, tmp)
print('Contact information updated')


def edit_contact_num(name, new_num):
delete(name)
save_contact(name, new_num)
print('Contact information updated')


while True:
print('Press \n1. To save a contact\n2. To display contats\n3. To delete a contact\n4. To edit a contact\n5. To quit')
user_input = int(input())
if user_input == 1:
response_to_one = input('Enter name and number')
cont_name, cont_num = response_to_one.split()
save_contact(cont_name, cont_num)
elif user_input == 2:
print('Press \n1. To display alphabetically\n2. To display by time')
reply = int(input())
if reply == 1:
display_contact_alphabetical()
elif reply == 2:
display_contact_date()
elif user_input == 3:
response_to_three = input('Enter name')
delete(response_to_three)
elif user_input == 4:
print('Press \n1. To edit contact name\n2. To edit contact number')
reply_two = int(input())
if reply_two == 1:
reply_two_one = input('Enter old name and new name')
old_name, new_name = reply_two_one.split()
edit_contact_name(old_name, new_name)
elif reply_two == 2:
reply_two_two = input('Enter name and number')
cont_name, cont_num = reply_two_two.split()
edit_contact_num(cont_name, cont_num)
elif user_input == 5:
print('Exiting')
break

4 Likes

(1) (2) (3) (4) (5) (6) (Reply)

School Management System-SMS (version 1.7) Upgrade / Kehinde Adeyemi Among 12 Best Global IT Developers / Please Help Me With VTU API

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 88
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.