Difference between revisions of "Data structures and Loop"

From Biolecture.org
imported>Yesung Lee
(Created page with "<p>Data structures: list, tuple, dict, set </p> <p><Sequence></p> <p>: A type of data sturctures.</p> <p>Mutable sequence: list</p> <p>Immutable sequence: tuple, s...")
 
imported>Yesung Lee
 
Line 61: Line 61:
 
<p>&nbsp;</p>
 
<p>&nbsp;</p>
  
<p>&nbsp;</p>
+
<p><strong>&lt;Data structure: tuple &amp; string&gt;</strong></p>
  
<p>&nbsp;</p>
+
<p><strong>&lt;Tuple&gt;</strong></p>
 +
 
 +
<p>Notation: ()</p>
 +
 
 +
<p>ex) t1=(42, 1024, 23)</p>
 +
 
 +
<p>t2=(42, )</p>
 +
 
 +
<p>t1[2]=3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ERROR</p>
 +
 
 +
<p><strong>Tuple Unpacking:</strong></p>
 +
 
 +
<p>Ex)</p>
 +
 
 +
<p>my_tuple=(42, 1024, 23)</p>
 +
 
 +
<p>(first, second, third)= my_tuple</p>
 +
 
 +
<p>print(my_tuple)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(42, 1024, 23)</p>
 +
 
 +
<p>print(first, second, third)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 42 1024 23</p>
 +
 
 +
<p>print(type(my_tuple))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;class &#39;tuple&#39;&gt;</p>
 +
 
 +
<p>Ex)</p>
 +
 
 +
<p>var1=42</p>
 +
 
 +
<p>var2=1024</p>
 +
 
 +
<p>var1, var2=var2,var1</p>
 +
 
 +
<p>print(var1, var2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1024 42&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # SWAP!!</p>
 +
 
 +
<p>Ex)</p>
 +
 
 +
<p>def divide(dividend, divisor):</p>
 +
 
 +
<p>&nbsp; &nbsp; &quot;&quot;&quot;Return (quotient, remainder)&quot;&quot;&quot;&quot;</p>
 +
 
 +
<p>&nbsp; &nbsp; quotient=dividend//divisor</p>
 +
 
 +
<p>&nbsp; &nbsp; remainder=dividend%divisor</p>
 +
 
 +
<p>&nbsp; &nbsp; return(quotient, remainder)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # return a tuple</p>
 +
 
 +
<p>q,r= divide(13,3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#(4,1)</p>
 +
 
 +
<p>print(&#39;quotient=&#39;, q, &#39;, remainder=&#39;, r, sep=&#39;&#39;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;quotient=4, remainder=1</p>
 +
 
 +
<p><strong>&lt;Loops: While &amp; For&gt;</strong></p>

Latest revision as of 00:43, 4 October 2018

Data structures: list, tuple, dict, set 

<Sequence>

: A type of data sturctures.

Mutable sequence: list

Immutable sequence: tuple, str

<List>

notation: []

index start from 0.

Ex) 

t1=[42,1024, 23]

print(t1[0])                42

print(t1[1])              1024

t1[2]=7

print(t1)                 [42,1024,7]

Ex)

t=[42,1024, 23, 6, 28, 496]

print(t[1:4])               [1024,23,6]            (includes start point and exclude end point.)

print(t[3:])                 [6, 28, 496]           (includes start point to end.)

print(t[:2])                 [42, 1024]            (includes start and exclude end point.)

<Operations on list>

t=[42, 1024, 23]

print(1024 in t)               True

print(1024 not in t)          False

print(len(t))                      3           (number of the items.)

print(min(t))                    23

t2=[1, 2, 3]

t3=t+t2

t4=t*2

print(t3)                        [42, 1024, 23, 1, 2, 3]

print(t4)                        [42, 1024, 23, 42, 1024, 23]

 

<Data structure: tuple & string>

<Tuple>

Notation: ()

ex) t1=(42, 1024, 23)

t2=(42, )

t1[2]=3                              # ERROR

Tuple Unpacking:

Ex)

my_tuple=(42, 1024, 23)

(first, second, third)= my_tuple

print(my_tuple)                         (42, 1024, 23)

print(first, second, third)                            42 1024 23

print(type(my_tuple))                        <class 'tuple'>

Ex)

var1=42

var2=1024

var1, var2=var2,var1

print(var1, var2)                  1024 42                                  # SWAP!!

Ex)

def divide(dividend, divisor):

    """Return (quotient, remainder)""""

    quotient=dividend//divisor

    remainder=dividend%divisor

    return(quotient, remainder)            # return a tuple

q,r= divide(13,3)                                           #(4,1)

print('quotient=', q, ', remainder=', r, sep='')                                             quotient=4, remainder=1

<Loops: While & For>