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:
Code:
thislist = ["apple", "banana", "cherry"]
print(thislist)
List Items:
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
Ordered:
When we say that lists are ordered, it means that the items have a defined order, and that
order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
Allow Duplicates:
Since lists are indexed, lists can have items with the same value:
Lists allow duplicate values
Code:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
List Length:
To determine how many items a list has, use the len() function:
Print the number of items in the list
Code:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
output : 3
List Items - Data Types
List items can be of any data type
List items can be of any data type
String, int and boolean data types
Code:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
type ()
Code:
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
Append Items:
To add an item to the end of the list, use the append() method
Using the append() method to append an item
Code:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
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
Insert an item as the second position
Code:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
Python Tuples:
Tuple
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
Create a Tuple:
Code:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Tuple Items:
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Ordered:
When we say that tuples are ordered, it means that the items have a defined order, and that order
will not change.
Unchangeable:
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has
been created.
Allow Duplicates:
Since tuples are indexed, they can have items with the same value
Tuples allow duplicate values
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
Difference between List and Tuples:
The list is dynamic, whereas the tuple has static characteristics. This means that lists can be
modified whereas tuples cannot be modified, the tuple is faster than the list because of
static in nature. Lists are denoted by the square brackets but tuples are denoted as
parenthesis.
Dictionary
Dictionaries are used to store data values in key: value pairs.
A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
Dictionaries are written with curly brackets, and have keys and values.
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Dictionary Items
Dictionary items are ordered, changeable, and does not allow duplicates.
Dictionary items are presented in key: value pairs, and can be referred to by using the key
name.
Changeable
Dictionaries are changeable, meaning that we can change, add or remove items after the
dictionary has been created.
Duplicates Not Allowed
Dictionaries cannot have two items with the same key
Duplicate values will overwrite existing values
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
Dictionary Length
To determine how many items a dictionary has, use the len() function.
Print the number of items in the dictionary.
print(len(thisdict))
Dictionary Items - Data Types
The values in dictionary items can be of any data type:
String, int, boolean, and list data types:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
Python Collections (Arrays)
There are four collection data types in the Python programming language:
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate
members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.
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.
Code:
print( 10 > 9)
print(10 == 9)
print(10 < 9)
Print a message based on whether the condition is True or False:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Most Values are True
Almost any value is evaluated to True if it has some sort of content.
Any string is True, except empty strings.
Any number is True, except 0.
Any list, tuple, set, and dictionary are True, except empty ones.
Python Keywords
Python contains thirty-five keywords
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
CODE:
import keyword
# displaying the complete list using "kwlist ()."
print ("The set of keywords in this version is: ")
print ( keyword.kwlist )