Data types in python

rhythmsaha

Rhythm Saha

Posted on May 25, 2022

Data types in python

Today we are goint to discuss about the basic datatypes in python
lets discuss

What is Data type

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

What are the basic datatypes in python ?

  • int
  • float
  • string
  • boolean

Int is type of integer numbers without decimal points, it can be less than or greter than orequal to zero.

if we declare a variable with integer values its type will be int,

a = 10;
b = 20;

#type of a and b is int
Enter fullscreen mode Exit fullscreen mode

float is type of numbers with decimal points, it can be less than or greter than orequal to zero.

if we assign a number with decimal points to a variable, its type will be float,

a = 10.5;
b = 20.4;
c = 3.14

#type of a and b and c is float
Enter fullscreen mode Exit fullscreen mode

string is type of sentences or phrase declared and wrapped within single or double quotes. When we display a text with the print() function the output itself is a string.

a = "hello world"
Enter fullscreen mode Exit fullscreen mode

here the type of a is string.

a string is a sequence of characters, we can calculate the total number of characters present in a string.
to calculate the total characters present in a string python has a built-in function len()

total_Characters = len("hello world")
print(total_characters)
Enter fullscreen mode Exit fullscreen mode
output: 11
Enter fullscreen mode Exit fullscreen mode

We can pull out specific character from a string, its called subscripting.

text = "hello world"
print(text[0])

# text[0] means the the first characters of a text string,
in python or any other programming language 
the counting or first index starts with 0 (zero)
Enter fullscreen mode Exit fullscreen mode
output: h
Enter fullscreen mode Exit fullscreen mode

Boolean type of variable holds only two values True or False we use booleans to check if any specific expression is true or false. Its used in conditonal programming.

first_answer = True
second_answer = False

Enter fullscreen mode Exit fullscreen mode

here the first_answer holds a Boolean type value True and the second_answer holds the False value

How to check type of variables or values

to check type of variables python has a built-in function type()

a = 100
b = 10.50
c = "i am a string"
d = True

print("The type of a is", type(a))
print("The type of b is", type(b))
print("The type of c is", type(c))
print("The type of d is", type(d))
Enter fullscreen mode Exit fullscreen mode
output:
The type of a is <class 'int'>
The type of b is <class 'float'>
The type of c is <class 'str'>
The type of d is <class 'bool'>
Enter fullscreen mode Exit fullscreen mode

If you like the content and want to get updates on more post like that please follow me @rhythmsaha

here are my social profiles

💖 💪 🙅 🚩
rhythmsaha
Rhythm Saha

Posted on May 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Data types in python
python Data types in python

May 25, 2022