Tag: Lesson 2: Data Types

  • Lesson 2: Data Types

    In this lesson, we will explore the different data types in Python, including strings, integers, floats, and booleans. We will also cover type conversion, which allows you to change the type of a variable from one data type to another. Understanding data types is crucial for writing effective Python programs, as different operations and functions require specific data types.

    1. Strings

    A string in Python is a sequence of characters enclosed in either single quotes (‘) or double quotes (“). Strings are used to represent text or any data that can be considered a sequence of characters.

    Syntax:

    python
    my_string = “Hello, World!”

    another_string = ‘Python is fun!’

     

    Key Operations with Strings:

    Concatenation: You can combine (concatenate) strings using the + operator.

    python
    greeting = “Hello” + ” ” + “World”

    print(greeting)  # Output: Hello World

    Repetition: You can repeat a string multiple times using the * operator.

    python
    repeated = “Python” * 3

    print(repeated)  # Output: PythonPythonPython

    Accessing characters: You can access individual characters of a string using indexing. Python uses zero-based indexing, meaning the first character is at index 0.

    python
    my_string = “Hello”

    print(my_string[0])  # Output: H

    String methods: Python provides various built-in methods to manipulate strings, such as .lower(), .upper(), .split(), and .replace().

    python
    my_string = “Hello, World!”

    print(my_string.lower())  # Output: hello, world!

    print(my_string.replace(“World”, “Python”))  # Output: Hello, Python!

    2. Integers

    An integer is a whole number, either positive, negative, or zero, that does not have any fractional part. Integers are commonly used for counting or performing arithmetic operations.

    Syntax:

    python
    my_integer = 10

    negative_integer = -5

     

    Key Operations with Integers:

    Arithmetic operations: You can perform arithmetic operations like addition, subtraction, multiplication, division, and modulus (remainder).

    python
    a = 10

    b = 3

    print(a + b)  # Output: 13

    print(a – b)  # Output: 7

    print(a * b)  # Output: 30

    print(a / b)  # Output: 3.333…

    print(a % b)  # Output: 1 (remainder of 10 divided by 3)

    Integer division: Use the // operator to perform division that returns an integer result (floor division).

    python
    print(a // b)  # Output: 3

    Exponentiation: The ** operator is used to calculate powers of a number.
    python
    CopyEdit
    print(a ** b)  # Output: 1000 (10 raised to the power of 3)

    3. Floats

    A float is a number that has a decimal point. Floats are used to represent real numbers that require precision, such as measurements, currency, or scientific calculations.

    Syntax:

    python
    my_float = 10.5

    another_float = -3.14

     

    Key Operations with Floats:

    Arithmetic operations: Floats can also participate in the basic arithmetic operations like integers.

    python
    a = 5.5

    b = 2.0

    print(a + b)  # Output: 7.5

    print(a * b)  # Output: 11.0

    print(a / b)  # Output: 2.75

    Precision: When working with floating-point numbers, precision might be an issue due to the way computers represent decimals.
    python
    CopyEdit
    print(0.1 + 0.2)  # Output: 0.30000000000000004 (shows rounding issue)

    Note: You can control the display of float values with formatting or rounding methods, like round().

    python
    print(round(0.1 + 0.2, 1))  # Output: 0.3

     

    4. Booleans

    A boolean data type represents one of two values: True or False. Booleans are often used in conditional statements and loops to control the flow of a program.

    Syntax:

    python
    is_active = True

    is_logged_in = False

     

    Key Operations with Booleans:

    Logical operations: You can use logical operators such as and, or, and not to combine or negate boolean values.

    python
    a = True

    b = False

    print(a and b)  # Output: False

    print(a or b)   # Output: True

    print(not a)    # Output: False

    Comparison operations: Booleans are commonly used in comparisons. For example, checking if two values are equal or if one is greater than another:

    python
    a = 5

    b = 10

    print(a < b)  # Output: True

    print(a == b)  # Output: False

    5. Type Conversion

    Type conversion refers to the process of converting a variable from one data type to another. In Python, type conversion can be explicit or implicit.

    Implicit Type Conversion (Automatic)

    Python automatically converts types when it is safe to do so. For example, when an integer is used with a float, Python will automatically convert the integer to a float to perform the operation.

    Example:

    python
    a = 5       # integer

    b = 2.5     # float

    result = a + b  # The integer a is automatically converted to float

    print(result)  # Output: 7.5

     

    Explicit Type Conversion (Manual)

    Python provides functions that allow you to explicitly convert between different types. The common functions for type conversion include:

    • int(): Converts a value to an integer.
    • float(): Converts a value to a float.
    • str(): Converts a value to a string.
    • bool(): Converts a value to a boolean.

    Examples:

    python
    # Converting a string to an integer

    x = “10”

    y = int(x)  # Now y is an integer

    print(y)  # Output: 10

     

    # Converting a float to an integer (this truncates the decimal part)

    a = 3.14

    b = int(a)

    print(b)  # Output: 3

     

    # Converting an integer to a string

    num = 100

    num_str = str(num)

    print(num_str)  # Output: “100”

     

    Note: When converting between incompatible types, Python will raise a ValueError.

    python
    # Trying to convert a non-numeric string to an integer

    x = “hello”

    y = int(x)  # This will raise a ValueError