Skip to main content

Python Data Types

Understanding Python Data Types with Examples

Python is a dynamically typed language, meaning you don't need to declare the type of a variable when you create it. The type is automatically assigned based on the value you provide. However, understanding data types is crucial for efficient coding and avoiding bugs in your programs. In this blog, we’ll explore the various data types in Python with examples.

1. Numeric Data Types

Python supports several numeric types, including integers, floating-point numbers, and complex numbers.

  • Integers (int): These are whole numbers without a fractional part.

age = 25

print(type(age))  # Output: <class 'int'>

 

  • Floating-point (float): These are numbers with a fractional part.

height = 5.9

print(type(height))  # Output: <class 'float'>

 

  • Complex numbers (complex): These consist of a real part and an imaginary part.

complex_number = 3 + 4j

print(type(complex_number))  # Output: <class 'complex'>

 

2. Sequence Data Types

Python provides several types to represent sequences of data.

  • Strings (str): Strings are immutable sequences of characters.

name = "Deepak"

print(type(name))  # Output: <class 'str'>

 

  • Lists (list): Lists are mutable sequences, typically used to store collections of homogeneous items.

fruits = ["apple", "banana", "cherry"]

print(type(fruits))  # Output: <class 'list'>

 

  • Tuples (tuple): Tuples are immutable sequences, typically used to store collections of heterogeneous items.

coordinates = (10, 20)

print(type(coordinates))  # Output: <class 'tuple'>

 

3. Mapping Data Type

  • Dictionaries (dict): Dictionaries are unordered collections of key-value pairs. Keys must be unique and immutable.

person = {"name": "Deepak", "age": 41}

print(type(person))  # Output: <class 'dict'>

 

4. Set Data Types

Sets are unordered collections of unique elements.

  • Set (set): A set is mutable and does not allow duplicate elements.

unique_numbers = {1, 2, 3, 4, 4, 5}

print(unique_numbers)  # Output: {1, 2, 3, 4, 5}

print(type(unique_numbers))  # Output: <class 'set'>

 

  • Frozenset (frozenset): An immutable version of a set.

frozen_numbers = frozenset([1, 2, 3, 4, 4, 5])

print(frozen_numbers)  # Output: frozenset({1, 2, 3, 4, 5})

print(type(frozen_numbers))  # Output: <class 'frozenset'>

 

5. Boolean Data Type

  • Boolean (bool): Represents one of two values: True or False.

is_adult = True

print(type(is_adult))  # Output: <class 'bool'>

 

6. Binary Data Types

Python provides types to handle binary data.

  • Bytes (bytes): Immutable sequences of bytes.

byte_data = b"Hello"

print(type(byte_data))  # Output: <class 'bytes'>

 

  • Bytearray (bytearray): Mutable sequences of bytes.

mutable_byte_data = bytearray(b"Hello")

print(type(mutable_byte_data))  # Output: <class 'bytearray'>

 

  • Memoryview (memoryview): Allows you to access the internal data of an object that supports the buffer protocol without copying.

mv = memoryview(byte_data)

print(type(mv))  # Output: <class 'memoryview'>

 

7. None Type

  • NoneType (None): Represents the absence of a value.

result = None

print(type(result))  # Output: <class 'NoneType'>

 

Final Remarks

Understanding Python data types is essential for writing effective code. Each type serves a specific purpose, and knowing when to use which type can significantly improve the performance and readability of your programs. This is just fundamental information about the data types. In next blogs you will study every data type in detail.

Happy coding!

 

Comments

Popular posts from this blog

Difference between Bootloader & Firmware Update

Updating the bootloader and updating the firmware in a microcontroller are two distinct processes, each serving different purposes and involving different steps. Here's a breakdown of the differences: Bootloader Update Purpose : The bootloader is a small program that runs before the main application. It is responsible for initializing the microcontroller and loading the main application firmware. Updating the bootloader might be necessary to add new features, fix bugs, or improve the boot process.   Process : 1.       Enter Bootloader Mode : To update the bootloader, the microcontroller must be put into a special mode. This often involves setting specific pins, pressing buttons, or using a specific command through an existing firmware interface. 2.      Communication Interface : The bootloader update typically uses a specific communication interface such as UART, SPI, I2C, or USB. 3.   ...

AND Logic Gate by Transistors

Creating an AND gate using transistors is a fundamental exercise in digital electronics. In this post, I will guide you through the process of building an AND gate with transistors, explaining each step in detail.   What is an AND Gate? An AND gate is a basic digital logic gate that outputs TRUE or HIGH (1) only when all its inputs are true or high. If any of the inputs are false or low (0), the output is false or low (0). The truth table for a two-input AND gate is as follows: Input A Input B Output 0 0 0 0 1 0 1 0 0 1 1 1   Components Needed To build an AND gate, you will need the following components: NPN Transistors: BC547 (2 Nos.); Q1, Q2. Resistors : 10K (2 Nos) - R1, R2. Resistors : 1K (1 No) - R3. LED: 5mm Red (1 No) – L1, for output indication. Switches: ...

How to control Digital Output using Serial Monitor

Hello Friends, in this blog I will share with you how to control Arduino Digital Output with Serial Monitor. I am using One Arduino Uno, One 330 ohm resistor, One LED, 2 connecting cable, and one small Bread board. LED is connected to pin number 12, with the help of resistor 330 ohm. Below is the sketch: // Code by Deepak Sharma // The code is to control one digital output by serial monitor const int LED = 12; void setup() {   // put your setup code here, to run once:   Serial.begin(9600);   pinMode(LED, OUTPUT); } void loop() {   // put your main code here, to run repeatedly:   if (Serial.available()) {     String input = Serial.readStringUntil('\n');     input.trim();     if (input.equalsIgnoreCase("ON")) {       digitalWrite(LED, HIGH);       Serial.println("LED ON");     }     else if (input.equalsIgnoreCase("OFF")) {       digitalWrite(LED, LOW);     ...