With our first script complete, it is time to understand the basic data types of Python. These data types are similar to those found in other programming languages, but are invoked with a simple syntax, which is described in the following table and sections. For a full list of standard data types available in Python, visit the official documentation at https://docs.python.org/3/library/stdtypes.html:
Data Type
|
Description
|
Example
|
Str |
String |
str(), "Hello", 'Hello' |
Unicode |
Unicode characters |
unicode(), u'hello', "world".encode('utf-8') |
Int |
Integer |
int(), 1, 55 |
Float |
Decimal precision integers |
float(), 1.0, .032 |
Bool |
Boolean values |
bool(), True, False |
List |
List of elements |
list(), [3, 'asd', True, 3] |
Dictionary |
Set of key:value pairs used to structure data |
dict(), {'element': 'Mn', 'Atomic Number': 25, 'Atomic Mass': 54.938} |
Set |
List of unique elements |
set(), [3, 4, 'hello'] |
Tuple |
Organized list of elements |
tuple(), (2, 'Hello World!', 55.6, ['element1']) |
File |
A file object |
open('write_output.txt', 'w') |
You will find that most of our scripts can be accomplished using only the standard data types Python offers. Before we take a look at one of the most common data types, strings, we will introduce comments.
Something that is always said, and can never be said enough, is to comment your code. In Python, comments are formed by any line beginning with the pound, or more recently known as the hashtag, # symbol. When Python encounters this symbol, it skips the remainder of the line and proceeds to the next line. For comments that span multiple lines, we can use three single or double quotes to mark the beginning and end of the comments rather than using a single pound symbol for every line. What follows are examples of types of comments in a file called comments.py. When running this script, we should only see 10 printed to the console as all comments are ignored:
# This is a comment
print(5 + 5) # This is an inline comment.
# Everything to the right of the # symbol
# does not get executed
"""We can use three quotes to create
multi-line comments."""
The output is as follows:
When this code is executed, we only see the preceding at the console.