Python Tutorials - Python Standard Types, Data Types in Python, How to Cast a type to another type in Python, Type casting in Python

Python Standard Types

Python Standard Types, Data Types in Python, How to Cast a type to another type in Python, Type casting in Python

The Python is a loosely typed language and there are no special name for data types like other programming languages like java, In Java, if we want to create a variable we must provide that a data type like int, double, String etc,but in python the variable automatically cast to assigned value type but we can say the python also categorize the variables into types. We now introduce you to the standard data types in Python.They include numbers and strings, or they are “containers,” or data structures, used to group together multiple Python objects. Before we introduce you to the main data types, it is worth first noting all Python objects have some inherent Boolean value.

Object Boolean Values

Like most other languages like java, c# etc, exactly two Boolean values can be expressed: True and False. All Python values can be represented as a Boolean value, regardless of their data values. For example, any numeric type equal to 0 is considered False while all nonzero (negative and positive) numeric values are True. Similarly, empty containers(like bool("")) are False while nonempty (bool(["hello",0]))containers are True. You can use the bool function to determine the Boolean value of any Python object; furthermore, True and False are legitimate values of their own and can be explicitly assigned as a variable’s value.

>>> process_executed = False
>>> bool(process_executed)
>>> bool(-3.78)
>>> bool(0.0)
>>> bool("")
>>> bool([None, 0])

The ABOVE examples and the output of bool should all make sense.In the Last example the bool() method have a container which has two values in it so it gives a True value as a result.


Python has two primary numeric types: int (for integer) and float (for real number). Python has only one integer type, int, as compare to many other languages that have multiple integer types. floats are double-precision real numbers as like other programming languages.The following are some examples of ints and floats as well as

>>> 2.56 + 3.45
>>> -5 - 2
>>> 2.1

As like other programming languages the addition in above example the result is as expected but what about in the last example?
in C the range of float is only 6 charector but in python it range increase to 16 characters.
Python Built-in Numeric Types

intsigned integer(no size limit)-4, 0, 0xA2E2, 0453, 67
floatDouble precision floating point numbers3.45, 2.3e+5, -9., -4.6e, 0.894 
complexComplex (Real + Imaginary Numbers)4+1j, .5-j, -34.3e+2-40j

Numeric Built-in and Factory Functions(Conversion or Casting)

Each of the numeric types in Python has a factory function that are used  to convert from one numeric type to another. In other words we can say it “conversion” and “casting,” but we don’t use those terms in Python because we are not changing the type of an existing object.We are returning a new object based on the original object. It is very simple to convert as telling int(12.34) to create a new integer object with value 12 and the fraction will terminate while float(12) returns 12.0.

Python also provides the range of  built-in functions that apply to numbers, such as round() to round () floats to a specified number of digits or abs() for the absolute value of a number.The following are a few examples of these and other built-ins functions:

>>> int(63.89)

>>> int('256')
>>> float(45)

>>> round(4.25, 1)

>>> ord('A')

>>> divmod(21, 4)
(5, 1)

>>> chr(97)

{ 0 comments... read them below or add one }

Post a Comment