Data Types in Python

Integer Numbers (int)

Integer numbers abbreviated int are essentially whole numbers they can be input using the numbers 0,1,2,3,4,5,6,7,8 and 9.

In [1]:
1
Out[1]:
1
In [2]:
10
Out[2]:
10

Floating Point Numbers (float)

Floating point numbers abbreviated float are numbers with a decimal component. The full stop is used . to denote the decimal point.

In [3]:
1.1
Out[3]:
1.1
In [4]:
10.10
Out[4]:
10.1

Boolean Values (bool)

Boolean values abbreviated bool are either True or False. This numerically equates to 1 or 0. Note the capitalization and color-coding.

In [5]:
True
Out[5]:
True
In [6]:
False
Out[6]:
False
In [7]:
true
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-724ba28f4a9a> in <module>
----> 1 true

NameError: name 'true' is not defined
In [8]:
false
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-b73d74fcede9> in <module>
----> 1 false

NameError: name 'false' is not defined

A String of Characters (str)

A string of characters otherwise known as a string or abbreviated as str, can be used to store text variables. This can be lower abc,... and upper case ABC,..., the space , underscore _, comma , and full stop .. The str is enclosed in quotation marks either double " (default) or single '. Note the color-coding of the str in brown.

In [9]:
"hello"
Out[9]:
'hello'
In [10]:
'hello'
Out[10]:
'hello'
In [11]:
"hello world"
Out[11]:
'hello world'

We need to use quotation marks to enclose a str however sometimes we will also want to embed quotation marks into the str itself. Let's see what happens when we attempt this.

In [12]:
"She said "Hello" to him."
  File "<ipython-input-12-2568471f90f8>", line 1
    "She said "Hello" to him."
               ^
SyntaxError: invalid syntax

Note the color coding of the str in brown. We can see the quotations around the word Hello end the str She Said and start another str to him..

When there is only one type of quotation used within the str, the other quotation mark can be used to enclose the str.

In [13]:
'She said "Hello" to him.'
Out[13]:
'She said "Hello" to him.'
In [14]:
"Philip's Python Notes"
Out[14]:
"Philip's Python Notes"

In some cases however we might want to create a str which uses both single ' (or an apostrophe) and double " quotation marks. Note again the color-coding.

In [15]:
'She said "I like Philip's Python Notes" to him.'
  File "<ipython-input-15-6968bf7ff30c>", line 1
    'She said "I like Philip's Python Notes" to him.'
                             ^
SyntaxError: invalid syntax

The quotation mark " is an escape character. An escape character can be inserted in Python by using the backslash \ followed by the character to be inserted. This is typically used for the following.

Code Name Result
\\' Single Quote \'
\\" Double Quote \"
\t Tab TAB
\v Vertical Tab VT
\r Carriage Return CR
\f Form Feed FF
\n New Line CR+FF
\b Backspace BS
In [16]:
"She said \"I like Philip\'s Python Notes\" to him."
Out[16]:
'She said "I like Philip\'s Python Notes" to him.'

The print function

We can use the print function to print the output of a str. Functions are called with parenthesis ( ). The parenthesis enclose the input argument to be printed to output of a cell.

In [17]:
print("She said \"I like Philip\'s Python Notes\" to him.")
She said "I like Philip's Python Notes" to him.

Historically computers originated from the typewritters. In a typewritter a new line involved two operations, a carriage return, moving the ink carriage back to the left hand side of the paper.

In [18]:
print("      World\rHello")
Hello World

And a form feed which moves the paper form down by the length of a line. \f doesn't seem to work correctly in the print function but conceptually the output of print("Hello World\fHello World") should essentially look like this.

In [19]:
print("Hello World\n           Hello World")
Hello World
           Hello World

The new line is both a carriage return and a form feed.

In [20]:
print("Hello World\nHello World")
Hello World
Hello World

A tab is typically used to indent text four spaces to start a new paragraph and a new line is used to start text on a new line. The backspace is used to delete a character and go back one space.

In [21]:
print("\tHello World\nHello World\nHello\bWorld")
	Hello World
Hello World
HellWorld

The vertical tab does not work correctly with the print function.

In [22]:
print("\tHello World\vHello World")
	Hello WorldHello World

The type function

We have seen the print function, which used parenthesis ( ) to enclose its input argument the object to be printed. The type function has a similar form and returns the data type of an object.

In [23]:
type(1)
Out[23]:
int
In [24]:
type(1.1)
Out[24]:
float
In [25]:
type(True)
Out[25]:
bool
In [26]:
type("hello")
Out[26]:
str