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

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for

How to drive high voltage/current load by small voltage signal from a microcontroller?

Sometimes we need to control or drive a high voltage and heavy current load by a small voltage signal. For example, if you want to control water motor with your microcontroller output. We know that microcontroller gives only 5v output which is not sufficient to drive a heavy motor. This circuit, about which this post is, is very-very useful for electronics engineer and hobbyist. So pay attention! For this circuit the material we need is… General Purpose PCB - 5x5 cm. Transistor KN 2222A (TO-92) - 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Tools. Concept: Weak signal triggers the transistor and transistor acts as a switch for the relay. You can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed).

How to Read Analog Input & Use PWM pin as Analog Output

  Analog Input: An analog signal can take on any number of values. To measure the value of analog signals, Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. There is an inbuilt function to read Analog value; analogRead(pin_number). This function converts the value of the voltage on the analog input pin and returns a digital value ranges from 0 to 1023, relative to the reference value. The default reference voltage is 5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino boards). This function has only one parameter, which is the pin number.     Analog Output: The Arduino does not have any built-in digital-to-analog converter (DAC), but it can do pulse-width modulation (PWM); a digital signal to achieve some of the functions of an analog output. The function analogWrite(pin, value) is used to output a PWM signal. In the function ‘pin’ is the pin number used for the PWM output. ‘value’ is a number proportiona