Python Coding Challenge 6 | Oxford Comma

preview_player
Показать описание
Enroll for exercises, tutorials, courses, and projects...

Enroll in Learn Python™ course

==================================================
Connect With Me!

Snapchat ► Rafeh1
Рекомендации по теме
Комментарии
Автор

def format(list):
list[-1]= 'and ' + list[-1]
return ', '.join(list)
print (format(['apple', 'grape', 'orange']))

isratjahan
Автор

Qazi please reopen the course we want to learn Python!🙏🙏🙇

talentedtuber
Автор

def commafy(list_):
if len(list_) > 2:
list_[-1] = 'and ' + list_[-1]
return ', '.join(list_)
#return ', '.join(list_[:-1]) + ' ' + list_[-1] # to print out "string1, string2 and string3" instead of "string1, string2, and string3"

print(commafy(['trinket', 'learning', 'wtf']))
print(commafy(['tralala', 'o7', '1337']))

>>> trinket, learning, and wtf
>>> tralala, o7, and 1337


If there is less than 3 strings:

def modify_last_element(list_):
list_[-1] = 'and ' + list_[-1]
return list_

def commafy(list_):

if len(list_) > 2:
modify_last_element(list_)
return ', '.join(list_)
elif len(list_) == 2:
modify_last_element(list_)
return ' '.join(list_)
else:
return list_[0]

print(commafy(['trinket', 'learning', 'wtf']))
print(commafy(['tralala', '1337']))
print(commafy(['tralala']))


>>> trinket, learning, and wtf
>>> tralala and 1337
>>> tralala

dvorapat
Автор

If you don't want to modify the last item of the list :-

def commafy(list):
return ', '.join(list[:-1]) + ", and " + list[-1]

lochnload
Автор

What if you didn't want to change the contents of the list after using this function? The modification of the last elements is done on the actual list correct?

_avenger
Автор

def commafy(list):
if list[-1]:
list[-1] = "and "+list[-1]

a = ", ".join(list)
return a
print(commafy(["hello", "bro", "ok"]))
it will say:
hello, bro, and ok

faisaltaher
Автор

@Clever Programmer
Hi, I'm trying to find what coding language to learn, I would like to know how to use terminal.

silerwiler
Автор

Hey i made a different solution on it. It's longer than yours though Qazi.
"""
def commafy(list):
string = ''
for number in range(0, len(list)):
if number == len(list)-1 :
string += 'and ' + list[number]
else:
string += list[number] + ', '
print string

print commafy(['lions', 'tigers', 'bears'])

"""

shreshthkaushik
Автор

Hi Qazi I am an undergraduate student so I was thinking 🤔 of getting into google summer of code but I am unable to figure out that which field should I choose
whether its related to data or related to web and which courses should I choose in order to learn any one of it...please help me out🆘

SM-gyyi
Автор

def commafy(given_list):
outputlist=''
for i in range(0, len(given_list)):
if (i==len(given_list)-1):
outputlist+='and '+str(given_list[i])
else:
outputlist+=str(given_list[i])+', '
return outputlist

i did this before i watched video and i thought "omg i did amazing this time" then i saw 2 line code then i was like -_-

gorkemcelebiler
Автор

Hey bro many of us want to enroll in your website, but we cant

combatxhoop
Автор

Hey, bro! is it necessary to enroll in to your website? cuz i want to learn about python from the first start. Btw, great videos tho! keep it up 👍

jaegerdanger
Автор

why don't you use a for loop?
for i in len(list):
if list[i] == list[len(list)-1]:
print "and "+list[i]
else:
print list[i]+", "

I didn't compile it but it should work

albertoeusebio
Автор

Qaziiii pls reopen the coursssseeee. We all want to learn Python cause of youuuu... PLS
I want an email in my inbox sent from le Qazi Porgrammer about the coursssseeee!

hornetiron
Автор

enrollment is not working.. yet to receive the email

premyou
Автор

I tried to enroll yesterday to your python programming lessons in your website but I still have not received an email to enter the lessons....I am really interested in learning python...Please send me the email.

giorgossofronas
Автор

I remember when you were at 8k subscribers and you've only gotton better, do you remember me???👈👈

joshnewman