Docsity

Lists, Loops and Conditionals, Study notes of Computer Programming

Partial preview of the text

Download Lists, Loops and Conditionals and more Study notes Computer Programming in PDF only on Docsity!

Lists, Loops and

Conditionals

HORT 59000

Lecture 11

Instructor: Kranthi Varala

Core data types

  • Numbers
  • Strings
  • Lists
  • Dictionaries
  • Tuples
  • Files
  • Sets

Lists: Common Methods

  • L.append() : Adds one item to the end of the list.
  • L.extend() : Adds multiple items to the end of the list.
  • L.pop(i) : Remove item ‘i’ from the list. Default:Last.
  • L.reverse() : Reverse the order of items in list.
  • L.insert(i,item): Inserts ‘item’ at position i.
  • L.remove(item) : Finds ‘item’ in list and deletes it from the

list.

  • L.sort(): Sorts the list in- place i.e., changes the sequence in

the list. (Sorting mixed data types only works on python2)

MultiDimensional Lists

  • Lists are of arbitrary length and and easily be nested.
  • Simplest nested lists are 2 – dimensional matrices.
  • my2DList = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] 1 2 3 4

my2DList 0 1 2 3

Arbitrary dimensional Lists

  • Nested Lists need not be of the same length.
  • my2DList = [[1,2,3,’a’],[5,6,7],[9,10,’e’,12,’cat’],[’beta’,14,15,16]] 1 2 3 a

e 12 cat beta 14 15 16 my2DList 0 1 2 3

Arbitrary dimensional Lists

  • Nested Lists can have arbitrary depth as well.
  • subL = [['p','q'],['r','s']]
  • my2DList = [[1,2,3,'a'],[5,6,7,'cat'],[9,10,'e', 12 ],['beta',14,15,subL]] 1 2 3 a

cat

e 12 beta 14 15 my2DList 0 1 2 3 p q r s

Lists as sequences of references

  • myList = [’Name’,[Month,Date,Year],Address,[Home,Cell]] [Ref] [Ref] [Ref] [Ref] int int int Str Str Str Str

Lists are mutable!!

>>>subL = [[’p’,’q’],[‘r’,’s’]]

myList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,subL]] myList [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’q’],[‘r’,’s’]]]]

>>>subL[0][1] = ‘z’

>>>myList

[[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’z’],[‘r’,’s’]]]]

Relational Operators

  • These operators compare the value of two ‘expressions’ and returns a Boolean value.
  • Beware of comparing across data types, especially when reading values in from command line or files.

Relational Operators

== equal True if expressions are equal

!= not equal True if expressions are not equal

> Greater than True if left is greater than the right

< Less than True if left is less than the right

>= greater than OR equal

<= less than OR equal

is identity True if the left is the same object as right

in contains True if the object on left is contained in object

on right (Useful for finding values in list)

Boolean Operators

Combines two or more statements that return a Boolean value.

A and B True if both A and B are true

A or B True if either A or B is true

not A Reverse the Boolean given by A

xor(A,B) True if only one of A or B is True

A B A and B A or B Not A xor(A,B) TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE

General Python Syntax rules

  • End of line is end of statement
  • Statements at the same indentation level are in the same block (e.g., within a loop or condition)
  • End of indentation is end of block
  • Exceptions:
    • Semi colon ; separates statements on the same line
    • Single line blocks are allowed without indentation

If/elif/else statements if test1:

statement 1

elif test2:

statement 2

else:

statement 3

  • Both the elif and else blocks are optional.

if/elif/else statements