Subscribe Contact

Home  »  Reference Materials



Data Types

Category Python Syntax Description
Text str Strings are sequences of alphanumeric characters include the alphabetic letters A thru Z, both upper case and lowercase, as well as the digits 0 thru 9. Remember that when a numeric digit is within an alphanumeric string, it is not considered a number but rather a textual representation of that digit. Alphanumeric also includes other characters found on keyboards, such as @ $ # & * ( ) { } [ ] , = - _ . + ; ' /. Also, a blank space (created on the keybard by the spacebar) is considered alphanumeric.
Numeric int Integers are whole (counting) numbers, which can be negative or positive. Examples: 1, -45, 2462864. In Python integers can be of any length, the only limitation is available memory in the computer or device. When reading a numeric value, the Python interpreter determines the number is an integer if it starts with a whole number, with no prefix and with no decimal values at the end.
Numeric float Floating point numbers are numbers that include a decimal point and digits to the right of it. Examples: 4.0, 3.14, 0.003. In Python, we can also represent floats as values in scientific notation by adding an e with a positive or negative number after the e indicating the number of digits of notation. For example, 1.0e-5 equals 0.00001.
Numeric complex A Complex Number is a combination of a Real Number and an Imaginary Number. Complex numbers are used in scientific, mathematical and other application. You can learn more about the mathematical basis of Complex Numbers here. In Python, we represent Complex Numbers in the form < real number > + < imaginary number > + j. Examples: 5+2j, -2+4j, 6+19j.
Sequence list Lists are data structures in Python that allow us to store multiple data elements together with one variable name. Lists can store values of different data types in the same structure (this is called heterogeneous). The elements in a list are ordered and mutable, which means they remain in the specified order and that the values can be changed at runtime. Also, lists allow duplicate values, they are indexed (first element is index [0], second element is [1], etc.). In Python, lists are objects that have methods we can use to work with the list and they can be nested, that is, a list can contain lists. Example:

employee = [123456, "Bob", "Smith", 35, "Manager"]
print(employee)
print("Employee ID:\t" + str(employee[0]))
print("First Name:\t" + employee[1])
print("Last Name:\t" + employee[2])
print("Age:\t\t" + str(employee[3]))
print("Title:\t\t" + employee[4])
Sequence tuple Tuples are another data structure in Python that is nearly identical to the list data structure, in that it is an indexed sequence however, unlike the list, tuples are immutable. Once a tuple has been declared it cannot be changed. Tuples are very useful for data values that will not need to be changed. In fact, in some applications changing data is forbidden, so using a tuple in those instances adds a layer of confidence in the integrity of the data. Example:

employee = (123456, "Bob", "Smith", 35, "Manager")
print(employee)
print("Employee ID:\t" + str(employee[0]))
print("First Name:\t" + employee[1])
print("Last Name:\t" + employee[2])
print("Age:\t\t" + str(employee[3]))
print("Title:\t\t" + employee[4])
Sequence range Ranges in Python establish a sequence of numbers that, by default, start with zero and increments by one up to one less than a specified number. The general form of a range is range(start, stop, step), where start is the number to begin the range (default is zero), stop is the number that indicates the end of the range (it is not inclusive in the range values) and step is the increment for the range (default is one). Example:

nums = range(2, 20, 2)
for n in nums:
    print(n)
Map dict Dictionaries are collections of data values stored as key:value pairs. Dictionaries in Python are very similar to lists, however they are not indexed like lists, rather they are indexed by the keys of the key:value pairs. Keys in dictionaries must be unique, dictionaries cannot contain duplicate keys, similar to primary keys in databases. Example:

employee = {
  "ID": 123456,
  "first_name": "Bob",
  "last_name": "Smith",
  "age": 35,
  "title": "Manager"
}
print(employee)
print(employee["ID"])
print(employee["first_name"])
print(employee["last_name"])
print(employee["age"])
print(employee["title"])
Set set Sets in Python are unordered data structures whose elements must be unique, that is, it does not allow duplicate elements. Elements may be added or removed, but element values in the set cannot be modified (that is, they are immutable). Example:

cars = {"Audi", "Chevrolet", "Dodge", "Toyota"}
print(cars)
print(len(cars))
for c in cars:
    print(c)
Set frozenset A frozenset in Python is a set that is unchangeable, that is, it is frozen once it is defined.

Boolean bool Boolean is a binary state that can only be one of two values, True or False. Boolean variables in Python can only hold one of those two values and conditions can only result in one of those two values. We use boolean in many scenarios in programming, such as repeating an action until some condition becomes True. We often associate boolean result values of True and False to Yes or No questions.


«  Previous : Reference : Concepts Index
Next : Reference : Date & Time Format Codes  »




© 2023 John Gordon
Cascade Street Publishing, LLC