Primitive Data Type in Python

Maneesha Nirman
CodeX
Published in
4 min readMar 31, 2023

--

The most basic data types used to represent different data during programming are known as fundamental or primitive data types. Primitive data types use to represent single elements.

There are two types of Data types in python.

1. Primitive - Primitive types are used to represent single elements.

2. Compound - Compound types are used to represent multiple elements.

There are four types of primitives. Those are,

· Integer

· Float

· String

· Boolean

  1. Integer (int) — Numbers without Decimals known as Integers. It can contain negative and positive values. ex-: 1, -1, 2, -2
  2. Float (float)— Numbers with Decimals known as float. It can contain negative and positive values. ex-: 1.0, -1.0, 2.05, -2.005
  3. String (str)— Any kind of textual data is known as a String. It means character, Sysmobl, word, or sentence. etc. We can use double quotations (“ ”) or single quotations(‘’) to represent String. But we usually use “ ” to represent String. Space is also Considered a String. The digits written inside double quotations or single quotations are considered as String. ex-: ‘Sample’, “Sample”, ”@#$”, ‘@#$’, “123456”, ’123456’
  4. Boolean (bool) — Boolean is a value that returns with logical and relational operations. Boolean consists of only two values. Python is a case-sensitive language. So you must use the first letter as capital in boolean values. (True/False)

In python, We don't want to define a data type for a variable. Python is strong enough to understand the data types of a variable.

type()

The type() function is used to check the data type of a value. This is a function already defined in python.

type(10)

isinstance()

isinstance() is a function that provides by the python-dev team to verify the data type of a value.

isinstance(10,int) 
isinstance(10.0,float)
isinstance(20,float)
isinstance("Sample",str)
isinstance(True,bool)

output-:

True

True

False

True

True

Casting

Simply casting is converting a variable particular data type into another data type. These conversions can be implicit (automatically interpreted) or explicit (using built-in functions).

Integer

We can cast variables into Integer using the int() function.

str into int

int("5") 
int("10")

What is your idea about the below code?

int("hey") -> Error
int("123Hey") -> Error
int("123@") -> Error

Usually, we consider int values as digits. So we cannot cast values into Integer when the values include symbols or letters.

float into int

int(10.0)
int(20.2)
int(30.5)

bool into int

int(True)
int(False)

This is very important. bool consists of only 2 values True and False. So the output is given below.

int(True) — > 1

int(False) — > 0

Float

We can cast variables into Float using the float() function.

str into float

float("10")
float("20")
float("30")

What is your idea about the below code?

float("hey") -> Error
float("123Hey") -> Error
float("123@") -> Error

Usually, we consider float values as digits with decimal points. So we cannot cast values into Float when the values include symbols or letters.

int into float

float(10)
float(20)
float(30)

bool into float

float(True)
float(False)

This is very important. bool consists of only 2 values True and False. So the output is given below.

float(True) → 1.0

float(False) → 0.0

Boolean

We can cast variables into a boolean using the bool() function.

str into bool

What is the output of the below code?

bool("False")

This returns True as output. If we pass any String value to cast into bool it returns True.

But there is a way to get output as False when casting str into a bool.

bool("")

If no parameter is passed, then False is returned by default.

int into bool

bool(1) -> True
bool(3) -> True
bool(100) -> True
------------------------------------------------
bool(0) -> False

In all cases except for 0 returns True as output.

float into bool

bool(1.0) -> True
bool(3.3) -> True
bool(100.3) -> True
------------------------------------------------
bool(0.0) -> False

In all cases except for 0.0 returns True as output.

String

We can cast variables into String using the str() function.

int into str

str(1)
str(20)
str(1000)

float into str

str(10.5)
str(20.6)
str(36.2)

bool into str

str(True)
str(False)

There is one special point you must keep in mind.

float("10")

Actually, in the above casting, there are 2 operations.

  1. The string value should be converted into an Integer value
  2. Converted the Integer value should be converted into a float value.

This process is totally fine. But consider the below code.

int("10.5")

This is a process that is the opposite of the above scenario. But Keep in mind,

“When float value inside single/double quotations, it cannot be cast into int”. This is a very special situation in python.

Traceback (most recent call last):
File "<string>", line 2, in <module>
ValueError: invalid literal for int() with base 10: '10.5'
>

This is related to the fundamentals of Python. So hope you enjoy my articles. Practice those by yourself.

Stay tuned… Thank you!

--

--

Maneesha Nirman
CodeX

Associate Engineer @ Virtusa. Bsc Hons. Software Engineering, Oracle Certified Engineer