Daily · February 6, 2023

Python Lists

Python lists can be extremely useful.

What is a list? A list holds elements together. Example:

g = ['hi', 'hello', 'how are you', 'good day', 'how are you doing']

The element “hi” is element 0. “hello” is number 1. So the first element we see is actually the 0th element in Python.

What you can do to a list:

(result is below)

g = ['hi', 'hello', 'how are you', 'good day', 'how are you doing']
print(g[0]) #print the first element in greetings
hi

g = ['hi', 'hello', 'how are you', 'good evening', 'good afternoon']
g[1] = 'good morning'  #switch element 1 (hello) with "good morning"
print(g)
['hi', 'good morning', 'how are you', 'good evening', 'good afternoon']

g = ['hi', 'good morning', 'how are you', 'good evening', 'good afternoon']
g.append('hey')  #add something
print(g)
['hi', 'good morning', 'how are you', 'good evening', 'good afternoon', 'hey']

g = ['hi', 'good morning', 'how are you', 'good evening', 'good afternoon', 'hey']
g.remove('how are you')  #remove something
print(g)
['hi', 'good morning', 'good evening', 'good afternoon', 'hey']

g = ['hi', 'good morning', 'how are you', 'good evening', 'good afternoon', 'hey']
g.sort()  #sort in alphabetical order
print(g)
['good afternoon', 'good evening', 'good morning', 'hey', 'hi', 'how are you']

Also:

s = [1, 2, 3, 4, 5]
a = sum(s)
print(a)
15