Python Data Types

Updated: Aug 29, 2020




Python Data Types


Hello, readers, first of all, congratulations on being able to install python on your systems and now you must be all set to get tour hands dirty with Python.


Before discussing data types, I want to spend a couple of minutes discussing keywords in Python.


Keywords


Keywords are the words reserved by Python for its own use, they cannot be used as variable names. Below is the table of Python Keywords. As compared to other programming languages like C, Java, Python have very less number of keywords.




REPL: Read Evaluate Print Loop


Just for the testing purpose to make sure if your code is working fine, you really don't need to have a very big arrangement. Go to the Unix shell and type python, it will give you a python command line to work on. Note that it only executes one line at a time and is there just for handy python programming.


To exit the REPL, press Ctrl + D or type exit()



Comments In Python


Comments in python starts with #

There is a misconception that python supports multi-line comments with ''' or """



''' 
This is a multi-line comment
Welcome to LinuxAdvise
'''


In reality, it's a multi-line string that is going to take space in memory.



Data types In Python




Data types define the type of the variable, whether it is an integer variable, string variable, tuple, dictionary, list, etc. In this article, we will learn about the data types and their usage in Python.



1. Numeric Data Type in Python


IntegerIn Python 3, there is no upper bound on the integer number which means we can have the value as large as our system memory allows.




# Integer number
a=100
print(a)
print("Data Type of a variable is", type(a))


Output:


100 
Data Type of a variable is <class 'int'>



FloatValues with decimal points are the float values, there is no need to specify the data type in Python. It is automatically inferred based on the value we are assigning to a variable. For example, here fval is a float data type.



# Float number
fval=87.97
print(fval)
print("Data Type of a variable is", type(fval))

Output:


87.97 
Data Type of a variable is <class 'float'>



Complex Number Numbers with real and imaginary parts are known as complex numbers. Unlike other programming languages such as Java, Python is able to identify these complex numbers with the values. In the following example when we print the type of the variable cval, it prints as a complex number.


# Complex number
cval=5+4j
print(cval)
print("Data Type of a variable is", type(cval))

Output:


(5+4j) 
Data Type of a variable is <class 'complex'>



Binary, Octal, and Hexadecimal numbers


In Python, we can print decimal equivalent of binary, octal, and hexadecimal numbers using the prefixes.

0b(zero + 'b') and 0B(zero + 'B') – Binary Number 0o(zero + 'o') and 0O(zero + 'O') – Octal Number 0x(zero + 'x') and 0X(zero + 'X') – Hexadecimal Number



# Integer equivalent of binary number 1010
bval=0b1010
print(bval)

# Integer equivalent of octal number 323
cval=0o323
print(cval)

# Integer equivalent of hexadecimal number FFA
hval=0xFFA
print(hval)

Output:


10
211
4090



2. Python Data Type – String


String is a sequence of characters in Python. The data type of String in Python is called "str".

Strings in Python are either enclosed with single quotes or double-quotes. In the following example, we have demonstrated two strings one with the double quotes and other string str2 with the single quotes.


# Python program to print strings and type
str1="This is a string with double quotes"
str2='this is a string with single quotes'

# Displaying string str1 and its type
print(str1)
print(type(str1))

# Displaying string str2 and its type
print(str2)
print(type(str2))

Output:


This is a string with double quotes
<class 'str'>
this is a string with single quotes
<class 'str'>



3. Python Data Type – List


A list is like a tuple, it is also an ordered collection of elements, however, the list is a mutable data type which means it can be changed, unlike tuple which is an immutable data type.

A list is enclosed with square brackets and elements are separated by commas.


# List of integer
list1=[1,2,3,4,5]

# Print entire list
print(list1)

# List of strings
list2=["C++","Java","Python"]
print(list2)
# List of mixed type elements
list3=[8,'Suresh', 85.7,'Rajkumar']
print(list3)

Output:


(1, 2, 3, 4, 5) 
('C++', 'Java', 'Python') 
(8, 'Suresh', 85.7, 'Rajkumar')



4. Python Data Type – Dictionary


Dictionary is a collection of key and value pairs. A dictionary doesn’t allow duplicate keys, but the values can be duplicate. It is an ordered, indexed, and mutable collection of elements.

The keys in a dictionary doesn’t necessarily to be a single data type, as you can see in the following example that we have 1 integer key and two string keys.


# Dictionary example
dict={1:"Akshay","lastname":"Siwal","age":30}

# Print the value where key value is 1
print(dict[1])

# Print the value where key value is "lastname"
print(dict["lastname"])

# Print the value where key value is "age"
print(dict["age"])

Output:


Akshay
Siwal
30



5. Python Data Type – Tuple


A tuple is an immutable data type in Python which means it cannot be changed. It is an ordered collection of elements enclosed in round brackets and separated by commas.



# Tuple of integer
t1=(1,2,3,4,5)
# Print entire tuple
print(t1)

# Tuple of strings
t2=("Hi","How are you ?","welcome to LinuxAdvise")
print(t2)

# Tuple of mixed type elements
t3=(2,'Prashanti', 45.6,'Shradha')
print(t3)

Output:


(1, 2, 3, 4, 5) 
('Hi', 'How are you ?', 'welcome to LinuxAdvise') 
(2, 'Prashanti', 45.6, 'Shradha')



6. Python Data Type – Set


A set is an unordered and un-indexed collection of items. This means when we print the elements of a set they will appear in the random order and we cannot access the elements of set based on indexes because it is un-indexed.

Elements of the set are separated by commas and enclosed in curly braces.


# Set example
myset ={'Hi',2,4.5,"LinuxAdvise"}
print(myset)

Output:


{'LinuxAdvise', 2, 'Hi', 4.5}



7.Python Data Type – Boolean


The boolean data type is either True or False. In Python, boolean variables are defined by the True and False keywords.


a = True
print(type(a))

b = False
print(type(b))

print(10 > 9)

print(10 == 9)

print(10 < 9)

Output:


<class 'bool'> 
<class 'bool'> 
True
False 
False






In the next article, we will study Python datatype in detail.



Happy Reading !!









113 views0 comments

Recent Posts

See All
 

Subscribe Form

©2020 by Linux Advise