Beginner’s Guide to Standard Data Types in Python

Prashant Last Updated : 14 Oct, 2024
5 min read

Introduction

Python is a language that needs no introduction. It’s incredibly powerful, versatile, fast, and easy to learn. It is one of the languages that’s witnessing incredible growth and recognition year by year. Being object-oriented, it is often a programmer’s first choice for general-purpose programming. In this article, we will learn about some of the different values and standard data types in the Python language.

Learning Objectives:

  • Understand what data types are.
  • Learn about the different data types in Python.
  • Know the applications of the various data types in Python programming and their syntax.

What Are Data Types?

Data types are the classification or categorization of knowledge items. It represents the useful type that tells what operations are often performed on specific data. Since everything is an object in Python programming, data types are classes, and variables are instances (objects) of those classes.

Data types are an important concept within the Python programing language. Every value has its own Python data type in the Python programming language. Data Type is the classification of knowledge items or placing the info value into some kind of data category.

Data Types in Python

Python has six standard or built-in data types:

  • Numeric
  • String
  • List
  • Tuple
  • Set
  • Dictionary
Python has six standard Data Types
Source: educba.com

Now let’s discuss the above data types one by one.

Numeric Data Type

In Python, numeric data type represents the data that has a numeric value. The numeric value can be an integer, a floating number, or even a complex number. These values are defined as int, float, and complex classes in Python.

IntegersThis data type is represented with the help of int class. It consists of positive or negative whole numbers (without fractions or decimals). In Python, there’s no limit to how long integer values are often.

Example:
Python Code:

a = 2
print(a, "is of type", type(a))

Float: The float class represents this type. It is a true number with a floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer could even be appended to specify scientific notation.

Example:

b = 1.5
print(b, "is of type", type(b))
Output: 1.5 is of type

Complex Numbers: Complex numbers are represented by complex classes. It is specified as (real part) + (imaginary part)j, For example – 4+5j.

Example:

c = 8+3j
print(c, "is a type", type(c))
Output: (8+3j) is a type

String Data Type

The string is a sequence of Unicode characters. A string may be a collection of 1 or more characters put during a quotation mark, double-quote, or triple-quote. It can be represented using an str class.

Example:

string1=  “Hello World”
print(string1)
output: Hello World

We can perform several operations in strings like Concatenation, Slicing, and Repetition.

Concatenation: It includes the operation of joining two or more strings together.

Example:

String1 = "Hello"
String2 ="World"
print(String1+String2)
Output: Hello World

Slicing: Slicing is a technique for extracting different parts of a string.

Example:

String1 = "Hello"
print(String1[2:4])
Output: llo

Repetition: It means repeating a sequence of instructions a certain number of times.

Example:

Print(String1*5)
Output: HelloHelloHelloHelloHello

List Data Type

A list is formed (or created) by placing all the items (elements) inside square brackets [ ], separated by commas.

It can have any number of items that may or may not be of different types (integer, float, string, etc.).

A list is mutable, which suggests we will modify the list

Example:

List1 = [3,8,7.2,"Hello"]
print("List1[2] = ", List[2])
Output:  List1[2] = 7.2
print("List1[1:3] = ", List[1:3])
Output: List1[1:3] = [8, 7.2]

Updating the list: we can update the list.

List1[3] = "World"
#If we print the whole list, we can see the updated list.
print(List1)
Output: [3, 8, 7.2, ‘World’]

Tuple Data Type

A tuple is defined as an ordered collection of Python objects. The only difference between a tuple and a list is that tuples are immutable, i.e., tuples can’t be modified once created. It is represented by a tuple class. We can represent tuples using parentheses ( ).

Example:

Tuple = (25,10,12.5,"Hello")
print("Tuple[1] = ", Tuple[1])
Output: Tuple[1] =  10
print("Tuple[0:3] =", Tuple[0:3])
Output: Tuple[0:3] =  (25,10,12.5)

Set Data Type

A set is an unordered collection of items. Every set element is exclusive (no duplicates) and must be immutable (cannot be changed).

Example:

Set = {4,3,6.6,"Hello"}
print(Set)
Output: {‘Hello’, 3, 4, 6.6}

As the set is an unordered collection, indexing will be meaningless. Hence the slicing operator [ ] doesn’t work.

Set[1] = 12
Output: TypeError

Dictionary Data Type

In Python, Dictionary is an unordered collection of data values that stores data values like a map. Unlike other Data Types with only a single value as an element, a Dictionary consists of a key-value pair. Key-value is provided within the dictionary to form it more optimized. A colon (:) separates each key-value pair during a Dictionary, in the representation of a dictionary data type. Meanwhile, a comma (,) separates each key.

Syntax:
Key:value

Example:

Dict1 = {1:'Hello',2:5.5, 3:'World'}
print(Dict1)
Output: {1: ‘Hello’, 2: 5.5, 3: ‘World’}

We can retrieve the value by using the following method:

Example:

print(Dict[2])
Output: 5.5

We can update the dictionary by following methods as well:

Example:

Dict[3] = 'World'
print(Dict)
Output:
{1: ‘Hello’, 2: 5.5, 3: ‘World’}

Conclusion

If you’re reading this text, you’re probably learning Python or trying to become a Python developer. Learning Python or other programming languages begins by understanding the basic concepts of its foundation. You’ve covered the most commonly used data types used in Python programming in this article. Keep learning, and good luck in your journey to mastering Python!

Key Takeaways:

  • Data types are the classification or categorization of knowledge items.
  • The 6 standard data types in Python are Numeric, String, List, Tuple, Set, and Dictionary.

Frequently Asked Questions

Q1. What are the supported data types in Python?

A. Python supports several standard data types, including:
1. Numeric Types:
int: Integers, e.g., 10, -3, 0.
float: Floating-point numbers, e.g., 3.14, -2.5, 0.0.
complex: Complex numbers, e.g., 2+3j, -1-4j.
2. Sequence Types:
str: Strings of characters, e.g., “Hello”, ‘World’, “123”.
list: Ordered, mutable sequences, e.g., [1, 2, 3], [‘a’, ‘b’, ‘c’].
tuple: Ordered, immutable sequences, e.g., (1, 2, 3), (‘a’, ‘b’, ‘c’).
3. Mapping Type:
dict: Key-value pairs, e.g., {‘name’: ‘John’, ‘age’: 25}.
4. Set Types:
set: Unordered, mutable collection of unique elements, e.g., {1, 2, 3}, {‘a’, ‘b’, ‘c’}.
frozenset: Immutable set, e.g., frozenset({1, 2, 3}).
5. Boolean Type:
bool: Represents the truth values True and False.
6. Binary Types:
bytes: Immutable sequence of bytes, e.g., b’hello’, bytes([65, 66, 67]).
bytearray: Mutable sequence of bytes, e.g., bytearray(b’hello’), bytearray(3).
7. None Type:
None: Represents the absence of a value or a null value.

These are the most common standard data types in Python. Additionally, Python allows defining and using custom data types using classes and objects.

Q2. What is double data type in Python?

A. Python does not have a specific “double” data type like some other programming languages. Instead, Python uses the built-in “float” data type to represent floating-point numbers, which provides double-precision floating-point values. The “float” data type in Python can represent decimal numbers with a high level of precision, similar to the “double” data type in other languages.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Hello, my name is Prashant, and I'm currently pursuing my Bachelor of Technology (B.Tech) degree. I'm in my 3rd year of study, specializing in machine learning, and attending VIT University.

In addition to my academic pursuits, I enjoy traveling, blogging, and sports. I'm also a member of the sports club. I'm constantly looking for opportunities to learn and grow both inside and outside the classroom, and I'm excited about the possibilities that my B.Tech degree can offer me in terms of future career prospects.

Thank you for taking the time to get to know me, and I look forward to engaging with you further!

Responses From Readers

Clear

DebrajCode
DebrajCode

Hello Bro Nice Information

Programmer
Programmer

Good morning, As i was reading this article i found an error for the following example output: String1 = "Hello" print(String1[2:4]) Output: llo The output should be: ll Best regards, Anonymous programmer

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details

-->