Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Arrays

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Arrays

      Python's standard data types list, tuple and string are sequences. A sequence object is an ordered collection of items. Each item is characterized by incrementing index starting with zero. Moreover, items in a sequence need not be of same type. In other words, a list or tuple may consist of items of different data type.
      This feature is different from the concept of an array in C or C++. In C/C++, an array is also an indexed collection of items, but the items must be of similar data type. In C/C++, you have an array of integers or floats, or strings, but you cannot have an array with some elements of integer type and some of different type. A C/C++ array is therefore a homogenous collection of data types.
      Python's standard library has array module. The array class in it allows you to construct an array of three basic types, integer, float and Unicode characters.

      Syntax

      The syntax of creating array is
      import array
      obj = array.array(typecode[, initializer])

      Parameters

      typecode − The typecode character used to create the array.
      initializer − array initialized from the optional value, which must be a list, a bytes-like object, or iterable over elements of the appropriate type.

      Return type

      The array() constructor returns an object of array.array class

      Example

      import array as arr
      
      # creating an array with integer type
      a = arr.array('i', [1, 2, 3])
      print (type(a), a)
      
      # creating an array with char type
      a = arr.array('u', 'BAT')
      print (type(a), a)
      
      # creating an array with float type
      a = arr.array('d', [1.1, 2.2, 3.3])
      print (type(a), a)
      It will produce the following output
      <class 'array.array'> array('i', [1, 2, 3])
      <class 'array.array'> array('u', 'BAT')
      <class 'array.array'> array('d', [1.1, 2.2, 3.3])
      Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.
      Python array type is decided by a single character Typecode argument. The type codes and the intended data type of array is listed below
      typecode
      Python data type
      Byte size
      'b'
      signed integer
      1
      'B'
      unsigned integer
      1
      'u'
      Unicode character
      2
      'h'
      signed integer
      2
      'H'
      unsigned integer
      2
      'i'
      signed integer
      2
      'I'
      unsigned integer
      2
      'l'
      signed integer
      4
      'L'
      unsigned integer
      4
      'q'
      signed integer
      8
      'Q'
      unsigned integer
      8
      'f'
      floating point
      4
      'd'
      floating point
      8

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.