What Is Data Type in Python With Example?

//

Angela Bailey

Python is a versatile programming language that allows developers to work with different types of data. Understanding data types is crucial in Python as it determines the kind of operations that can be performed on a particular variable. In this tutorial, we will explore the concept of data types in Python and provide examples to illustrate their usage.

What are Data Types?

Data types define the category of values that can be assigned to variables in programming languages. Each data type has its own set of rules and operations associated with it. In Python, there are several built-in data types that are commonly used:

  • Integer (int): Integers represent whole numbers without any decimal points. For example, 5 and -10 are integers.
  • Float: Floats represent numbers with decimal points. For example, 3.14 and -0.5 are floats.
  • String (str): Strings represent textual data enclosed within single or double quotes. For example, “Hello, World!”

    is a string.

  • Boolean (bool): Booleans represent either true or false values which are used for logical operations. The values True and False belong to this data type.
  • List: Lists are ordered collections of items enclosed within square brackets []. Each item can be of any data type, including lists themselves.
  • Tuple: Tuples are similar to lists but are immutable, meaning their elements cannot be changed after creation.
  • Dictionary: Dictionaries store key-value pairs enclosed within curly braces {}. They are used to represent mappings between unique keys and their corresponding values.

Examples of Data Types in Python

Let’s take a look at some examples to understand how data types are used in Python:

Example 1: Integer

To declare an integer variable, you can simply assign a whole number to it:


x = 5
print(x)  # Output: 5

Example 2: Float

Floats can be assigned decimal values:


y = 3.14
print(y)  # Output: 3.14

Example 3: String

Strings can be created using single or double quotes:


message = "Hello, World!"
print(message)  # Output: Hello, World!

Example 4: Boolean

Booleans are often used in conditional statements:


is_true = True
is_false = False

if is_true:
    print("It is true!") # Output: It is true! 

else:
    print("It is false!") 

Example 5: List

List variables can store multiple values:


fruits = ["apple", "banana", "cherry"]
print(fruits)   # Output: ["apple", "banana", "cherry"]

Example 6: Tuple

Tuples are created by enclosing elements within parentheses ():


coordinates = (10, 20)
print(coordinates)   # Output: (10, 20)

Example 7: Dictionary

Dictionaries use key-value pairs to represent data:


person = {
    "name": "John",
    "age": 25,
    "city": "New York"
}
print(person)   # Output: {"name": "John", "age": 25, "city": "New York"}

These are just a few examples of the commonly used data types in Python. By understanding and utilizing the appropriate data type for each variable, you can effectively manipulate and process data within your Python programs.

Now that you have a better understanding of data types in Python, you can confidently work with variables and perform operations based on their respective types. Remember to always consider the specific requirements of your program when choosing the appropriate data type.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy