# Data Types in Python – CBSE Class 12

**Introduction**

In any language, there are some fundamentals you need to know before you can write even the most elementary programs. In this tutorial, we are going to introduce some such fundamentals: data types, variables, operators and expression in Python.

Python provides a predefined set of data types for handling the data it use. Data can be stored in any of these data types. This chapter is going to discuss various types of data that you can store in Python. Of course, a program also needs a means to identify stored data.

So, this tutorial we shall also talk about mutable and immutable variables in Python.

Data can be of many types for example, character, integer, real, string etc. Anything enclosed in quotes represents string data in Python. Numbers without fractions represent integer data. Numbers with fractions represent real data and True and false represent Boolean data. Since the data to be dealt with are of many types, a programming language must provide ways and facilities to handle all types of data.

Before you learn how you can process different types of data in Python, let us discuss various data- types supported in Python. In this discussion of data types, you “II be able to know Python’s capabilities to handle a specific type of data, such as the memory space it allocates to hold a certain types of data and the range of values supported for a data type etc.

### Data Types

**In python programming, data can be of many types for example**

**Integer:**Numbers without fractions represent integer data (Like 1 or 2 or 3).**Real:**Numbers with fractions represent real data (like 1.2 or 3.5 or 7.4).**Complex:**A complex number is always in the form of**A+Bi.**Where I is the imaginary number (equal to the square root of -1).**Boolean**: True and false represent Boolean data (Like 0 or 1).**String**: Anything enclosed in quotes represents a string (Like “Python” or ‘Python’) data in Python.**List**: A list in python represents a list of comma-separated value of any datatype between square brackets like: L=[1,2,3,4,5].**Tuple**: Tuples are represented as group of commas – separated values of any data type within parentheses, like: P = (1,2,3,4,5).**Dictionary**: The dictionary is an unordered set of comma-separated key value pairs, within [], like: D = [“a” : 1, “b” : 2, “c” : 3].

Since the data to be dealt with are of many types, a programming language must provide ways and facilities to handle all types of data and no doubt, python programming language is doing a very good work in this area.

Before learning, how can you process different types of data in Python, let’s start learning of these **Built-in Core Data** types of python programming and get a very good understanding to work with python programming language.

In this discussion of data types, you “II be able to know Python’s capabilities to handle a specific type of data, such as the memory space it allocates to hold a certain type of data and the range of values supported for a data type etc.

**Built-in Core Data Types**

From a beginner’s point of view, here we are dividing our discussion in the following types.

- Numeric (Numbers, Integer, Float, Complex)
- String
- List
- Tuple
- Dictionary

### Numeric:

As it is clear by the name the number data types are used to store numeric values in Python. The **numbers** in Python have the following core data types:

- Integers
- Floating – point Numbers
- Complex Numbers

**Integers:**

Integers are whole numbers such as 5, 39, 19117, O etc. They have no fractional parts. Integers are represented in Python by numeric values with no decimal point. Integers can be positive or negative, e.g., + 12, – 15, 3000 (missing + or – symbol means it is positive number).

**Floating point numbers**

A number having fractional part is a floating-point number. For example, 3.14159 is a floating-point number. The decimal point signals that it is a floating -point number, not an integer. The number 12 is an integer, but 12.0 is a floating-point number. Recall that fractional numbers can be written in two forms:

**Fractional Form**(Normal Decimal Notation) e.g., 3500.75, 0.00005, 147.9101 etc.**Exponent Notation**e.g., 3.50075EO3, 0.53-04, 1. 479101EO2 etc.

Floating point variables represent real numbers, which are used for measurable quantities like distance, area, temperature etc. and typically have a fractional part. Floating -point numbers have two advantages over integers:

- They can represent values between the integers:
- They can represent a much greater range of values. But floating-point numbers suffer from one disadvantage also:
- Floating-point operations are usually slower than integer operations.

In Python, floating point numbers represent machine-level double precision floating point numbers2 (15digit precision). The range of these numbers is limited by underlying machine architecture subject to available (virtual) memory.

### Complex Numbers

**Complex Number in python**

Python represents complex numbers in the form A + B j. That is, to represent imaginary number, python uses j (or j) in place of traditional i. so in python I. so in python, . Consider the following examples where “a” and b are storing two complex in python:

a = 0 + 3. 1 j

b = 1.5 + 2 j

The above complex number “a” has real component as **0** and imaginary component as **3.1**; in complex number** b**, the real part is **1.5** and imaginary part is **2. **When you display complex numbers, python **displays complex numbers in parentheses when they have a nonzero real part** as shown in following examples

Unlike python’ s other numeric types, complex numbers are a composite quantity made of two parts: the real part and the imaginary part , both of which are represented internally as **float** values ( floating point numbers).

You can retrieve the two components using attribute references. For a complex number z:

**real**gives the real part.**imag**gives the imaginary part as a float, not as a complex value.

### Boolean:

These represent the truth values false and true. The Boolean types is a subtype of plain integers, and Boolean values false and true behave like the values O and 1, respectively. To get the Boolean equivalent of O or 1, you can type bool (O) Bool (1), Python will return False or Ture respectively. However, when you convert Boolean values False and True to a string, the strings “False” or “True” are returned, respectively. The str () function converts a value to string. See figure above (right side).

However, when you convert boolean values TRUE and FALSE to a string, the strings TRUE and FALSE are returned, respectively. The str() function converts a value to string.

#### Boolean Operators:

Boolean operators are keywords and symbols, such as AND or NOT, which facilitate you to expand or narrow your search parameters while using a database or search engine. When you search using these operators, it is known as a Boolean search.

You can use Boolean operators such as AND, OR, and NOT alongside keywords to create a Boolean string that will refine your search to find the most relevant results and sources.

Common boolean operations are –

- Boolean AND operator.
- Boolean OR operator.
- Boolean NOT operator.
- Boolean equivalent (==) operator.
- Boolean not equivalent(!=) operator.

### String:

You already know about strings (as data) in Python. In this section, we shall be talking about Python’s data type strings. A string data type lets you hold string data, i.e., any number of valid characters into a set of quotation marks.

In Python, each character stored in a string is a Unicode character. Or in other words, all strings in Python 3.x are sequences of pure Unicode characters. Unicode is a system designed to represent every character from every language. A string can hold any type of known characters i.e., letters, numbers, and special characters, of any known scripted language.

Following are all legal strings in Python:

**“abcd”, “1234”, ‘$%^&, 2222’, “ŠÆËS”, “222222”, 2222**

### List:

A list in python represents a list of comma-separated value of any datatype between square brackets e.g., following are some lists:

** 1,2,3,4,5]**

** [‘a’,’e’,’I’,’o’,’u’]**

** [Qissba, 102, 79.5]**

Like any other value, you can assign a list to a variable.

**Example**

** ****>>> a= [1,2,3,4,5] #statement 1**

**>>> a [1,2,3,4,5]**

**>>> print (a) [1,2,3,4,5]**

To change first value in a list namely given above, you may write

**>> a[0] = 10 #change 1st item – consider statement 1 above**

**>>> a [10,2,3,4,5]**

To change 3^{rd} item, you may write

**>> a [2] = 30 #change 3rd item**

** >>> a [10,2,30,4,5]**

You guessed it right; the value internally are numbered from (zero) onwards i.e., first item of the list is internally numbered as zero, second item of the list as 1,3^{rd} item as 2 and so on.

### Tuple:

You can think of tuples (pronounced as tu-pp-le, rhyming with couple) as those lists which cannot be changed i.e., are not modifiable. Tuples are represented as group of commas – separated values of any data type within parentheses, e.g., following are some tuples:

**P = (1,2,3,4,5) **

**q = (2,4,6,8,) **

**r = (‘a’,’e’,’I’,’o’,’u’) **

**h = (7,8,9, ‘A’,’B’,’C’)**

### Dictionary:

Dictionary data types is another feature in python’s hat. The dictionary is an unordered set of comma-separated key : value pairs, within [ ], with the requirement that within a dictionary, no two keys can be the same (i.e., there are unique keys within a dictionary). For instance, following are some dictionaries:

**persondict = {**

** “name”: “qissba”,**

** “age”: “09”,**

** “class”: 4**

** }**

Another example of dictionary:

Adddict = {

“city”: “delhi”,

“pin”: “110034”,

“country”: india

}