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.
In [1]:
x = 1
type(x)
Out[1]:
Another convenient feature of Python integers is that by default, division up-casts to floating-point type:
In [2]:
5 / 2
Out[2]:
In [3]:
x = 0.000005
y = 5e-6
print(x == y)
In [4]:
x = 1400000.00
y = 1.4e6
print(x == y)
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]:
In [6]:
complex(1, 2)
Out[6]:
Alternatively, we can use the "j
" suffix in expressions to indicate the imaginary part:
In [7]:
1 + 2j
Out[7]:
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]:
In [10]:
c.imag # imaginary part
Out[10]:
In [11]:
c.conjugate() # complex conjugate
Out[11]:
In [12]:
abs(c) # magnitude, i.e. sqrt(c.real ** 2 + c.imag ** 2)
Out[12]:
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]:
In [15]:
# Make upper-case. See also str.lower()
response.upper()
Out[15]:
In [16]:
# Capitalize. See also str.title()
message.capitalize()
Out[16]:
In [17]:
# multiplication is multiple concatenation
5 * response
Out[17]:
In [18]:
# concatenation with +
message + response
Out[18]:
Q: What does split()
method do? What is the result?
In [19]:
message.split()
Out[19]:
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]:
Q: When you have a sequence of string, you can use join()
method:
In [22]:
l = notebook_name.split('-')
' '.join(l)
Out[22]:
You will know more about lists in Python in the next notebook.
In [23]:
# Access individual characters (zero-based indexing)
message[0:5]
Out[23]:
In [24]:
message[5:11]
Out[24]:
In [25]:
message[11:17] # note that spaces count as a character
Out[25]:
Strings are immutable. See for your self:
In [26]:
# s = "0123456789"
# s[0] = 1
In [27]:
e = 2.71828182845904590
'{:.4f} is a {}'.format(e, 'float')
Out[27]:
In [28]:
'{a:4.2f} blah-blah {b:05d}'.format(a=3.141516, b=123)
Out[28]:
In [29]:
type(None)
Out[29]:
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')
In [31]:
print(return_value)
In [32]:
result = (4 < 5)
result
Out[32]:
In [33]:
type(result)
Out[33]:
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)