Loop Through a List
You can loop through the list items by using a for
loop:
Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
You can loop through the list items by using a for
loop:
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
The remove()
method removes the specified item.
Remove “banana”:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
If there are more than one item with the specified value, the remove()
method removes the first occurrence: Continue reading Python – Remove List Items
To add an item to the end of the list, use the append() method:
Using the append()
method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
To insert a list item at a specified index, use the insert()
method.
The insert()
method inserts an item at the specified index: Continue reading Python – Add List Items
To change the value of a specific item, refer to the index number:
Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values: Continue reading Python – Change List Items
List items are indexed and you can access them by referring to the index number:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Note: The first item has index 0.
Negative indexing means start from the end
-1
refers to the last item, -2
refers to the second last item etc. Continue reading Python – Access List Items
mylist = ["apple", "banana", "cherry"]
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets: Continue reading Python Lists
Operators are used to perform operations on variables and values.
In the example below, we use the +
operator to add together two values:
print(10 + 5)
Python divides the operators in the following groups:
Arithmetic operators are used with numeric values to perform common mathematical operations: Continue reading Python Operators
Booleans represent one of two values: True
or False
.
In programming you often need to know if an expression is True
or False
.
You can evaluate any expression in Python, and get one of two answers, True
or False
.
When you compare two values, the expression is evaluated and Python returns the Boolean answer: Continue reading Python Booleans
Python has a set of built-in methods that you can use on strings.
Note: All string methods return new values. They do not change the original string.
To insert characters that are illegal in a string, use an escape character.
An escape character is a backslash \
followed by the character you want to insert.
An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
You will get an error if you use double quotes inside a string that is surrounded by double quotes:
txt = "We are the so-called "Vikings" from the north."
To fix this problem, use the escape character \"
: Continue reading Python – Escape Characters