04. Data Types in Python

Welcome to Day 4 of our 60 Days of Python series! Today, we dive into the fundamental data types in Python, a crucial aspect for every programmer to understand. Let’s explore these data types with detailed explanations and examples.

Today we are not going to Nuke, We will try to Nuke the next day to learn ‘List data type’.   

[ex: theory topic I collected from internet]

1. Numeric Types

a. Integer (int)

Integers are whole numbers without a decimal point. They can be positive, negative, or zero. Integers are typically used when you need to count discrete items or perform operations that require whole numbers.


					a = 10
b = -5
c = 0
# Output: <class 'int'>
# Output: <class 'int'>
# Output: <class 'int'>


In the example above, a is a positive integer, b is a negative integer, and c is zero. All of them belong to the int type.

b. Float (float)

Floats are numbers with a decimal point. They are used when precision is required, such as in calculations involving fractions, percentages, or measurements.


					x = 10.5
y = -7.8
z = 3.0

# Output: <class 'float'>
# Output: <class 'float'>
# Output: <class 'float'>


Here, x, y, and z are floating-point numbers. Even though z looks like an integer, it is considered a float because of the decimal point.

c. Complex (complex)

Complex numbers consist of a real part and an imaginary part, represented as a + bj, where a is the real part and b is the imaginary part. Complex numbers are useful in scientific computations.


					a = 2 + 3j
b = -1j
c = 3 + 0j
# Output: <class 'complex'>
# Output: <class 'complex'>
# Output: <class 'complex'>


In this example, a has both a real part (2) and an imaginary part (3j), b is purely imaginary, and c is a real number represented in complex form.

2. Sequence Types

a. String (str)

Strings are sequences of characters enclosed in single, double, or triple quotes. Strings are used to represent text data.


					name = "John Doe"
greeting = 'Hello, World!'
multiline = """This is a
multiline string."""

# Output: <class 'str'>
# Output: <class 'str'>
# Output: <class 'str'>


Strings can be manipulated in many ways. You can concatenate them, slice them, and use various methods to transform and analyze the text.

b. List (list)

Lists are ordered, mutable collections of items. Items can be of different types. Lists are useful for storing collections of related items.


					numbers = [1, 2, 3, 4, 5]
mixed = [1, "Hello", 3.14, True]

# Output: <class 'list'>
# Output: <class 'list'>


Lists allow you to add, remove, and modify elements. They support various methods like append(), remove(), and sort(). We will talk later about this topic.

c. Tuple (tuple)

Tuples are ordered, immutable collections of items. Like lists, items can be of different types. Tuples are often used to group related data that should not be changed.


					coordinates = (10, 20)
info = ("Alice", 30, "Engineer")
# Output: <class 'tuple'>
# Output: <class 'tuple'>


Tuples are immutable, meaning once defined, their elements cannot be changed. They are useful for fixed collections of items.

3. Mapping Type

a. Dictionary (dict)

Dictionaries are unordered collections of key-value pairs. Keys must be unique and immutable, while values can be of any type. Dictionaries are useful for storing data that can be quickly retrieved via a key.


					person = {"name": "John", "age": 25, "city": "New York"}

print(type(person))  # Output: <class 'dict'>
print(person["name"])  # Output: John


In dictionaries, you can add, remove, and modify key-value pairs. They support methods like keys(), values(), and items().

4. Set Types

a. Set (set)

Sets are unordered collections of unique items. They are useful for membership testing and eliminating duplicate entries.


					fruits = {"apple", "banana", "cherry"}

# Output: <class 'set'>
# Output: {'banana', 'apple', 'cherry'}


Sets support various methods like add(), remove(), and mathematical operations like union and intersection.

b. Frozen Set (frozenset)

Frozen sets are immutable sets. They have all the characteristics of sets but cannot be modified after creation.


					immutable_fruits = frozenset(["apple", "banana", "cherry"])

# Output: <class 'frozenset'>
# Output: frozenset({'banana', 'apple', 'cherry'})


Frozen sets are useful in situations where you need a set that should remain constant.

5. Boolean Type

a. Boolean (bool)

Booleans represent one of two values: True or False. They are commonly used in conditional statements and comparisons.


					is_python_fun = True
is_sky_green = False

# Output: <class 'bool'>
# Output: <class 'bool'>


Booleans result from comparisons and logical operations. They are essential in controlling the flow of programs using conditionals and loops.

6. None Type

a. NoneType

The None type represents the absence of a value. It is often used to indicate that a variable has no value assigned yet.


					value = None

# Output: <class 'NoneType'>


None is commonly used as a default value for function arguments or to reset variables.


Understanding Python’s data types is fundamental for effective programming. Each type is suited for different kinds of tasks, and knowing when and how to use them can greatly enhance your coding efficiency. Experiment with these examples and explore how these data types can be used in your projects.

In our next few classes, we’ll dive deeper into the ‘List’ data type, exploring its functionalities and practical applications. Additionally, we’ll apply our Python skills to real-world scenarios with practical Foundry Nuke examples. Stay tuned for more hands-on learning and exciting insights! Happy coding!

join my 60-day Nuke Python challenge!

Subscribe now!


Leave a Comment

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