Python Data Types

Python Data Types

Python Data Types

Variables can hold values of different data types(facts sorts). Python is a dynamically typed language hence we need now not define the data type of the variable whilst maintaining it. The interpreter implicitly binds the data with its type.

Python enables us to test the kind of variable used within the program. Python presents us with the type() function which returns the sort of the variable exceeded.

take into account the following example to outline the values of different data types and checking their type.

  1.  A=100  
  2. b="Welcome to Python"  
  3. c = 100.5  
  4. print(type(a));   
  5. print(type(b));   
  6. print(type(c));   
Output:
<type 'int'>
<type 'str'>
<type 'float'>


Standard Data Types

A variable can keep extraordinary kinds of values. for instance, a person's name ought to be stored as a string while its identity ought to be stored as an integer.

Python presents numerous popular data types that define the garage approach on each of them. The information types defined in Python are given below.
  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

on this segment of the academic, we can supply a brief introduction of the above data types. we will discuss each considered one of them in element later in this academic.

Numbers

a wide variety of shops numeric values. Python creates range items when a variety of is assigned to a variable. as an example;

  1. a = 5 , b = 100  #a and b are number variable  
Python supports 4 types of numeric data.
  1. int (signed integers like 10, 2, 29, etc.)
  2. long (long integers used for a higher range of values like 908090800L, -0x1929292L, etc.)
  3. float (float is used to store floating point numbers like 1.9, 9.902, 15.2, etc.)
  4. complex (complex numbers like 2.14j, 2.0 + 2.3j, etc.)
Python lets in us to apply a decrease-case L for use with long integers. but, we should usually use an top-case L to avoid confusion.

A complex number includes an ordered pair, i.e., x + y where x and y denote the real and imaginary elements respectively).

String

The string can be described because of the collection of characters represented inside the quotation marks. In python, we will use single, double, or triple quotes to define a string.

String coping within python is an honest venture on the grounds that there are various inbuilt capabilities and operators supplied.

in the case of string dealing with, the operator + is used to concatenate two strings as the operation "hi there"+" python" returns "hi there python".

The operator * is known as a repetition operator as the operation "Python " *2 returns "Python Python ".

the subsequent instance illustrates the string coping within python.

  1. str1 = 'hello student' #string str1  
  2. str2 = ' how are you' #string str2  
  3. print (str1[0:2]) #printing first two character using slice operator  
  4. print (str1[4]) #printing 4th character of the string  
  5. print (str1*2#printing the string twice  
  6. print (str1 + str2) #printing the concatenation of str1 and str2  
Output:
he
o
hello studenthello student
hello student how are you

Lists

Lists are much like arrays in C. however; the list can incorporate statistics of different kinds. The gadgets saved in the listing are separated with a comma (,) and enclosed within rectangular brackets [].
we can use slice [,:] operators, to get right of entry to the facts of the list. The concatenation operator (+) and repetition operator (*) works with the list inside the same way as they had been working with the strings.

take into account the following instance.

  1. l  = [1"hi""python"2]  
  2. print (l[3:]);  
  3. print (l[0:2]);  
  4. print (l);  
  5. print (l + l);  
  6. print (l * 3);   
Output:
[2]
[1, 'hi']
[1, 'hi', 'python', 2]
[1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
[1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2]

Tuple

A tuple is much like the listing in many ways. Like lists, tuples additionally contain the collection of the gadgets of various facts types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().


A tuple is a read-handiest records shape as we cannot adjust the size and value of the items of a tuple.

let's see an easy instance of the tuple.

  1. t  = ("hi""python"2)  
  2. print (t[1:]);  
  3. print (t[0:1]);  
  4. print (t);  
  5. print (t + t);  
  6. print (t * 3);   
  7. print (type(t))  
  8. t[2] = "hi";  
Output:
('python', 2)
('hi',)
('hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2)
<type 'tuple'>
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    t[2] = "hi";
TypeError: 'tuple' object does not support item assignment

Dictionary

Dictionary is an ordered set of a key-price pair of gadgets. it's far like an associative array or a hash table wherein each key stores a specific price. Key can keep any primitive data type whereas value is an arbitrary Python object.

The gadgets inside the dictionary are separated with the comma and enclosed inside the curly braces {}.

don't forget the following instance.

  1. d = {1:'John'2:'Sohan'3:'Jecob'4:'Stive'};   
  2. print("1st name is "+d[1]);  
  3. print("2nd name is "+ d[4]);  
  4. print (d);  
  5. print (d.keys());  
  6. print (d.values());  
Output:
1st name is John
2nd name is Stive
{1: 'John', 2: 'Sohan', 3: 'Jecob', 4: 'Stive'}
[1, 2, 3, 4]
['John', 'Sohan', 'Jecob', 'Stive']



Python Data Types Python Data Types Reviewed by Sk Jha on January 06, 2019 Rating: 5

No comments:

Powered by Blogger.