Does python have data types?

Python Data Types

Although we don’t need to declare a type for Python variables, a value does have a type. This data is crucial to the interpreter. Python supports the subsequent Python information types.

1.Python Numbers

Python has four numeric data types.

1.1. Int

int stands for integer. This Python Data Type holds signed integers. We can use the kind() feature to locate which elegance it belongs to.

>>> a= 10
>>> type(a)
<class ‘int’>

An integer may be of any length, with the most effective predicament being the to be had memory.

1.2. Float

This Python Data Type holds floating-point actual values. An int can simplest shop the variety 4, but go with the float can shore 4.25 if you want.

>>> a=4.5
>>> type(a)
<class ‘float’>

1.3. Long

This Python Data type holds a long integer of limitless length. But this assemble does no longer exist in Python 3.X.

1.4. complex

This Python Data kind holds a complicated number. A complex range seems like this: a+bj Here, a and b are the actual elements of the range, and j is imaginary.

>>> a=4+5j
>>> type(a)
<class ‘complex’>

Use the isinstance() function to inform if Python variables belong to a particular magnificence. It takes two parameters- the variable/price, and the class.

>>> print(isinstance(a,complex))

True It’s time to recognise the distinctive insights of Python numbers.

2. Strings

A string is a sequence of characters. Python does no longer have a char information kind, not like C++ or Java. You can delimit a string the usage of single prices or double-quotes.\

>>> city='Pune'
>>> city

'Pune'

2.1. Spanning a String Across Lines

To span a string across a couple of lines, you may use triple quotes.

>>> var="""If
    only"""
>>> var
‘If\n\tonly’

>>> print(var)
If
Only

>>> """If
only"""
‘If\n\tonly’

As you can see, the quotes preserved the formatting (\n is the escape sequence for newline, \t is for tab).

2.2. Displaying Part of a String

You can display a man or woman from a string the usage of its index inside the string. Remember, indexing begins with 0.

>>> lesson='Corona'
>>> lesson[0]
‘C’
>>> lesson[2:5]
‘ron’

You can also display a burst of characters in a string using slicing operator [].

2.3. String Formatters

String formatters permit us to print characters and values at once. You can use the % operator.

>>> x=5;
>>> book="Data Science"
>>> print("I just bought %s book of %s" % (x, book))

Or you can use the format method.

>>> print("I just bought{0}  book of {1}".format(x, book))
>>> print("I just bought  {x} book of {book}".format(x=3, printer="Data Science"))

A third alternative is to apply f-strings.

>>> print(f"I just bought {x} book of {book}")

2.4. String Concatenation

>>> a='20'
>>> print(a+a)
2020

You can concatenate(join) strings. However, you can’t concatenate values of different types.

>>> print('20'+20)
Traceback (most recent call last):
File “<pyshell#89>”, line 1, in <module>;
print(’20’+20)
TypeError: must be str, not int

3. Lists

A listing is a group of values. Remember, it may contain different styles of values. To outline a list, you need to put values separated with commas in rectangular brackets.There is no need to declare a type for a list either.

>>> city=['Mumbai','Pune',4,5,6,]
>>> city
['Mumbai','Pune',4, 5, 6]

 3.1. Slicing a List

You can slice a listing the way you’d slice a string- with the reducing operator.

>>> city[1:3]
[‘Pune’, 4]

Indexing for a list starts offevolved with 0, like for a string. A Python doesn’t have arrays.

3.2. Length of a List

Python supports an in built characteristic to calculate the duration of a list.

>>> len(city)
5

3.3. Reassigning Elements of a List

A listing is mutable. This means that you could reassign elements later on.

>>> city[2]='Nagpur'
>>> city
['Mumbai','Pune','Nagpur',4, 5, 6]

3.4.Iterating at the List

To iterate over the list we will use the for loop. By iterating, we are able to access each detail one by one which could be very helpful while we want to perform some operations on each list.

nums = [1,2,4,5]
for n in nums:
   print(n)
#output
1
2
4
5

3.5. Multidimensional Lists

A list might also have more than one dimension.

>>> a=[[1,2,3],['Mumbai','Pune','Nagpur']]
>>> a
[[1, 2, 3], ['Mumbai','Pune','Nagpur']]

4. Python Tuples

A tuple is like a list. You claim it the use of parentheses instead.

>>> subjects=('Machine Learning','Database','Algorithm')
>>> subjects
('Machine Learning','Database','Algorithm')

4.1. Accessing and Slicing a Tuple

You get right of entry to a tuple the equal way as you’d get right of entry to a list. The equal goes for reducing it.

>>> subjects[2]
'Algorithm'
>>> subjects[0:2]
('Machine Learning','Database')

4.2. A tuple is Immutable

However, Python tuple is immutable. Once declared, you could’t alternate its size or elements.

>>> subjects[2]='probability'
Traceback (most recent call last):
File “<pyshell#107>”, line 1, in <module>
subjects[2]=’probability’
TypeError: ‘tuple’ object does not support item assignment

5. Dictionaries

A dictionary holds key-value pairs. Declare it in curly braces, with pairs separated through commas. Separate keys and values by way of a colon(:).

>>> person={'Name':'Rohit','age':20}
>>> person
{‘city’: ‘Rohit’, ‘age’: 20}

The type() feature works with dictionaries too.

5.1. Accessing a Value

To get admission to a cost, you mention the important thing in rectangular brackets.

>>> person['Name']
‘Rohit’

5.2. Reassigning Elements

You can reassign a value to a key.

>>> person['age']=21
>>> person['age']
21

5.3. List of Keys

Use the keys() feature to get a list of keys within the dictionary.

>>> person.keys()
dict_keys([‘city’, ‘age’])

6. Boolean

A Boolean value may be True or False.

>>> a=2>1
>>> type(a)
<class ‘bool’>

7. Sets

A set will have a list of values. Define it the use of curly braces.

>>> a={1,2,3}
>>> a
{1, 2, 3}

It returns handiest one instance of any price presents extra than once.

>>> a={1,2,2,3}
>>> a
{1, 2, 3}

However, a set is unordered, so it doesn’t aid indexing.

>>> a[2]
Traceback (most recent call last):
File “<pyshell#127>”, line 1, in <module>
a[2]
TypeError: ‘set’ object does not support indexing

Also, it is mutable. You can change its elements or add more.

>>> a={1,2,3,4}
>>> a
{1, 2, 3, 4}

Use the remove()

>>a.remove(3)
{1, 2, 4}

add()

>>a.add(4)
{1, 2, 3, 4}

Confused MUCH?  Don’t worry! Read Mr.Bigdata for Python tutorial blogs to get deep insight and understand why Python is trending in industry.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.