Data types in python programming


Free python courses
Free python courses

Introduction

Data types are an important concept in python programming. Every value in Python has its data type. As you know, every value in python is an object, and the data types are the classes, and the instance of these classes are called variables. The classification of data items into a data category to understand what kind of operations we can perform on these values is known to be data types.

Some built-in data types are as follows:- 

  • Binary type – memory view, byte array, bytes.
  • Boolean type- bool 
  • Set type – frozenset,set
  • Mapping type -dict
  • Sequence type – string ,list ,tuple 
  • Numeric type – integer , complex number, float 
  • Text type – str

Data Types of Python Of Python Programming

  1. Numeric type – as the name suggests, it represents the numeric value. The value can be an integer (int), floating number (float), or a complex number (complex) in python. 

REMEMBER – we can use the type () function to determine the type of data type. If we want to check the value of a particular class, we use the “isinstance ()”  function.

  • Integers – we can represent integer value by int class as the integers contain both the positive and the negative-whole numbers without fraction or decimals. There is no limit in the Python programming language on how long an integer value should be, i.e., the integer value has no maximum limit. 
  • Float- we can represent this by float class. A float is a real number having the representation of a floating point. We can specify it by a decimal point which is the difference between floating-point and integer. Though there is an exception if a positive or negative integer follows a character e or E, it specifies a scientific notation.
  • Complex number – a complex class can represent the complex numbers, for example – 2 + 3j; in this, the number followed by j is called the imaginary part that is 3j while 2 is called the real part. So we can say that it is specified as ((real part) + (imaginary part)).
  1. Sequence type – Because of the sequence type, we can store multiple values (similar or different data types) in an organized manner; there is no compulsion on a data type to be of the same type. There are three types of sequence types in python programming. 
  2. String – in python, we represent the string as “str.” The string is a sequence of Unicode characters and is represented using double quotes or single quotes. We can also triple quotes (“”” or”‘) if the strings are multiple. The characters that are listed inside the quotes are known as the items of the string.

Like tuples, the strings are also immutable and use the slicing operators. We can extract the items. We can insert as many characters as we want, while the only limitation is the machine’s memory resources. An error will occur if we try to delete or update items of a string in python programming.

A syntax of string is – 

>>> s = “python string”

>>> s = “’ a multi-string 

If we want to represent something as a string, we will need to use another type of quote to define the string at the beginning and end.

The syntax for a single quote character 

>>> print (“This string contains a single quote (‘) character.”)

This string contains a single quote (‘) character

The syntax for double quote character 

>>> print (‘This string contains a double quote (“) character.’)

This string contains a double quote (“) character.

We can access the single character of a string by using a method called indexing. In the indexing method, a negative address reference is allowed to access the characters from the back of the string. 

  1. List – An ordered sequence of items represents the LIST. In python, it is known to be as most flexible data type and is highly used. There is no need for a value in the list to be of the same type. It contains versatile data and hence is the most exclusive datatype. It is easy for the data type to hold different types of data. A list can be represented in the enclosed brackets, and the commas are used to separate the items.

A list can be represented as – 

>>> a= [ 6, 8, 8,’list’]

If you want to alter the values of the item, you can do it in the list. We can access the list data type using the index operator. A negative sequence index represents the position of the index from the end of an array. If instead of writing the whole compute offset as List[len (list)-3], only List[-3] would be enough.

  1. Tuple – sequence of items in order is known as a tuple. We cannot modify the tuples. As in the list, we can alter the items we cannot do in tuples, which is the only difference between a tuple and a list. The tuples cannot be modified or changed like a list datatype, so they are faster than the list. The representation of tuples is the same as that of the list. But instead of brackets, we use parenthesis () in tuples. The use of tuple is to write-protect data. 

A tuple can be represented as- 

>>>t= (4,’tuple’, 4+2r)

As in string, we can use a slicing operator to extract the items. We can do the same for the tuple, but this will not allow the tuple to change the value. 

Similarly, as a list data type, We can access the tuple using the index operator. The index here must be an integer. In case you want to access nested tuples in such a case you need to use nested indexing.

  1. Boolean type – it is the data type that has two built-in values, true and false. The non-Boolean objects can also be evaluated in Boolean form. We can represent the class of Boolean as a bool. True with ‘T’ and false with ‘F’ are valid Booleans in python, or there will be an error. In the Boolean context, the true value is called “truthy,” while the false value is called “falsy.” 

A Boolean type look as follows- 

>>>type (True)

<Class ‘bool’>

>>>type (False)

<Class ‘bool’>

  1. Set type – A group or collection of unique unordered items is called the set data type. In simple words, a set data type is unordered. Like sequence (type-list), a set type is represented using braces [] a set is defined, and a comma is used to separate the items.

In a set type, the duplicate values are eliminated, and only the unique values are kept. We can perform operations like union and intersection on these sets. 

A set type in python looks as follows- 

>>> a=[3,4,5,2,2,2,8,9]

>>>a

[3,4,5,2,8,9]

As mentioned, the set data type is unordered; hence the slicing operator cannot work in this datatype. So there is no meaning in indexing a set.

  1. Dictionary – like the set data type, a dictionary data type is unordered. The values are in pairs and hence are known to be the key-value pairs. If you need to deal with a high volume of data, then the Dictionary data type is useful in such a case. The function of the dictionary data type is to revert the data from which it is optimized. One can only retrieve the value if the key to retrieve is known. 

We use curly braces {} to define the data type. The key: value pair is the item. The value and the key used can be of any data type. 

A python dicitionary looks like this- 

>>>d= { 3:’key’,5:’value’}

0



Source link

Leave a Comment

Your email address will not be published. Required fields are marked *

//benoopto.com/4/3962221