BASIC DATA TYPES IN PYTHON

 

NUMERIC

Numeric data can be broadly divide into integers and real numbers (i.e. fractional numbers). Integers can themselves be positive and negative. Unlike many other programming languages, Python does not have any upper bound on the size of integers. The real numbers or fractional numbers are called floating point numbers in programming languages. Such floating point numbers contain a decimal and a fractional part.

Let us look at an example that has an integer as well as a real number:



STRING

A String in Python can be a series or a sequence of alphabets, numerals and special characters. Similar to C, the first character of a string has an index 0. Single quotes or double quotes are used to represent strings.

 There are many operations that can be performed on a string. There are several operators such as slice operator ([] and [:]), concatenation operator (+), repetition operator (*), etc. Slicing is used to take out a subset of the string, concatenation is used to combine two or more than two strings and repetition is used to repeat the same string several times.

 Here is the example of string data:


LIST

List is the most used data type in Python. . A list is an ordered and indexable sequence.  A list can contain the same type of items. Alternatively a list can also contain different types of items. To declare a list in Python we need to separate the items using commas and enclose them within square brackets ([]). A list is somewhat similar to the array in C language. However, an array can contain only the same type of items while a list can contain different types of items.

Similar to string data type, the list also has plus (+), asterisk (*) and slicing [:] operators for concatenation, repetition and sub-list, respectively.

 Examples for list data type:



TUPLE

Similar to list a tuple is used to store sequence of items, Like a list, a tuple consists of items separated by commas. However, tuples are enclosed within parenthesis rather than within square brackets.


Difference between list and tuple is that list is mutable i.e. we can change the items in a list even after its initialization but tuple is immutable, i.e.  items in a tuple cannot be changed after its initialization. Once the item is stored in the tuple, it cannot be modified.


DICTIONARY

A Python dictionary is an unordered collection of key-value pairs. When we have the large amount of data, the dictionary data type is used. Keys and Values can be of any type in a dictionary. Items in dictionary are enclosed in the curly-braces {} and separated by comma (,). A colon (:) is used to separate key from value. A key inside the square bracket [] is used for accessing the dictionary items.

Example of dictionary:


BOOLEAN

In programming language, mostly data is stored in the form of alphanumeric but sometimes we need to store the data in the form of ‘yes’ or ‘no’. In terms of programming language, Yes is termed as true and No is termed as false.

This True and False data is known as Boolean Data and the data types which stores this Boolean data are known as Boolean Data Types.



Comments

Popular posts from this blog

Computer Science Lecture Notes for Degree Students

DATA TYPES IN PYTHON

COMMENTS, IDENTIFIERS AND KEYWORDS IN PYTHON