Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong."— Presentation transcript:

1 LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong

2 Today's Topics Perl data type coercion, conditions and looping
More on Perl hashes and Python dicts Homework 2

3 Implicit Coercion Example: Example:
Perl features implicit coercion of data types Example: the following program prints Equal! == is the numeric equality comparison operator my $one_string = "1"; my $one_number = 1; if ($one_string == $one_number) { print "Equal!\n" } else { print "Different!\n" } Example: the following program prints 3 is my number . is the string concatenation operator = qw(one, two, three); my $string is my number"; print "$string\n";

4 Implicit Coercion print "4" * 4 16
print "4" x 4 (“x” is the repetition operator) 4444 @a = (4) x 4 (in list context) (4, 4, 4, 4)

5 Conditionals and Looping
if < 10 ) { print “Small array\n” } else {print “Big array\n” } here is a scalar = size of array unless > 10) { print } Note: if size of array a is ≤ 10, it prints the contents of array a Ungraded Homework Exercise: look up the equivalents in Python ( do they always exist?

6 Conditionals and Looping

7 Conditionals and Looping
Looping over arrays %fruits = qw(apple green orange orange lemon yellow); foreach $f (keys %fruits) { print $f, " => ", $fruits{$f}, "\n” } gives output: lemon => yellow apple => green orange => orange Note: keys %fruits = ("lemon", "apple", "orange") is an array

8 ARGV Command line: perl argv.perl 1 2 3

9 General Looping

10 More on Perl hash tables
Example: Output: Unique key constraint:

11 Python dict Lists and tuples are indexed by whole numbers (from 0)
Dictionaries are indexed by a key (usually a string) and return some value associated with the key Note: use of curly braces Dictionaries are not ordered (like sets) – see next slide Methods keys(), values(), items() Refer to key + value as an item: encoded as a tuple

12 Python dict

13 Python dict Dictionary order preservation depends on the version of Python used … Python 3.6

14 Python dict list comprehension

15 Python dict How to print the contents of a dictionary?
Use a for-loop and method items(): k,v is a tuple

16 Python dict All values are lists Advantage: simplifies the code
Trade-off Values are lists or a string Advantage: simpler-looking dict

17 Python dict Works too! Less transparent: relies on a
Python-particuar quirk …

18 Python dict function zip() pairs up elements from two (or more) lists into an iterable

19 Python dict How to get that Perl list data structure format into a Python dict?

20 Python list ranges Perl has a range operator: ..
less powerful in some ways, more powerful in others

21 Python dict function zip() doesn't always work quite the same in all versions …

22 Homework 2 Q1: Perl. Q2: Perl. Q3: Python. Q4: Python.
What = 4 x 4 do? Q2: Perl. @l1 = ("a", "e", "i", "o" , "u"); @l2 = ("あ", "え", "い", "お", "う"); write a program that builds a hash that maps romaji to hiragana, e.g. $h{i} should be "い" Give examples to show your program works Hint: use a loop and shift (or pop) Q3: Python. Do the same for l1 and l2 in Python using zip. Q4: Python. Do the same, without zip, using list comprehensions

23 Homework 2 Instructions: email me submit everything in one PDF file!
subject of course number, your name, homework 2 cite any discussion or source due date: Monday 10th by midnight


Download ppt "LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong."

Similar presentations


Ads by Google