Page 69 - Touhpad Ai
P. 69

# Convert integer to float
                   int_to_float = float(int_value)
                   # Convert float to integer
                   float_to_int = int(float_value)
                   # Convert string to integer
                   str_to_int = int(str_value)
                   # Convert boolean to integer
                   bool_to_int = int(bool_value)

                   # Print the casted values and their new types
                   print("Casted values and their new types:")
                   print("int_to_float:", int_to_float, type(int_to_float))
                   print("float_to_int:", float_to_int, type(float_to_int))
                   print("str_to_int:", str_to_int, type(str_to_int))
                   print("bool_to_int:", bool_to_int, type(bool_to_int))
                   Output:

                   Casted values and their new types:
                   int_to_float: 10.0 <class 'float'>
                   float_to_int: 15 <class 'int'>
                   str_to_int: 25 <class 'int'>
                   bool_to_int: 1 <class 'int'>
                 The type() function is used to display the data type of a variable or object.


                 Operations on List

                 We can perform different types of operations on a list, like accessing elements, adding elements, and removing elements.
                 Before performing any operation, we need to first create a list.


                 Creating a List
                 You can create a list by placing all the elements inside square brackets [], separated by commas.

                   L=[]               # creates an empty list
                   L=[1, 2, 3, 4, 5]     # creates a list with numeric values
                   fruits = ["kiwi", "pineapple", "cherry", "orange"] # creates a list with string values
                   mix=["India", 12, 34.5]           # creates a list with mixed data types
                   nested_list[[1, 2, 3], 12, 5, 6] # creates a nested list

                 A nested list is a type of list that contains another list as its element.

                 Indexing in Lists

                 Lists are indexed, which means each element has a unique position. The index starts at 0 for the first element, 1 for the
                 second, and so on. Negative indexing starts from the right to left with -1 being the last element.


                                                                       Length = 5
                                                             'p'    'r'    'o'   'b'    'e'
                                               positive index  0    1      2      3      4
                                              negative index  -5    -4     -3    -2     -1



                                                                                 Basic Concepts of Artificial Intelligence  67
   64   65   66   67   68   69   70   71   72   73   74