1 Built-In Types: Simple Values

  • Now we will briefly go through the simple built-in Python types
  • built-in means that these objects are available in Python straight away, no need to import other packages
Python Scalar Types
Type Example Description
int x = 1 integers (i.e., whole numbers)
float x = 1.0 floating-point numbers (i.e., real numbers)
complex x = 1 + 2j Complex numbers (i.e., numbers with real and imaginary part)
bool x = True Boolean: True/False values
str x = 'abc' String: characters or text
NoneType x = None Special object indicating nulls

We’ll take a quick look at each of these in turn.

1.1 Integers

The most basic numerical type is the integer. Any number without a decimal point is an integer:

Code
x = 3
type(x)
int

Another convenient feature of Python integers is that by default, division up-casts to floating-point type:

Code
5 / 2
2.5

1.2 Floating-Point Numbers

The floating-point type can store fractional numbers. They can be defined either in standard decimal notation, or in exponential notation:

Code
x = 0.000005
y = 5e-6
print(x == y)
True
Code
x = 1400000.00
y = 1.4e6
print(x == y)
True

In the exponential notation, the e or E can be read “…times ten to the…”, so that 1.4e6 is interpreted as ~1.4 \times 10^6.

An integer can be explicitly converted to a float with the float constructor:

Code
float(1)
1.0

1.3 Complex Numbers

Complex numbers are numbers with real and imaginary (floating-point) parts. We’ve seen integers and real numbers before; we can use these to construct a complex number:

Code
complex(1, 2)
(1+2j)

Alternatively, we can use the “j” suffix in expressions to indicate the imaginary part:

Code
1 + 2j
(1+2j)

Complex numbers have a variety of interesting attributes and methods, which we’ll briefly demonstrate here:

Code
c = 3 + 4j
Code
c.real  # real part
3.0
Code
c.imag  # imaginary part
4.0
Code
c.conjugate()  # complex conjugate
(3-4j)
Code
abs(c)  # magnitude, i.e. sqrt(c.real ** 2 + c.imag ** 2)
5.0

1.4 String Type

Strings in Python are created with single or double quotes:

Code
message = "what do you like?"
response = 'spam'

Python has many extremely useful string functions and methods; here are a few of them:

Code
# length of string
len(response)
4
Code
# Make upper-case. See also str.lower()
response.upper()
'SPAM'
Code
# Capitalize. See also str.title()
message.capitalize()
'What do you like?'
Code
# multiplication is multiple concatenation
5 * response
'spamspamspamspamspam'
Code
# concatenation with +
message + response
'what do you like?spam'

Q: What does split() method do? What is the result?

Code
message.split()
['what', 'do', 'you', 'like?']

Q: Now try to split this notebook’s name into words. What argument you have to pass to split()?

Code
notebook_name = 'Built-in-Scalar-Types.ipynb'
Code
notebook_name.split('-')
['Built', 'in', 'Scalar', 'Types.ipynb']

Q: When you have a sequence of string, you can use join() method:

Code
l = notebook_name.split('-')
' '.join(l)
'Built in Scalar Types.ipynb'

1.4.0.1 Indexing

Code
# Access individual characters (zero-based indexing)
message[0:5]
'what '
Code
message[5:11]
'do you'
Code
message[11:17] # note that spaces count as a character
' like?'

Strings are immutable

Code
s = "0123456789"
s[0] = "1"
TypeError: 'str' object does not support item assignment

1.5 None Type

Python includes a special type, the NoneType, which has only a single possible value: None. For example:

Code
type(None)
NoneType

You’ll see None used in many places, but perhaps most commonly it is used as the default return value of a function. For example, the print() function in Python 3 does not return anything, but we can still catch its value:

Code
return_value = print('abc')
abc
Code
print(return_value)
None

1.6 Boolean Type

The Boolean type is a simple type with two possible values: True and False, and is returned by comparison operators discussed previously:

Code
result = (4 > 5)
result
False
Code
type(result)
bool

Keep in mind that the Boolean values are case-sensitive: unlike some other languages, True and False must be capitalized!

Code
print(True, False)
True False

1.7 References

A Whirlwind Tour of Python by Jake VanderPlas (O’Reilly). Copyright 2016 O’Reilly Media, Inc., 978-1-491-96465-1 ## License Open MIT License, adapted from the Python Course 2020 developed by Cefas (Centre for Environment, Fisheries and Aquaculture Science) and Python Course for SENSE (Centre for Satellite Data in Environmental Science) PhD students 2020.