Standard data types of Python programming language

Standard data types of Python programming language

A variable can hold different types of values, these types are called data types. A person's name or address stored as a string because it is stored as letters and words but a person's id must be an integer because it is digits only.

Python provides a lot of standard data types. Data types available in Python are below.

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Let's explain these data types one by one

1. Numbers

Number stores numeric values. When we assign any number to a variable Python programming language makes a number object for that variable. For example;

1.      a = 3 , b = 5  #a and b are number objects  

Python supports 4 types of numeric data.

  1. int (signed integers like 10, 2, 29, etc.)
  2. long (long integers used for long-range integer 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.)

lower-case L can be use in Python with long integers. But developers always use an upper-case L to avoid confusion.

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

2. String

The string can be defined as the sequence of characters represented in the quotation marks. Python provide us to use single, double, or triple quotes to define a single or multiline string.

For string handling python provides multiple operators and functions that perform operations on string for multiple porposes.

Concatenation Operator (+):

Concatenation Operator + is used to merge two strings. 

Example : "hello"+" python" returns "hello python".

Repetition Operator (*):

When we want to repeat some text multiple times we can use * operator to repeat text.

Example: "Python " *4 returns "Python Python Python Python".

Below are some more example about string in python

1.      str1 = 'hello the World of Python ' #string str1  

3.      str2 = ' how are you' #string str2  

4.      print (str1[0:2]) #printing first two character using slice operator  

5.      print (str1[4]) #printing 4th character of the string  

6.      print (str1*2#printing the string twice  

7.      print (str1 + str2) #printing the concatenation of str1 and str2  

 

3. List

Lists are similar to arrays we use C / C++ or other programming languages. The main difference is that list is able to contain data of different data types. The items we want to store in a list must be separated with a comma (,). List are always enclosed in braces ([]).

List can be divide in multiple slices. slice operator [:] will use to access the data of the list. The concatenation operator (+) and repetition operator (*) are used in the same way as on strings.

Consider the following example.

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, 'hello', 'pythoon', 2, 1, 'hello', 'pythoon', 2, 1, 'hello', 'pythoon', 2]

4. Tuple

A tuple is same as the list but there are some differences. Tuples also have the collection of the items of different data types which is same like list. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().

We cant modify size and items of a tuple. so its read only.

Let's see a simple example 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 
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment

5. Dictionary

Dictionary key-value pair of items organized in a set. Dictionary is like an associative array or a hash table. In dictionary every key stores a specific value.

Dictionary Items are separated with the comma (,) and enclosed in the curly braces {}.

Consider the following example.

1.      d = {1:'Jimmy'2:'Alex'3:'john'4:'mike'};   

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 Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
[1, 2, 3, 4]

 

['Jimmy', 'Alex', 'john', 'mike']

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

Related Articles
About Author