What are the common built-in data types in Python?



Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Numeric Types

Python supports four different numerical types −

  • int − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

  • long − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.

  • float − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • complex − These are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.

Example

Let us see an example 

# Python int val1 = 25 print(val1) # Python float val2 = 11.89 print(val2) # Python complex val3 = 6+2.9j print(val3) # Python hexadecimal val4 = 0x12d print(val4) # Python octal val5 = 0o021 print(val5)

Output

25
11.89
(6+2.9j)
301
17

Booleans

Example

Boolean type has two values i.e. True and False. True is for 1 and False 0. Let’s see an example −

a = (1 == True) b = (1 == False) print(a) print(b)

Output

True
False

Text Sequence Types – string

We can easily create a string by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable.

Let’s see how to easily create a String in Python −

myStr = Thisisit!'

Example

We will now see an example of creating single-line and multi-line strings 

str1 = "John" print(str1) # Multi-line string str2 = """ This, is it! """ print(str2)

Output

John
This,
is it!

List

A list contains items separated by commas and enclosed within square brackets ([]). Creating a list is as simple as putting different comma-separated values between square brackets. A list can have integer, string or float elements. With that, we can also create a List with mixed data types.

The list can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type

Create a Python List with Integer elements

We will create a list with 10 integer elements and display it. The elements are enclosed by square brackets. With that, we have also displayed the length of the list and how we can access specific elements using the square brackets 

Example

# Create a list with integer elements mylist = [25, 40, 55, 60, 75, 90, 105, 130, 155, 180]; # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])

Output

List =  [25, 40, 55, 60, 75, 90, 105, 130, 155, 180]
Length of the List =  10
1st element =  25
Last element =  180

Create a Python List with String elements

We can also add string elements to a Python List. We will create a list with 5 string elements and display it. The elements are enclosed by square brackets. With that, we have also displayed the length of the list and how we can access the first and last element using the square brackets −

Example

# Create a list with string elements mylist = ["BMW","Audi","Tesla","Honda","Toyota"]; # Display the list print("List = ",mylist) # Display the length of the list print("Length of the List = ",len(mylist)) # Fetch 1st element print("1st element = ",mylist[0]) # Fetch last element print("Last element = ",mylist[-1])

Output

List =  ['BMW', 'Audi', 'Tesla', 'Honda', 'Toyota']
Length of the List =  5
1st element =  BMW
Last element =  Toyota

Tuple

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square brackets.

Let us first create a basic Tuple with integer elements and then move towards Tuples within a Tuple 

Example

# Creating a Tuple mytuple = (20, 40, 60, 80, 100) # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple))

Output

Tuple =  (20, 40, 60, 80, 100)
Tuple Length=  5

Dictionary

Python's dictionaries are kind of hash-table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. The correct syntax to create a Python Dictionary is storing values in the form of key:value pairs. On the left of colon, we store keys and on the right, values i.e.

key:value

Dictionary is enclosed by curly bracket and do not allow duplicates. According to the 3.7 Python update, dictionaries are now ordered. Consider Dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.

We will create 4 key-value pairs, with keys Product, Model, Units and Available and values Mobile, XUT, 120 and Yes. Keys are on the left of colon, whereas values are on the right −

Example

# Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print("Dictionary = \n",myprod)

Output

Dictionary = 
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}

Advertisements