Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python Data Types

Python has several built-in data types that you can use to represent and manipulate data in your programs. In this tutorial, we will discuss the most commonly used Python data types.

  • Numeric Data Types:

Python has three numeric data types: integers, floating-point numbers, and complex numbers.

a) Integers:

Integers are whole numbers, either positive or negative, without any decimal point. In Python, you can represent integers as follows:

a = 5
b = -3
c = 0

b) Floating-point numbers:

Floating-point numbers are numbers that have a decimal point. They can also be expressed using scientific notation. In Python, you can represent floating-point numbers as follows:

a = 3.14
b = -0.5
c = 1.5e2  # 1.5 * 10^2 = 150.0

c) Complex numbers:

Complex numbers have a real part and an imaginary part, written as a + bj, where a and b are real numbers, and j is the imaginary unit. In Python, you can represent complex numbers as follows:

a = 2 + 3j
b = 1 - 1j
  • String Data Type:

Strings are sequences of characters enclosed in single quotes ('), double quotes ("), or triple quotes (''' or """). In Python, you can represent strings as follows:

a = 'Hello, World!'
b = "Python is fun."
c = '''This is a multiline
string in Python.'''
  • List Data Type:

Lists are ordered, mutable (changeable) sequences of items. Items can be of different data types. In Python, you can represent lists as follows:

a = [1, 2, 3, 4, 5]
b = ['apple', 'banana', 'cherry']
c = [1, 'apple', 3.14, [2, 4, 6]]
  • Tuple Data Type:

Tuples are similar to lists, but they are immutable (cannot be changed). In Python, you can represent tuples as follows:

a = (1, 2, 3, 4, 5)
b = ('apple', 'banana', 'cherry')
c = (1, 'apple', 3.14, (2, 4, 6))
  • Dictionary Data Type:

Dictionaries are unordered collections of key-value pairs. They are mutable and have unique keys. In Python, you can represent dictionaries as follows:

a = {'a': 1, 'b': 2, 'c': 3}
b = {'name': 'Alice', 'age': 30, 'occupation': 'Engineer'}
  • Set Data Type:

Sets are unordered collections of unique items. They are mutable and do not allow duplicate items. In Python, you can represent sets as follows:

a = {1, 2, 3, 4, 5}
b = {'apple', 'banana', 'cherry'}
  • Boolean Data Type:

Booleans represent the truth values True and False. They are often used in conditional statements and comparisons. In Python, you can represent booleans as follows:

a = True
b = False

In conclusion, Python has several built-in data types, including numeric types (integers, floating-point numbers, and complex numbers), strings, lists, tuples, dictionaries, sets, and booleans.

  1. Numeric data types in Python: int, float, complex:

    • Description: Python supports three main numeric data types: integers (int), floating-point numbers (float), and complex numbers (complex).
    • Code:
    # Integer
    x = 5
    
    # Float
    y = 3.14
    
    # Complex
    z = 2 + 3j
    
  2. String data type in Python and string manipulation:

    • Description: Strings (str) are sequences of characters. Python provides various string manipulation methods.
    • Code:
    # String
    text = "Hello, World!"
    
    # String manipulation
    upper_case = text.upper()
    lower_case = text.lower()
    substring = text[7:12]
    
  3. List data type in Python and list operations:

    • Description: Lists (list) are ordered, mutable collections. Various list operations are available.
    • Code:
    # List
    numbers = [1, 2, 3, 4, 5]
    
    # List operations
    numbers.append(6)
    numbers.extend([7, 8])
    numbers.pop(2)
    
  4. Tuple data type in Python and tuple usage:

    • Description: Tuples (tuple) are ordered, immutable collections.
    • Code:
    # Tuple
    coordinates = (3, 4)
    
    # Tuple usage
    x, y = coordinates
    
  5. Dictionary data type in Python and dictionary operations:

    • Description: Dictionaries (dict) are unordered collections of key-value pairs.
    • Code:
    # Dictionary
    person = {'name': 'Alice', 'age': 25}
    
    # Dictionary operations
    person['city'] = 'New York'
    del person['age']
    
  6. Set data type in Python and set operations:

    • Description: Sets (set) are unordered collections of unique elements.
    • Code:
    # Set
    fruits = {'apple', 'banana', 'orange'}
    
    # Set operations
    fruits.add('kiwi')
    fruits.remove('banana')
    
  7. Boolean data type in Python and boolean operations:

    • Description: Booleans (bool) represent truth values: True or False.
    • Code:
    # Boolean
    is_python_fun = True
    is_learning = False
    
    # Boolean operations
    result = is_python_fun and not is_learning
    
  8. Custom data types and user-defined classes in Python:

    • Description: Python allows defining custom data types using classes.
    • Code:
    # Custom class
    class Point:
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
    # Create an instance
    point1 = Point(3, 4)