Category Archives: Library

11.4 Remove List Items

Remove Specified Item The remove() method removes the specified item. Example Remove “banana”:thislist = [“apple”, “banana”, “cherry”]thislist.remove(“banana”)print(thislist) Output:[‘apple’, ‘cherry’] Remove Specified Index The pop() method removes the specified index. Example Remove the second item:thislist = [“apple”, “banana”, “cherry”]thislist.pop(1)print(thislist) Output:[‘apple’, ‘cherry’] If you do not specify the index, the pop() method removes the last item. Example Remove the last item:thislist = [“apple”, “banana”, “cherry”]thislist.pop()print(thislist) Output:[‘apple’, ‘banana’] The del keyword also… Read More »

11.3 Add List Items

Append Items To add an item to the end of the list, use the append() method: Example Using the append() method to append an item:thislist = [“apple”, “banana”, “cherry”]thislist.append(“orange”)print(thislist) Output:[‘apple’, ‘banana’, ‘cherry’, ‘orange’] Insert Items To insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index: Example Insert an item as the second… Read More »

11.2 Change List Items

Change Item Value To change the value of a specific item, refer to the index number: Example Change the second item:thislist = [“apple”, “banana”, “cherry”]thislist[1] = “blackcurrant”print(thislist) Output:[‘apple’, ‘blackcurrant’, ‘cherry’] Change a Range of Item Values To change the value of items within a specific range, define a list with the new values, and refer to the range… Read More »

11.1 Access List Items

Access Items List items are indexed and you can access them by referring to the index number: Example Print the second item of the list:thislist = [“apple”, “banana”, “cherry”]print(thislist[1]) Output:banana Note: The first item has index 0. Negative Indexing Negative indexing means start from the end -1 refers to the last item, -2 refers to the second last item etc. Example… Read More »

11. Lists

mylist = [“apple”, “banana”, “cherry”] List 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: Example Create a List:thislist = [“apple”, “banana”, “cherry”]print(thislist) Output:[‘apple’,… Read More »

10. Operators

Operators are used to performing operations on variables and values. In the example below, we use the + operator to add together two values: Example print(10 + 5) Output:15 Python divides the operators into the following groups: Arithmetic operators Assignment operators Comparison operators Logical operators Identity operators Membership operators Bitwise operators Python Arithmetic Operators Arithmetic operators are used with… Read More »

9. Booleans

Booleans represent one of two values: True or False. Boolean Values 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: Example print(10 > 9)print(10 == 9)print(10 < 9) Output:TrueFalseFalse When you run a condition… Read More »

8.6 String Methods

Python has a set of built-in methods that you can use on strings. Note: All string methods returns new values. They do not change the original string. Example Upper case the first letter in this sentence:txt = “hello, and welcome to my world.”x = txt.capitalize()print (x) Output:Hello, and welcome to my world. Method Syntax Description capitalize() string.capitalize() Converts… Read More »

8.5 Escape Characters

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: Example You will get an error if you use double… Read More »

8.4 Format-Strings

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this: Example age = 36txt = “My name is John, I am ” + ageprint(txt) Output:Traceback (most recent call last):  File “demo_string_format_error.py”, line 2, in <module>    txt = “My name is John, I am ” + ageTypeError: must be str, not int But we can… Read More »