#5 – LEARN PYTHON WITH PRACTICE – Lists

#5 – LEARN PYTHON WITH PRACTICE – Lists

Following the last example (exercice) of the previous course about conditional structures in Python, we would add skills to the user resume.

This is our actual program of our resume software :

print("This program will help you to create awesome resume and find \n new job")
user_name = input("What is your full name ?")
user_age = input("What is your age ?")
user_school = input("What is your school name ?")
user_looking_for_new_job = input("Are you looking for new job ?\n if yes tape 'YES'")

# let's print the user resume

print("Name : ", user_name)
print("age : ", user_age)
print("School : ", user_school)

if user_looking_for_new_job == "YES":
	print("Looking for new job : ", user_looking_for_new_job)

If we wanted to add skills to the resume, we would ask user for his skills and save them as a string. We can imagine something like :

print("This program will help you to create awesome resume and find \n new job")
user_name = input("What is your full name ?")
user_age = input("What is your age ?")
user_school = input("What is your school name ?")
user_looking_for_new_job = input("Are you looking for new job ?\n if yes tape 'YES'")
user_skills = input("Enter your skills separated by comma (,)")

# let's print the user resume

print("Name : ", user_name)
print("age : ", user_age)
print("School : ", user_school)
print("Skills : ", user_skills)
if user_looking_for_new_job == "YES":
	print("Looking for new job")

Output

We can add the user skills in that way but it is not pratical, beacause if we want to add new skill or remove on, it will be a bit difficult and will take time to write our code.

Python helps us with Lists, a list is a another type of data or another data structure.

In a List we can add elements, iterate them and remove them, check if element is in a list or not … etc.

Before starting studying lists, I would like teach you how to extract data from a string using delimiter.

skills = "Python, Java, C, PHP, Javascript, HTML5, CSS3"

skills_list = skills.split(",")

print(skills_list)

Output :

As you see, skills.split(« , ») splits the string skills to elements and save them in a list.

skills_list is a list created and returned by skills.split() function

split function extract data from a string, data are separated with some delimiter, in our case the delimiter was a comma. The delimiter can be another character.

If we want to check if user masters Javascript, we can do like :

skills = "Python,Java,C,PHP,Javascript,HTML5,CSS3"

skills_list = skills.split(",")

print(skills_list)

if 'Javascript' in skills_list :
    print("User is mastering Javascript !")

Now, if we want to add a new skill, we have to use .append() method (function) :

skills = "Python,Java,C,PHP,Javascript,HTML5,CSS3"
skills_list = skills.split(",")

print(skills_list)
skills_list.append("C#")
print(skills_list)

Output :

To remove an element from list, we have to use .remove() function :

skills = "Python,Java,C,PHP,Javascript,HTML5,CSS3"
skills_list = skills.split(",")

print(skills_list)
skills_list.remove("Javascript")
print(skills_list)

Output :

Indexes :

Index is the position of an element in a list, in Python indexes start from 0 and not 1, like you start counting from 0 not 1.

To get an element in a specified index, you have to use expression like :

skills = "Python,Java,C,PHP,Javascript,HTML5,CSS3"
skills_list = skills.split(",")

print(skills_list[0])
print(skills_list[1])
print(skills_list[2])
print(skills_list[3])
print(skills_list[4])
print(skills_list[5])
print(skills_list[6])

Output :

Imagine our list contains 100 elements and we want to print them one after one, do we have to write print(skills_list[0]), print(skills_list[0]), print(skills_list[3]), print(skills_list[2]) …, print(skills_list[99]) ?

That’s what we will see in the next chapter, how to browse a list using loops.

NOTHER STEP AND YOU PASS YOU BEGINNER LEVEL, CONTINUE THE LAST CHAPTER OF BEGINNER LEVEL

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *