In programming, the data type is an important concept, and variables are used to store different types of data.
A variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.
Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
| Data | Type |
|---|---|
| Text | str |
| Numeric | int, float, complex |
| Squence | list, tuple, range, daterange |
| Mapping | dict |
| Set | set, frozenset |
| Boolean | bool |
| Binary | bytes, bytearray, memoryview |
| Object | date, time, datetime... |
| Classes | Custom Types |
| Modules | Specialized Datatypes |
| None | class NoneType object |
strDateType = "Hello World" #str
intDataType = 30 #int
floatDataType = 20.6 #float
complexDataType = 1j #complex
listDataType = ["apple", "banana", "cherry"] #list
tupleDataType = ("apple", "banana", "cherry") #tuple
rangeDataType = range(6) #range
dictDataType = {"name" : "John", "age" : 40} #dict
setDataType = {"apple", "banana", "cherry"} #set
frozesetDataType = frozenset({"apple", "banana", "cherry"}) #frozenset
boolDataType = True #bool
bytesDataType = b"Hello" #bytes
bytearrayDataType = bytearray(5) #bytearray
memoryviewDataType = memoryview(bytes(5)) #memoryview
|
To verify the type of any object in Python, use the type() function:
Integer is a number
print(1 + 2) #Output: 3 print(100 - 200) #Output: -100 print(10 * 10000) #Output: 100000 print(0) #Output: 0 print(type(1 + 2)) #Output: < class 'int'> print(type(100 - 200)) #Output: < class 'int'> print(type(10 * 10000)) #Output: < class 'int'> print(0) #Output: < class 'int'> |
float is a decimal numbers
float requires more space than int for storing a values
print(1.0 + 2) #Output: 2.0 #Python print larger data types to avoid the loss of data print(100 / 200) #Output: 0.5 print(10.43) #Output: 10.43 print(type(1 / 2)) #Output: < class 'float'> print(type(100 / 200)) #Output: < class 'float'> print(type(10.43)) #Output: < class 'float'> |
Strings in python are arrays of bytes representing Unicode characters
A single character is simply a string with a length of 1
x = "A"
print(x[1])
Output: A
x = "ASTRING"
print(x[1])
Output: A
Either you can create a string using single quotes or double quotes
user_name = "Michael" #Output: Michael print(type(user_name)) #Output: < class 'str'> user_name = 'Michael' #Output:Michael print(type(user_name)) #Output: < class 'str'> |
For Multiple line String use three single quotes '''
user_name = ''' Michael Jackson William''' |
first_name = "Sachin" last_name = "Tendulkar" full_name = first_name + " " + last_name #Output: Sachin Tendulkar |
For more detail, please refer Python Strings
print(True) #Output: Trie print(type(True)) #Output: |
None is used to define a null value. It is not the same as an empty string, False, or a zero. It is a data type of the class NoneType object.
Assigning a value of None to a variable is one way to reset it to its original, empty state.
Python function may returns None when it does not return anything.
print(type(None)) #Output:< Class 'NoneType' > var_name = None print(var_name) #Output:None |
Converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
Python has two types of type conversion.
num_integer = 123
num_float = 1.23
num_new = num_integer + num_float
print("datatype of num_integer:",type(num_integer))
print("datatype of num_float:",type(num_float))
print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
|
num_integer = 123
num_string = "456"
print("Data type of num_integer:",type(num_integer))
print("Data type of num_string before Type Casting:",type(num_string))
num_str = int(num_string)
print("Data type of num_str after Type Casting:",type(num_string))
num_sum = num_integer + num_str
print("Sum of num_integer and num_string:",num_sum)
print("Data type of the sum:",type(num_sum))
|
# Empty string always returns False
print(bool("")) #Output: False
# Non-Empty strings always returns True
print(bool("some characters")) #Output: True
# Integer values other than 0 returns True
print(bool(123)) #Output: True
# 0 returns False
print(bool(0)) #Output: False
#from String Type 'True' to Boolean Type 'True'
print(bool('True')) #Output: True
|
Other data types will see in detail in the upcoming sections
If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!