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.

Integers

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


In [1]:
x = 1
type(x)


Out[1]:
int

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


In [2]:
5 / 2


Out[2]:
2.5

Floating-Point Numbers

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


In [3]:
x = 0.000005
y = 5e-6
print(x == y)


True

In [4]:
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:


In [5]:
float(1)


Out[5]:
1.0

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:


In [6]:
complex(1, 2)


Out[6]:
(1+2j)

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


In [7]:
1 + 2j


Out[7]:
(1+2j)

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


In [8]:
c = 3 + 4j

In [9]:
c.real  # real part


Out[9]:
3.0

In [10]:
c.imag  # imaginary part


Out[10]:
4.0

In [11]:
c.conjugate()  # complex conjugate


Out[11]:
(3-4j)

In [12]:
abs(c)  # magnitude, i.e. sqrt(c.real ** 2 + c.imag ** 2)


Out[12]:
5.0

String Type

Strings in Python are created with single or double quotes:


In [13]:
message = "what do you like?"
response = 'spam'

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


In [14]:
# length of string
len(response)


Out[14]:
4

In [15]:
# Make upper-case. See also str.lower()
response.upper()


Out[15]:
'SPAM'

In [16]:
# Capitalize. See also str.title()
message.capitalize()


Out[16]:
'What do you like?'

In [17]:
# multiplication is multiple concatenation
5 * response


Out[17]:
'spamspamspamspamspam'

In [18]:
# concatenation with +
message + response


Out[18]:
'what do you like?spam'

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


In [19]:
message.split()


Out[19]:
['what', 'do', 'you', 'like?']

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


In [20]:
notebook_name = '05-Built-in-Scalar-Types.ipynb'

In [21]:
notebook_name.split()


Out[21]:
['05-Built-in-Scalar-Types.ipynb']

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


In [22]:
l = notebook_name.split('-')
' '.join(l)


Out[22]:
'05 Built in Scalar Types.ipynb'

You will know more about lists in Python in the next notebook.

Indexing


In [23]:
# Access individual characters (zero-based indexing)
message[0:5]


Out[23]:
'what '

In [24]:
message[5:11]


Out[24]:
'do you'

In [25]:
message[11:17] # note that spaces count as a character


Out[25]:
' like?'

Strings are immutable. See for your self:


In [26]:
# s = "0123456789"
# s[0] = 1

String formatting

  • by order

In [27]:
e = 2.71828182845904590
'{:.4f} is a {}'.format(e, 'float')


Out[27]:
'2.7183 is a float'
  • by keys

In [28]:
'{a:4.2f} blah-blah {b:05d}'.format(a=3.141516, b=123)


Out[28]:
'3.14 blah-blah 00123'

None Type

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


In [29]:
type(None)


Out[29]:
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:


In [30]:
return_value = print('abc')


abc

In [31]:
print(return_value)


None

Boolean Type

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


In [32]:
result = (4 < 5)
result


Out[32]:
True

In [33]:
type(result)


Out[33]:
bool

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


In [34]:
print(True, False)


True False

References

A Whirlwind Tour of Python by Jake VanderPlas (O’Reilly). Copyright 2016 O’Reilly Media, Inc., 978-1-491-96465-1