coderz.py

Keep Coding Keep Cheering!

Data Types in Python

JOIN Clause

The classification or categorization of data items is referred to as data types. It represents the type of value that specifies which operations can be performed on a given piece of data. Because everything in Python programming is an object, data types are classes, and variables are instances (objects) of these classes.

The data types defined in Python are given below.

  • Numbers
  • Sequence Type
  • Boolean
  • Set
  • Dictionary
Numbers:

Numeric values are stored in Numbers. Python Numbers data types include integer, float, and complex values. Python includes the type() function for determining the data type of the variable. Similarly, the isinstance() function is used to determine whether an object belongs to a specific class.

  • int – stores signed integers of unbounded length.
  • long– contains long integers (exists in Python 2.x, deprecated in Python 3.x).
  • float– holds floating precision numbers with a precision of up to 15 decimal places.
  • complex– contains complex numbers

For instance,

x = 33 
print("The type of x: ", type(x))  
  
y = 410.5  
print("The type of y: ", type(y))  
  
z = 1+3j  
print("The type of z: ", type(z))  
print(" z is a complex number", isinstance(1+3j,complex))

Output:

The type of x:  <class 'int'>
The type of y:  <class 'float'>
The type of z:  <class 'complex'>
 z is a complex number True
> 
Sequence Type:

Sequences allow to store multiple values in an organized and efficient fashion. There are several sequence types in Python –

  • String
  • List
  • Tuple

String:

The string is defined as the sequence of characters enclosed by quotation marks. To define a string in Python, we can use single, double, or triple quotes.

String manipulation in Python is simple because Python includes built-in functions and operators for performing string operations. For instance,

a = "string in a double quote"
b= 'string in a single quote'
print(a)
print(b)

# using ',' to concatenate the two or several strings
print(a,"concatenated with",b)

#using '+' to concatenated the two or several strings
print(a+" concatenated with "+b)

Output:

string in a double quote
string in a single quote
string in a double quote concatenated with string in a single quote
string in a double quote concatenated with string in a single quote
> 

List:

Python lists are analogous to arrays in C. However, the list may contain data of various types. The items in the list are separated by commas (,) and are enclosed in square brackets [].

To access the list’s data, we can use the slice [:] operators. The concatenation operator (+) and repetition operator (*) operate on the list in the same way that they do on strings.

For instance,

#list of having only integers
a= [1,2,3,4,5,6]
print(a)

#list of having only strings
b=["hello","john","reese"]
print(b)

#list of having both integers and strings
c= ["hey","you",1,2,3,"go"]
print(c)

#index are 0 based. this will print a single character
print(c[1]) #this will print "you" in list c

Output:

[1, 2, 3, 4, 5, 6]
['hello', 'john', 'reese']
['hey', 'you', 1, 2, 3, 'go']
you

Tuple:

In many ways, a tuple is similar to a list. Tuples, like lists, contain a collection of items of various data types. The tuple’s items are separated by a comma (,) and enclosed in parentheses ().

A tuple is a read-only data structure because we cannot change the size or value of its items.

For instance,

#tuple having only integer type of data.
a=(1,2,3,4)
print(a) #prints the whole tuple

#tuple having multiple type of data.
b=("hello", 1,2,3,"go")
print(b) #prints the whole tuple

#index of tuples are also 0 based.

print(b[4]) #this prints a single element in a tuple, in this case "go"

Output:

(1, 2, 3, 4)
('hello', 1, 2, 3, 'go')
go
Boolean:

The Boolean type has two built-in values: True and False. These values are used to determine whether the given statement is true or false. The class bool denotes it. True is represented by any non-zero value or the letter ‘T,’ whereas false is represented by the letter ‘F.’

For example,

# Python program to 
# demonstrate boolean type
  
print(type(True))
print(type(False))
  
print(type(true))

Output:

<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
Set:

Python Set is the data type’s unordered collection. It is iterable, mutable (can be changed after creation), and contains distinct elements. The order of the elements in set is undefined; it may return the element’s changed sequence. The set is created by calling the built-in function set(), or by passing a sequence of elements enclosed in curly braces and separated by a comma. It can hold a variety of values. For instance,

# Creating Empty set  
set1 = set()  
  
set2 = {'Gosling', 2, 3,'Python'}  
  
#Printing Set value  
print(set2)  
  
# Adding element to the set  
  
set2.add(10)  
print(set2)  
  
#Removing element from the set  
set2.remove(2)  
print(set2)  

Output:

{'Gosling', 'Python', 2, 3}
{'Gosling', 2, 3, 10, 'Python'}
{'Gosling', 3, 10, 'Python'}
Dictionary:

A dictionary is an unordered collection of key-value pairs. It is similar to an associative array or a hash table in that each key stores a unique value. The key can be any primitive data type, whereas the value can be any Python object.

The dictionary items are separated by commas (,) and enclosed in curly braces.

Example,

#a sample dictionary variable

a = {1:"first name",2:"last name", "age":33}

#print value having key=1
print(a[1])
#print value having key=2
print(a[2])
#print value having key="age"
print(a["age"])

Output:

first name
last name
33

Note: also read about Input and Output in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement