Skip to main content
Engineering LibreTexts

12.1: Introduction

  • Page ID
    26344
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In broad terms, Python has two kinds of variables: Those that are simple and contain single items, examples being floats and integers; and compound types that contain many instances of items. These are called sequences. A string is a type of sequence because it is made up of a collection of individual characters. Although strings are often treated as a single item, it is possible to extract individual characters or groups of characters from them (a process known as slicing). Another type of sequence is the tuple (think of this as a contraction of multiple). A basic tuple can contain a group of floats or ints. It could also contain a group of sequences such as strings or even other tuples. For example, consider a tuple that contains a series of voltage settings. It might be initialized like this:

    V = (3.5, 2.0, 4.5, 6.0, 50.0, 10.0)
    

    The sequence is defined with enclosing parentheses and the individual items are separated by commas. Square brackets are used to access any given item or slice with the initial item at location 0, as shown in the examples below:

    print( V[1] )
    print( V[4] )
    

    These yield 2.0 and 50.0, respectively. A slice refers to a range of locations. Two values are specified separated by a colon: The first is the starting point while the second is the ending point (which is itself not included). It is also worth noting that a slice is itself a sequence and therefore will be printed with surrounding parentheses. For example:

    print( V[1:4] )
    

    This prints (2.0, 4.5, 6.0) If the start point is left off, it is assumed to be 0. Thus,

    print( V[:4] )
    

    yields (3.5, 2.0, 4.5, 6.0).

    Finally, a third argument may be added which indicates an increment. For example:

    print( V[0:4:2] )
    

    yields (3.5, 4.5).

    You can determine the number of items in a sequence by using the len() function:

    print( len(V) )
    

    This line will produce 6 (the number of items in the original declaration).

    Sequences are extremely useful when combined with loops. A loop control variable can be used to access the items in the sequence in order. Consider the program snippet below:

    for x in range(5):
         I = V[x] / R
         print( I )
    

    This would compute and print five different values of I. The first would use V[0], the second would use V[1], and so forth. Note that if you wanted this computation performed on every item in the sequence, you could modify the first line to:

    for x in range(len(V)):
    

    Although the examples above use a tuple filled with floats, the same sort of manipulation can be performed on a simple string:

    s = “abcdefg”
    print( s[1] )
    print( s[0:4] )
    

    These yield b and abcd, respectively. As previously mentioned, a tuple may hold other sequences. This means that it’s possible to have a tuple filled with strings:

    c = (‘black’, ‘brown’, ‘red’, ‘orange’, ‘yellow’, ‘green’)
    print( c[1:3] )
    

    This bit of code will print (‘brown’, ‘red’). It is possible to “drill down” into these strings (sequences) by using a second pair of brackets:

    print( c[1][2] )
    print( c[1][0:2] )
    

    These lines will print out o and br, respectively.


    This page titled 12.1: Introduction is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?