Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
boy_names = ['khaled', 'amir', 'malik'] # this creates the list
print(boy_names)

# this prints: ['khaled', 'amir', 'malik']

boy_names.append('ali') # You use the variable name then put .append() after the variable name. Then put the name/element/item you want to add to the list using quotes around the name and put it in the brackets it will add the new name to the end of the list. Note: you use quotes because it's a string (text). If you're doing a list in which you're using numbers and want to keep them as integers or floats etc (eg if you want to add and multiply them) you don't need the quotes.
print(boy_names) # this prints: ['khaled', 'amir', 'malik', 'ali'] as you can see the name 'ali' is added to the end of the list now and the variable boys_names will always have 'ali' in the list when it gets printed or used in the program etc since it got appended/added (unless​​ you remove it).


# Hello, this is the explanation and example to the question 'What does .append() do in Python'.

# Explantion: The .append function is used when you want to add a new element/item/thing to a list. Eg. append/add a new name to a list of boys names or add a username to a list of already taken/used usernames, like Instrgram would when you create a new account as an example. So to add a new element/item/thing to a list is to append (add) the item to the list. When you append an item to a list, the new element is added to the end of the list. If I put this in an example, let's say you want to make a list of possible baby names for a boy and you want to append (add) a new name that you like to this list, you would do what I did above.

# So I hope this clears up any problems or confusion, and if you have any questions feel free to ask me via my Instagram or Twitter @zcses or by email [email protected] :)

##########
# NOTE: To make this less confusing: fork/copy the code into a new repl, delete the comments and run the code then it should hopefully be more clear
##########