Python Data Types

Variables can hold values of different data types. Python is a dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.
Python enables us to check the type of the variable used in the program. Python provides us the type() function which returns the type of the variable passed.
Consider the following example to define the values of different data types and checking its type.


Standard data types

A variable can hold different types of values. For example, a persons name must be stored as a string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.
  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary
In this section of the tutorial, we will give a brief introduction of the above data types. We will discuss each one of them in detail later in this tutorial.

Numbers

Number stores numeric values. Python creates Number objects when a number is assigned to a 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 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 allows us to use a lower-case L to be used with long integers. However, we must 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).

String

The string can be defined as the sequence of characters represented in the quotation marks. In python, we can use single, double, or triple quotes to define a string.
String handling in python is a straightforward task since there are various inbuilt functions and operators provided.
In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python".
The operator * is known as repetition operator as the operation "Python " *2 returns "Python Python ".
The following example illustrates the string handling in python.
  1. str1 = 'hello javatpoint' #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  
affiliate_link
Output:
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
affiliate_link

List

Lists are similar to arrays in C. However; the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].
We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.
affiliate_link

Comments

Popular posts from this blog

Python

Python Applications