
Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
Different Data Types in Category: Python by amit
🕙 Posted on 2023-06-23 at 01:14:11 Read in Hindi ...
More on Data Types
Like PHP and JavaScript, Python is also a loose data-type programming language. It means that when you declare a variable name, and assign some value to it, then data-type of that value becomes the data-type of the variable name. Python has no command for declaring a variable. A variable name can be short (for example, x, y, z, etc.) or descriptive, as explained in previous page.
# you can use following command-line/
# in Windows: py new.py
# in Mac/Linux: python3.10 new.py
# Make sure that number of variables declared
# matches the number of respective values,
# otherwise an ERROR will be shown.
x,y,z = 2,3,4 # many variables can be declared in single line
print(x,y,z) # outputs 2 3 4
x = y = z = 24 # one value is assigned to many variables at one time.
print(x,y,z) # outputs 24 24 24
In following examples, you may think that value assigned to a variable name is of one data-type, but it can be of other data-type. You can see herein below that variable name k and l are not of tuple data-type, but they are respectively int (integer) and str (string) data-types. Strings (series of characters), Lists and Tuples are Sequence Data Types. Lists and tuples contain collection of items of different data types.
#1) Boolean data type: True False
a = True
print( type(a), a ) # outputs <class 'bool'> True
b = False
print( type(b), b ) # outputs <class 'bool'> False
#2) Numeric data type: int float complex
c = 12
print( type(c), c ) # outputs <class 'int'> 12
d = 12.5
print( type(d), d ) # outputs <class 'float'> 12.5
e = 12j # j is post-fixed after number to represent imaginary complex number.
print( type(e), e ) # outputs <class 'complex'> 12j
#3) String (immutable) data type: str
f = '12' # a pair of single quotes or a pair of double quotes is used to enclose characters
print( type(f), f ) # outputs <class 'str'> 12
#4) List (mutable array) data type: list range
g = [12] # This list contain only one item.
print( type(g), g ) # outputs <class 'list'> [12]
h = range(12)
print( type(h), h ) # outputs <class 'range'> range(0, 12)
i = list(range(12))
print( type(i), i ) # outputs <class 'list'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
#5) Tuple (immutable array) data type: tuple
j = () # j = (12, 24, 36)
print( type(j), j ) # outputs <class 'tuple'> ()
k = (12)
print( type(k), k ) # outputs <class 'int'> 12
l = ('12')
print( type(l), l ) # outputs <class 'str'> 12
#6) Dictionary (key:value pair) data type: dict
m = {}
print( type(m), m ) # outputs <class 'dict'> {}
#7) Set (and Frozenset) data type: set frozenset
n = {12} # values of items in a set (collection of unordered elements) cannot be same.
print( type(n), n ) # outputs <class 'set'> {12}
#8) Binary data types: bytes, bytearray, memoryview
o = b"Hello"
print( type(o), o ) # outputs <class 'bytes'> b"Hello"
p = bytearray(12)
print( type(p), p ) # outputs <class 'bytearray'> bytearray(b'\x00\x00\x00\x00
q = memoryview(bytes(12))
print( type(q), q ) # outputs <class 'memoryview'> <memory at 0x000002D1FEFADA80>
Type Casting
Values to be assigned to variable names can be created and type-casted (converted), with bool(), str(), int(), float(), complex(), list(), range(), tuple(), dict(), set(), frozenset(), bytes(), bytearray(), and memoryview() built-in constructor functions. You cannot convert complex numbers into another number type.
In addition to four scalar data-types, there are many more unique data-types in Python programming language (similar or other than those in PHP, JavaScript, and other C based programming languages). In next pages you will learn more about each of these data-types and type-casting in Python.
Leave a Comment:
Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤
Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇
