Python Data Types

Python Data Types Tutorial

Data types in Python define the type of value stored in a variable. Python is dynamically typed, meaning you don’t need to declare the type explicitly; it is assigned automatically.


1. Basic Data Types in Python


2. Numeric Data Types

(a) Integer (int)

x = 100
print(type(x))  # Output: <class 'int'>

(b) Floating Point (float)

pi = 3.1415
print(type(pi))  # Output: <class 'float'>

(c) Complex Numbers (complex)

z = 2 + 3j
print(type(z))  # Output: <class 'complex'>

3. Boolean Data Type (bool)

Boolean values represent True or False.

is_active = True
print(type(is_active))  # Output: <class 'bool'>

print(10 > 5)  # Output: True
print(10 == 20)  # Output: False

Booleans are often used in conditions and loops.


4. String Data Type (str)

Strings store sequences of characters.

text = "Hello, Python!"
print(type(text))  # Output: <class 'str'>

# Multi-line String
multiline = """This is
a multi-line string."""

Strings support indexing, slicing, and various methods for manipulation.


5. Sequence Data Types

(a) List (list)

  • Ordered, mutable (changeable), allows duplicates
  • Defined using [] brackets
numbers = [1, 2, 3, 4, 5]
print(type(numbers))  # Output: <class 'list'>

numbers.append(6)  # Add element
print(numbers)  # Output: [1, 2, 3, 4, 5, 6]

(b) Tuple (tuple)

  • Ordered, immutable (cannot be changed), allows duplicates
  • Defined using () brackets
coordinates = (10, 20, 30)
print(type(coordinates))  # Output: <class 'tuple'>

# Accessing elements
print(coordinates[0])  # Output: 10

Tuples are useful when data should not be modified.


(c) Range (range)

Used to generate a sequence of numbers.

r = range(5)  # Creates sequence: 0,1,2,3,4
print(list(r))  # Output: [0, 1, 2, 3, 4]

Often used in loops.


6. Set Data Types

(a) Set (set)

  • Unordered, mutable, no duplicate values
  • Defined using {} brackets
fruits = {"apple", "banana", "cherry"}
print(type(fruits))  # Output: <class 'set'>

fruits.add("orange")  # Add an element
print(fruits)

fruits.remove("banana")  # Remove an element
print(fruits)

Sets are useful when duplicates are not allowed.


(b) Frozen Set (frozenset)

  • Immutable version of set
fset = frozenset({1, 2, 3})
# fset.add(4)  # Error: frozenset is immutable
print(fset)

Useful when an immutable collection of unique values is needed.


7. Dictionary Data Type (dict)

  • Stores key-value pairs
  • Unordered, mutable
person = {"name": "Alice", "age": 25, "city": "New York"}
print(type(person))  # Output: <class 'dict'>

print(person["name"])  # Output: Alice

# Adding a new key-value pair
person["email"] = "alice@example.com"
print(person)

# Removing a key-value pair
del person["age"]
print(person)

Dictionaries are great for structured data storage.


8. Type Conversion (Type Casting)

Python allows explicit and implicit type conversion.

(a) Implicit Type Conversion (Done automatically by Python)

x = 10  # int
y = 3.5  # float

result = x + y  # int + float = float
print(result)  # Output: 13.5
print(type(result))  # Output: <class 'float'>

(b) Explicit Type Conversion (Done manually by the user)

a = "100"
b = int(a)  # Convert string to int
print(type(b))  # Output: <class 'int'>

x = 3.14
y = str(x)  # Convert float to string
print(type(y))  # Output: <class 'str'>

9. Mutable vs Immutable Data Types

Mutable types can be modified in place.
Immutable types create a new object when changed.


10. Checking Data Types (type())

Use type() to check a variable’s data type.

x = 42
print(type(x))  # Output: <class 'int'>

y = [1, 2, 3]
print(type(y))  # Output: <class 'list'>

z = (10, 20)
print(type(z))  # Output: <class 'tuple'>

Conclusion

  • Python provides several data types, including numbers, strings, lists, tuples, sets, and dictionaries.
  • Lists and dictionaries are mutable, while strings and tuples are immutable.
  • Type conversion helps in changing data types when needed.

🚀 Mastering data types is essential for writing efficient Python programs!

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment