fbpx

What is Data type in Python | Python Data Types example

0
(0)

Hello dear, today we will all read about the data types given in Python, so what are we waiting for, let’s start..

What is Data type in Python | Python Data Types example

Data types

Data types denote the type of data stored into variables. Like other programming languages Python has a set of built-in data types. Since everything is object in Python, every data type in Python is a class and each variable is an object of a class.

Data types classification:

Textstr
Numericint, float, complex
Sequencelist, tuple, range
Mappingdictionary
Setset, frozenset
Booleanbool
Binary typesbytes, bytearray, memoryview

Text

Text is a collection of characters otherwise called as string.String is represented using str data type. A set of characters enclosed within a set of single quotes(‘) or set of double quotes(“) is called string literal.Multiline strings are enclosed within triple quotes (”’ or “””)

Example 1
1
2
s='Hello world'
print(s)

Output:
Hello world
Example 2
1
2
s="abc*xyz#"
print(s)

Output:
abc*xyz#
Example 3
1
2
3
s="""Hi friends.
Happy programming."""
print(s)

Output:
Hi friends.
Happy programming.

 Strings are immutable

String is an object that is not changeable after creation. So it is called immutable object. The contents of string objects cannot be overwritten.

Example
1
2
3
s="""Hi friends.
Happy programming."""
s[3]='p'

Output:
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    s[3]='p'
TypeError: 'str' object does not support item assignment

 Slicing the strings

Slicing is the process of extracting a range of characters or substring from the given string.

Syntax:
string_name[starting index:ending index(not included)]
Example
1
2
s="Happy programming"
print(s[6:13])

Output:
program
Numeric data types

Numeric data types represented using int, float and complex and are called as Python Numbers.

Integers:

The numbers without fractional part (a decimal point and a set of digits after the decimal point) are called integers. The numbers 10,-100,234 are the examples of integers. Integer values can be of any length.

The limit on the length of integers is dependent on the limit of the memory in the computer.

Example
1
2
a = 1234567890123456789
print(a)

Output:

1234567890123456789


Floating point number

Floating point numbers are the numbers with fractional part (a decimal point and a set of digits after the decimal point), represented by ‘float’ data type and are 64-bit double precision values in all platforms. The maximum floating point value a variable can have is 1.8*10308 .

Example
1
2
pi=3.14
print(pi)

Output:

3.14


Complex number

Complex numbers are represented in the form a+bj, where a is real part and b is the imaginary part.

Example 1
1
2
c=1+2j
print(c)

Output:
(1+2j)
Example 2
1
2
3
4
a=1+2j
b=3+4j
c=a+b
print(c)

Output:

(4+6j)



How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply