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)
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)
In [29]:
a = 10
b = 15
print a + b , a - b , a * b , a / b
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
In [30]:
print Content[0] # this will print the first character
print Content[1:5] # this will print the substring
In [31]:
b1 = True
b2 = False
b3 = (b1 and b2) or (b1 or b2)
print b1,b2,b3
In [32]:
not True
Out[32]:
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)
In [ ]: