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

Slides:



Advertisements
Similar presentations
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong. Administrivia Homework 3 graded.
Advertisements

LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
LING/C SC/PSYC 438/538 Lecture 5 9/8 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open, References Perl modules Homework 2: due next Monday by midnight.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Continuing with Perl Homework 3: first Perl homework – due Sunday by midnight – one PDF file, by .
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
LING/C SC/PSYC 438/538 Lecture 3 8/30 Sandiway Fong.
Built-in Data Structures in Python An Introduction.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Tuples Chapter 10 Python for Informatics: Exploring Information
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong. Homework 4 Submit one PDF file Your submission should include code and sample runs Due date Monday 21.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
Advanced Python Idioms
Python Variable Types.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Intro To Pete Alonzi University of Virginia Library
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong.
Dictionaries, File operations
LING/C SC/PSYC 438/538 Lecture 10 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING 388: Computers and Language
CISC101 Reminders Quiz 2 this week.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Perl Variables: Array Web Programming.
Introduction to Python
CSc 110, Spring 2018 Lecture 33: Dictionaries
Introduction to Perl Jarrad Battaglia.
LING/C SC/PSYC 438/538 Lecture 10 Sandiway Fong.
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Python for Informatics: Exploring Information
Lists in Python Outputting lists.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong.
Python Primer 1: Types and Operators
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
CISC101 Reminders Assn 3 sample solution is posted.
Advanced Python Idioms
CISC101 Reminders Assignment 2 due today.
Advanced Python Idioms
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Sample lecture slides.
Karan Thaker CS 265 Section 001
Python - Tuples.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Presentation transcript:

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

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

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 my @a = qw(one, two, three); my $string = @a." is my number"; print "$string\n";

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)

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

Conditionals and Looping

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

ARGV Command line: perl argv.perl 1 2 3

General Looping

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

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

Python dict

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

Python dict list comprehension

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

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

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

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

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

Python list ranges Perl has a range operator: .. less powerful in some ways, more powerful in others https://perldoc.perl.org/perlop.html#Range-Operators

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

Homework 2 Q1: Perl. Q2: Perl. Q3: Python. Q4: Python. What does @a = 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

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