List Tuple Dictionary Boolean
List Tuple Dictionary Boolean
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
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
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
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.
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.
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(len(thisdict))
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)
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Python Keywords
Python contains thirty-five keywords
CODE:
import keyword
# displaying the complete list using "kwlist ()."
print ("The set of keywords in this version is: ")
print ( keyword.kwlist )