Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,485,072 members, 5,629,563 topics. Date: Sunday, 31 May 2020 at 11:23 AM

Python 3: Exercises And Solutions. - Programming - Nairaland

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

Consume Kibana Rest API Using Python 3 / How To Write A Guessing Game Program In Python 3 / Dificult To Update Pip in Python 3 when default is python 2 (2) (3) (4)

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

Python 3: Exercises And Solutions. by jayphe(m): 3:15pm On Apr 22
I welcome every Python Lords to this thread. Hmm....I don't seem to know you guys by name buh you can tell me to mention you in my next post.
Alright guys let's get this straight. This is not a tutorial thread...buh it can be of help sha. This is a place where we can ask ourselves 'Python' questions and share ideas.
Please welcome me home!
Re: Python 3: Exercises And Solutions. by jayphe(m): 3:18pm On Apr 22


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

2 Likes

Re: Python 3: Exercises And Solutions. by Collinsanele: 10:51am On Apr 23
website = "www...."
domain = website.split("http://www.")[1].split('/')[0]
print(domain)

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 3:12pm On Apr 23
Collinsanele:
website = "www...." domain = website.split("http://www.")[1].split('/')[0] print(domain)
File "<string>", line 2, in <module> IndexError: list index out of range

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 3:15pm On Apr 23
Collinsanele:
website = "www...." domain = website.split("http://www.")[1].split('/')[0] print(domain)
You are wrong...
Re: Python 3: Exercises And Solutions. by Collinsanele: 4:23pm On Apr 23
site ="https://www......"

domain = site.split('//')[1].split("www.")[-1]

print(domain)
Re: Python 3: Exercises And Solutions. by jayphe(m): 5:21pm On Apr 23
Collinsanele:
site ="https://www......"
domain = site.split('//')[1].split("www.")[-1]
print(domain)
you're making a mistake....
Re: Python 3: Exercises And Solutions. by jayphe(m): 5:22pm On Apr 23

This is my solution.....

#Obtain the domain (thapar.edu) name from the URL
#http://www.thapar.edu/index.php/about-us/mission

url = "http://www.thapar.edu/index.php/about-us/mission"
print ("Getting 'thapar.edu' from ",url)

list_url = url.rsplit("/",3)
print(list_url)
a = list_url[0]
str1 = str(a)
print("\n",a)
result = str1.split(".",1)
print("Finally, we now have....\n",result[1])
Re: Python 3: Exercises And Solutions. by jayphe(m): 5:30pm On Apr 23

This is another exercise.
Exercise 2

Write a Python program that accepts a string/sentence from the user. The program then changes all spaces to underscores. Finally, it prints out the final result.

Good luck guys!

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 8:49pm On Apr 23
jayphe:

This is another exercise.
Exercise 2

Write a Python program that accepts a string/sentence from the user. The program then changes all spaces to underscores. Finally, it prints out the final result.

Good luck guys!

user_input = str(input('write your full name))
user_input.replace(' ', ' _')
print(user_input)
Re: Python 3: Exercises And Solutions. by Collinsanele: 8:59pm On Apr 23
jayphe:

you're making a mistake....

What is the mistake
Re: Python 3: Exercises And Solutions. by kensmoney(m): 9:59pm On Apr 23
ken = "kensmoney is good"

def replaceWord(k):
sentence = str(k)
print(sentence.replace(" " , "_" )


replaceWord(ken)

#kensmoney_is_good
Re: Python 3: Exercises And Solutions. by jayphe(m): 7:53am On Apr 24
Taofeekdboy:

user_input = str(input('write your full name)) user_input.replace(' ', '_') print(user_input)
There was a space and a hypen in the 2nd argument of replace() Apart from that, you got it right! Bravo Toafeekdboy!
Re: Python 3: Exercises And Solutions. by jayphe(m): 7:56am On Apr 24
kensmoney:
ken = "kensmoney is good"
def replaceWord(k): sentence = str(k) print(sentence.replace(" " , "_" )

replaceWord(ken)
#kensmoney_is_good
kensmoney, I love your 'idea of thinking'. You got it right sha. Good job bro!
Re: Python 3: Exercises And Solutions. by jayphe(m): 7:58am On Apr 24
Collinsanele:

What is the mistake
Collinsanele, there was an IndexError stating 'list index out of range'
Re: Python 3: Exercises And Solutions. by jayphe(m): 8:05am On Apr 24

Collinsanele, Taofeekdboy, kensmoney another one is here!

Exercise 3

Write a Python program that accepts day, month and year from the user and prints the result in this form:

Today is 23/04/2020
For example,
Input:
day = 21
mnth = 07
year = 2010

Output:
Today is 21/07/2010

Good luck guys!

1 Like

Re: Python 3: Exercises And Solutions. by kensmoney(m): 11:46am On Apr 24
import datetime

date_and_time = datetime.datetime.now()
today_date =(str(date_and_time))
today_date_slice = ( today_date[:10] )
formated_date = today_date_slice.replace( "-", "/"
)
year = ( formated_date[:4] )
month_and_date = ( formated_date[5:] )

mydate= month_and_date + "/" + year
print(mydate)

#04/24/2020
Re: Python 3: Exercises And Solutions. by Zabiboy: 1:36pm On Apr 24
Op, these r short and not too tough projects...
Lets do the lengthy and real life application stuffs...
I can giv 3 diff. Projects if u guys are ok with d idea...

Note: these r projects i'v worked on/ working on

1 Like 1 Share

Re: Python 3: Exercises And Solutions. by jayphe(m): 6:54pm On Apr 24
Zabiboy:
Op, these r short and not too tough projects...
Lets do the lengthy and real life application stuffs...
I can giv 3 diff. Projects if u guys are ok with d idea...

Note: these r projects i'v worked on/ working on
Bro, I never said these were projects! They are - just as you said - simple exercises to keep 'things' busy. There is no bad if you also contribute to this. And I really would love to see one of your projects!
Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 7:16pm On Apr 24
jayphe:

Collinsanele, Taofeekdboy, kensmoney another one is here!

Exercise 3

Write a Python program that accepts day, month and year from the user and prints the result in this form:

Today is 23/04/2020
For example,
Input:
day = 21
mnth = 07
year = 2010

Output:
Today is 21/07/2010

Good luck guys!
I saw this late, I have been busy with my project and it took a lot of my time.
I will leave it here for who is interested to check..
It is a social media clone I made.
http://mysocialapp.pythonanywhere.com

Looking forward to your comments

1 Like

Re: Python 3: Exercises And Solutions. by Collinsanele: 10:17pm On Apr 24
Taofeekdboy:

I saw this late, I have been busy with my project and it took a lot of my time.
I will leave it here for who is interested to check..
It is a social media clone I made.
http://mysocialapp.pythonanywhere.com

Looking forward to your comments

You used Django right?
It's really nice
Re: Python 3: Exercises And Solutions. by Collinsanele: 10:28pm On Apr 24
kensmoney:
import datetime

date_and_time = datetime.datetime.now()
today_date =(str(date_and_time))
today_date_slice = ( today_date[:10] )
formated_date = today_date_slice.replace( "-", "/"
)
year = ( formated_date[:4] )
month_and_date = ( formated_date[5:] )

mydate= month_and_date + "/" + year
print(mydate)

#04/24/2020

You don't need to slice.
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 10:42pm On Apr 24
Collinsanele:


You used Django right?
It's really nice
Yes, I used django with Jquery, moving it to react soon cos I wanna add more functionalities to it.
Thanks bro.

2 Likes

Re: Python 3: Exercises And Solutions. by Daddyshome: 12:08am On Apr 25
jayphe:

Collinsanele, Taofeekdboy, kensmoney another one is here!

Exercise 3

Write a Python program that accepts day, month and year from the user and prints the result in this form:

Today is 23/04/2020
For example,
Input:
day = 21
mnth = 07
year = 2010

Output:
Today is 21/07/2010

Good luck guys!
day = int(input())
month = int(input())
year = int(input())
print("Today is {:02}/{:02}/{}".format(day, month, year))
Re: Python 3: Exercises And Solutions. by kensmoney(m): 1:56am On Apr 25
[quote author=Collinsanele post=88819388]

You don't need to slice.
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day

Yea but just want to do it in a diff way bro, no matter the approach,

1 Like

Re: Python 3: Exercises And Solutions. by Runningwater(m): 5:52am On Apr 25
I'm confused, the exercise said ACCEPT input from user. Isn't it like this?

def dateFun( currentDate, currentMonth, currentYear):
currentDate= int(input("Enter today's date: \n" )
currentMonth=int(input ("Enter today's month like 1 for January, 2 for February and so on: \n " )
currentYear=int(input("Enter current year: \n" )
message=currentDate,"/",currentMonth,"/",currentYear
return message

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 8:41am On Apr 25
Taofeekdboy:

I saw this late, I have been busy with my project and it took a lot of my time.
I will leave it here for who is interested to check..
It is a social media clone I made.
http://mysocialapp.pythonanywhere.com

Looking forward to your comments
A nice start bro....still need some fixing sha! Kudos!
Re: Python 3: Exercises And Solutions. by jayphe(m): 8:49am On Apr 25
kensmoney:
import datetime

date_and_time = datetime.datetime.now()
today_date =(str(date_and_time))
today_date_slice = ( today_date[:10] )
formated_date = today_date_slice.replace( "-", "/"
)
year = ( formated_date[:4] )
month_and_date = ( formated_date[5:] )

mydate= month_and_date + "/" + year
print(mydate)

#04/24/2020

Nice one bro...buh the question mentioned 'ACCEPT from user' and I can't find input().
Also just has Collinsanele said, you don't need to slice at all. However, you did great. Well done kensmoney
Re: Python 3: Exercises And Solutions. by jayphe(m): 8:51am On Apr 25
Runningwater:
I'm confused, the exercise said ACCEPT input from user. Isn't it like this?

def dateFun( currentDate, currentMonth, currentYear):
currentDate= int(input("Enter today's date: \n" )
currentMonth=int(input ("Enter today's month like 1 for January, 2 for February and so on: \n " )
currentYear=int(input("Enter current year: \n" )
message=currentDate,"/",currentMonth,"/",currentYear
return message

That's a wonderful approach Runningwater Kudos!
Buh u just defined the function. Anyway, I understand. That's gud!

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 8:54am On Apr 25
jayphe:

A nice start bro....still need some fixing sha! Kudos!
Thanks bro.
Obviously, it does!!!. Just worked on it with the aim of not using React, I worked with Django and JQuery because I wanted to master JQuery for small projects in which React might be an overkill. If time permits, I'm planning on moving it to a mobile app. Maybe it will be better there.

2 Likes

Re: Python 3: Exercises And Solutions. by jayphe(m): 9:13am On Apr 25

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!

2 Likes

Re: Python 3: Exercises And Solutions. by jayphe(m): 9:14am On Apr 25
Taofeekdboy:

Thanks bro.
Obviously, it does!!!. Just worked on it with the aim of not using React, I worked with Django and JQuery because I wanted to master JQuery for small projects in which React might be an overkill. If time permits, I'm planning on moving it to a mobile app. Maybe it will be better there.
Wish you the best bro! Always the best!

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

How Can I Deploy Applications Create In Vb.net / A Problem With Java Gui Design / Storing K largest numbers from a stream

(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 - 2020 Oluwaseun Osewa. All rights reserved. See How To Advertise. 115
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.