Integers
In Python 3, there is effectively no limit to how long an integer value can be.
>>> print(123123123123123123123123123123123123123123123123 + 1) 123123123123123123123123123123123123123123123124 >>> type(10) <class 'int'>
Binary, Octal, Hexa decimal
>>> print(0o10) 8 >>> print(0x10) 16 >>> print(0b10) 2
There is no ‘long integer‘ in Python 3 anymore.
Float
float values are specified with a decimal point.
>>> 4.2 4.2 >>> type(4.2) <class 'float'> >>> 4. 4.0 >>> .2 0.2
Complex numbers
Complex numbers are specified as <real part>+<imaginary part>j. For example:
>>> 2+3j (2+3j) >>> type(2+3j) <class 'complex'>
Note: Python stores the real and imaginary parts of a complex number as floats.
a = 5 + 2j print(a.real) >>> 5.0 print(a.imag) >>> 2.0
Most of the time Python will do variable conversion automatically. You can also use Python conversion functions (int(), long(), float(), complex()) to convert data from one type to another. In addition, the type function returns information about how your data is stored within a variable.
There is no ‘long integer‘ in Python 3 anymore.
Python code example for different data types :
"""
@author: rdayala
link - https://www.techbeamers.com/python-data-types-learn-basic-advanced/
"""
# booleans - True or False
# ---------------------------
isGameFinished = False
print(isGameFinished)
print(type(isGameFinished)) # output : <class 'bool'>
# In few cases, the boolean constants “True” and “False” might also act as numbers.
A, B = True + 0, False + 0
print(A, B) # output : 1 0
# Numbers - int, float, and complex
# ------------------------------------
# Python has a built-in function type() to determine the data type of a variable or the value.
num = 2
print("The number (", num, ") is of type", type(num))
# output : The number ( 2 ) is of type <class 'int'>
num = 3.0
print("The number (", num, ") is of type", type(num))
# output : The number ( 3.0 ) is of type <class 'float'>
num = 3+5j
print("The number ", num, " is of type", type(num))
# output : The number (3+5j) is of type <class 'complex'>
# built-in function isinstance() - for testing the type of an object.
# The isinstance() functions checks whether a number belongs to a
# particular class and returns True or False based on the result.
print("The number ", num, " is complex number?", isinstance(3+5j, complex))
# output : The number (3+5j) is complex number? True
# to form a complex number
complex_number = complex(1.2,5)
print(complex_number) # output : (1.2+5j)
# Integers in Python don’t have any size limitation as long as
# the required memory is available.
num = 1234567890
print(num) # output : 1234567890
# the number of bits required to represent an integer in binary
print(num.bit_length()) # output : 31
# There is no "long int" in Python3 anymore. There is only one "int" type,
# which contains both "int" and "long" from Python2.
x = 3 + 4j
y = 2 - 3j
z = x + y
print(z) # output : (5+1j)
# octal number
a = 0o10
print(a) # output : 8
# hex number
hex_number = 0x1C
print(hex_number) # output : 28
x = 0b1010
print(x) # output : 10
# The functions hex, bin, oct can be used to convert an integer number
# into the corresponding string representation of the integer number
x = hex(28)
print(x, type(x)) # output : 0x1c <class 'str'>
x = bin(7)
print(x) # output : 0b111
# The function int can be used to convert string to an integer
x = int(0x1C)
print(x) # output : 28
# If the string you want to convert into int belongs to a different number base
# other than base 10, then you can specify that base for that conversion.
x = int("0x1C", base=16)
print(x) # output : 28
x = int("28")
print(x) # output : 28
# float values - max precision is 15
import sys
print(sys.float_info)
# sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,
# min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
# mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
# https://appdividend.com/2019/02/03/how-to-convert-python-string-to-int-and-int-to-string/
# Strings
# -------------
str1 = 'A string wrapped in single quotes'
print(str1)
str1 = "A string enclosed within double quotes"
print(str1)
# multi-line strings
# multi-line strings which require a triple quotation mark
str1 = """A multiline string
starts and ends with
a triple quotation mark."""
print(str1)
# the strings in Python are immutable.
A = 'Python3'
print(id(A)) # output : 2422970856368
B = A
print(id(B)) # output : 2422970856368
# Python 3 strings are all Unicode (UTF-8).
print(type('Python String')) # output : <class 'str'>
print(type(u'Python Unicode String')) # output : <class 'str'>
# type conversion - integer to string - int to string
pincode = 500076
address = "ABCD Park, XYZ State, Pincode : " + str(pincode)
print(address) # output : ABCD Park, XYZ State, Pincode : 500076
# ************
# the other data types are : Lists, Tuples, Sets, Dictionaries