Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,483,418 members, 5,623,819 topics. Date: Thursday, 28 May 2020 at 03:09 PM

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

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

Consume Kibana Rest API Using 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)

Re: Python 3: Exercises And Solutions. by Daddyshome: 11:13pm On Apr 26
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
string = input('Enter a string')
char = input('Enter a character in the string')
print('{} appears {} times in the word {}'.format(char, string.casefold().count(char.casefold()), string ))

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 6:30am On Apr 27
kensmoney:
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
I love ur approach.....

1 Like

Re: Python 3: Exercises And Solutions. by scarplanet(m): 12:29pm On Apr 27
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!


Unfortunately, Nairaland would not allow indentation

class Student:

def __init__(self, firstname, lastname, matric, course):
self.firstname = firstname
self.lastname = lastname
self.matric = matric
self.course = course

def grade(self, score):
if score >= 95:
return "A"
elif score >= 85:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return " D "
elif score >= 50:
return "E"
else:
return "F"

def fullname(self):
return "{} {}".format(self.firstname, self.lastname)

first = input("Enter Firstname of Student: " )
last = input("Enter Lastname of Student: " )
matricNo = input("Enter Matriculation Number of Student: " )
coursecode = input("Enter the Course Code: " )
studentscore = eval(input("Enter the Score for Student: " ))
stud1 = Student(first, last, matricNo, coursecode)

print("{}, {}, {}, Grade {}".format(stud1.fullname(), stud1.matric, stud1.course, stud1.grade(studentscore)))

1 Like

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

Exercise 6
Write a Python program that accepts an integer number and checks if it is an even number or an odd number. Pls don't use the % (modulus) operator. Let's see some genuius.
Re: Python 3: Exercises And Solutions. by Shepherdd: 10:08am On Apr 28
jayphe:

Exercise 6
Write a Python program that accepts an integer number and checks if it is an even number or an odd number. Pls don't use the % (modulus) operator. Let's see some genuius.

def check_type(val):
_type = 'even' if val & 1 == 0 else 'odd'
return _type
Re: Python 3: Exercises And Solutions. by nwele2017: 10:27am On Apr 28
jayphe:

Exercise 6
Write a Python program that accepts an integer number and checks if it is an even number or an odd number. Pls don't use the % (modulus) operator. Let's see some genuius.

numbers=list(range(1,10000000))
even_number=int(input("Input a number:\n" ))
for i in numbers:
if (even_number/2)==i:
print(str(even_number)+" is even an number" )

1 Like

Re: Python 3: Exercises And Solutions. by nwele2017: 10:49am On Apr 28
nwele2017:


numbers=list(range(1,10000000))
even_number=int(input("Input a number:\n" ))
for i in numbers:
if (even_number/2)==i:
print(str(even_number)+" is even an number" )
this only works when you put an even number, it cannot tell if a number is not even number
Re: Python 3: Exercises And Solutions. by Walkingkilo(m): 11:03am On Apr 28
Collinsanele:


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'))

This is a quite a apt and accurate approach

2 Likes

Re: Python 3: Exercises And Solutions. by kensmoney(m): 5:59pm On Apr 28

def check_num(nums):
num = nums/2
check = num.is_integer()
if check == True:
print("even" )
else:
print("odd" )
check_num(5)

1 Like

Re: Python 3: Exercises And Solutions. by SKYQUEST(m): 12:46am On Apr 29
Great job guys!

Write a python program where python LOOKS FOR Total Assets and Total liabilities in the attached table and returns the value of the ratio of the two given variables as per their values in the table.

Re: Python 3: Exercises And Solutions. by scarplanet(m): 12:57am On Apr 29
SKYQUEST:
Great job guys!

Write a python program where python LOOKS FOR Total Assets and Total liabilities in the attached table and returns the value of the ratio of the two given variables as per their values in the table.


Is this a job at your workplace or an assignment given to you? Oya confess cheesy grin

1 Like

Re: Python 3: Exercises And Solutions. by hardeycute(m): 7:41am On Apr 29
scarplanet:


Is this a job at your workplace or an assignment given to you? Oya confess cheesy grin
Check his thread https://www.nairaland.com/5789054/python-accountants/
Re: Python 3: Exercises And Solutions. by Collinsanele: 10:25am On Apr 29
jayphe:
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!

Write a function that takes a list of numbers and returns the first unique number in the list.
(You may submit your solutions via pastebin)

Example:
[1,4,6,1,8,4,9] -> returns 6
Re: Python 3: Exercises And Solutions. by Collinsanele: 11:06am On Apr 29
SKYQUEST:
Great job guys!

Write a python program where python LOOKS FOR Total Assets and Total liabilities in the attached table and returns the value of the ratio of the two given variables as per their values in the table.

Can be easily done with pandas
Re: Python 3: Exercises And Solutions. by Collinsanele: 11:16pm On Apr 29
Daddyshome:

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

contact _date list is misleading as there aren't any date in it (Lol).

To use dates in Python, you can make use of the datetime module, it's straight forward, however, nice program.
Re: Python 3: Exercises And Solutions. by OutOfTheAshes(m): 2:41am On Apr 30
kensmoney:
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()

Your code is redundant. Fix it!!!!!
Re: Python 3: Exercises And Solutions. by StevDesmond(m): 2:42am On Apr 30
Collinsanele:


Write a function that takes a list of numbers and returns the first unique number in the list.
(You may submit your solutions via pastebin)

Example:
[1,4,6,1,8,4,9] -> returns 6

2 Likes

Re: Python 3: Exercises And Solutions. by gbolly1151(m): 7:46am On Apr 30
[quote author=StevDesmond post=89009836][/quote]

This seem to be the perfect algorithm to solve it.nice one sir
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 7:57am On Apr 30
Guy when you are posting code,you can use this format

[Code]
#Your code here

[/Code]

Change the C to c

Example of output is


print('hello word')
Re: Python 3: Exercises And Solutions. by Daddyshome: 8:08am On Apr 30
Collinsanele:


contact _date list is misleading as there aren't any date in it (Lol).

To use dates in Python, you can make use of the datetime module, it's straight forward, however, nice program.
Thanks I'll look into that. The list is ranked by first one created that's the closest thing to date I know currently.
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 8:13am On Apr 30
Daddyshome:

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


Nice one...by the time you learn class...you will really appreciate what you have done here,this is good for a start
Re: Python 3: Exercises And Solutions. by Daddyshome: 10:40am On Apr 30
gbolly1151:


Nice one...by the time you learn class...you will really appreciate what you have done here,this is good for a start
Thank you sir
Re: Python 3: Exercises And Solutions. by Daddyshome: 10:56am On Apr 30
jayphe:

Exercise 6
Write a Python program that accepts an integer number and checks if it is an even number or an odd number. Pls don't use the % (modulus) operator. Let's see some genuius.
def odd_even_checker(num):
if num[-1] in ['1', '3', '5', '7', '9']:
return 'odd'
elif num[-1] in ['0', '2', '4', '6', '8']:
return 'even'
num = input()
print(odd_even_checker(num))
Re: Python 3: Exercises And Solutions. by Daddyshome: 11:15am On Apr 30
Collinsanele:


Write a function that takes a list of numbers and returns the first unique number in the list.
(You may submit your solutions via pastebin)

Example:
[1,4,6,1,8,4,9] -> returns 6
lis = [1, 4, 6, 1, 8, 4, 9]
unique = [x for x in lis if lis.count(x) == 1]
print(unique[0])
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 11:36am On Apr 30
jayphe:

Exercise 6
Write a Python program that accepts an integer number and checks if it is an even number or an odd number. Pls don't use the % (modulus) operator. Let's see some genuius.

Let me see if my ojoro will work here....


num=int(input('your num '))
status='even' if (bin(num)).endswith('0') else 'odd'
print(status)


Oya find me the bug
Re: Python 3: Exercises And Solutions. by SKYQUEST(m): 5:41pm On Apr 30
Collinsanele:


Can be easily done with pandas

lets have your solution sir
Re: Python 3: Exercises And Solutions. by KingAzubuike(f): 6:42pm On Apr 30
kensmoney:
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
Thumbs up
Re: Python 3: Exercises And Solutions. by kensmoney(m): 7:31pm On Apr 30

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

Re: Python 3: Exercises And Solutions. by kensmoney(m): 7:32pm On Apr 30
gbolly1151:
Guy when you are posting code,you can use this format

[Code]
#Your code here

[/Code]

Change the C to c

Example of output is


print('hello word')


The formating is superb tongue
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 10:37pm On Apr 30
kensmoney:

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

Check your code bro,it doesn't seem right
Re: Python 3: Exercises And Solutions. by Collinsanele: 6:48am On May 01
SKYQUEST:


lets have your solution sir

Where can I get the dataset
Re: Python 3: Exercises And Solutions. by Collinsanele: 6:52am On May 01
Daddyshome:

lis = [1, 4, 6, 1, 8, 4, 9]
unique = [x for x in lis if lis.count(x) == 1]
print(unique[0])

Nice but if there's no unique item, then, you would have an index error.

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

How Programming Affects Your Life / Sign Your Online Documents With This App / How To Add Scratch Card To Website To Check Result

(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. 170
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.