Python Intrinsic Data Type

Overview

Python has these common built-in Data Types: https://docs.python.org/2/library/stdtypes.html

Python will set the variable type once it is assigned. You can change the variable type if the new variable value is set to another type. For example:


In [3]:
var = 321 # Create an integer assignment.
print type(var)
var = 'HKU' # The new `var` variable is now become a string type.
print type(var)


<type 'int'>
<type 'str'>

Numeric

Numeric Types are basic data storage in nearly every programming languages. In python, they can be : int, long, float and complex.

Type Format Description
int a = 10 Signed Integer
long a = 345L (L) Long integers
float a = 45.67 (.) Floating point real values
complex a = 3.14j (j) Complex number, Python use j to denote the imaginary part.

Python numbers variables are created by the standard assign method:


In [20]:
a = 68
print a,"is a",type(a)

b = 32385848583853453453255459395439L
print b,"is a",type(b)

c = 45.67
print c,"is a",type(c)

d = 3.14j
print d,"is a",type(d)


68 is a <type 'int'>
32385848583853453453255459395439 is a <type 'long'>
45.67 is a <type 'float'>
3.14j is a <type 'complex'>

Arithematic

Basic arithmatic functions You can print different variables at the same time with comma-separated, and the different arthimatic operations are showned


In [29]:
a = 10
b = 15
print a + b , a - b , a * b , a / b


25 -5 150 0

String

String types are created by enclosing characters in quotes. Single quotes ' double quotes " can be used to denote a string in one-line. The triple quotes """ can define a multi-line string. They shall be in pairs.


In [26]:
Name = 'Stephen Ng'
Subject = "Lunar eclipse open house today"
Email = "ncy@astro.physics.hku.hk"
Content='''There will be a lunar eclipse observable in Hong Kong on Jan 31 starting at 19:48pm. The Department will arrange an open house for the observatory at the roof from 7pm.
'''

In [28]:
print "from:", Name, '<'+Email+'>'
print "subject:", Subject
print "-------------------------------------------"
print Content


from: Stephen Ng <ncy@astro.physics.hku.hk>
subject: Lunar eclipse open house today
-------------------------------------------
There will be a lunar eclipse observable in Hong Kong on Jan 31 starting at 19:48pm. The Department will arrange an open house for the observatory at the roof from 7pm.


In [30]:
print Content[0] # this will print the first character
print Content[1:5] # this will print the substring


T
here

Boolean

Boolean


In [31]:
b1 = True
b2 = False
b3 = (b1 and b2) or (b1 or b2)
print b1,b2,b3


True False True

In [32]:
not True


Out[32]:
False

More on Integer

Because Python has arbitrarily large integers, there will not be integer overflow happened. An an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of bits – either larger than the maximum or lower than the minimum representable value. Integer can also be defined using octal or hexadecimial notation, theyare the same thing from the computer point-of-view


In [5]:
# Octal integers
a = 01
b = 027
c = 06645
print a,"is a",type(a)

# Hexadecimal integers
a = 0x1
b = 0x17
c = 0xDA5 
print b,"is a",type(b)


1 is a <type 'int'>
23 is a <type 'int'>

In [ ]: