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

Slides:



Advertisements
Similar presentations
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
Advertisements

Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Genome Sciences 373 Genome Informatics Quiz Section 2 April 7, 2015.
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.
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.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong. Adminstrivia Homework 4 not yet graded …
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
Advanced Python Idioms
10 - Python Dictionary John R. Woodward.
Python Variable Types.
Generating Random Numbers
Tuples and Lists.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Lecture 5: Some more Java!
CSc 120 Introduction to Computer Programing II
Chapter 5 - Control Structures: Part 2
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
Lecture 10 Data Collections
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 5 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.
LING 388: Computers and Language
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Introduction to Python
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 10 Sandiway Fong.
CS190/295 Programming in Python for Life Sciences: Lecture 6
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Introduction to Dictionaries
Perl I/O Learning Objectives:
Lists in Python Outputting lists.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Python Primer 1: Types and Operators
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong.
Advanced Python Idioms
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Advanced Python Idioms
Dynamic Programming II DP over Intervals
Python Review
“Everything Else”.
Arrays & Loops.
Arrays & Loops.
Class code for pythonroom.com cchsp2cs
Sample lecture slides.
Dictionary.
LING/C SC/PSYC 438/538 Lecture 5 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.
Selamat Datang di “Programming Essentials in Python”
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Presentation transcript:

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

Administrivia Homework 5 graded Terminal log from last time Homework 5 review Quick Homework 6 (Heads Up: Homework 7 coming on Thursday) Perl Hashes and Python dicts

Terminal log from last time Desktop$ perl -le "@a = qw-a aa aaa A AA AAA ( ) { }-; print \"@a\"" a aa aaa A AA AAA ( ) { } Desktop$ perl -le "@a = qw-a aa aaa A AA AAA ( ) { }-; @b = sort @a; print \"@b\"" ( ) A AA AAA a aa aaa { } Desktop$ perl -le "@a = qw-a aa aaa A AA AAA ( ) { }-; @b = sort {\$a cmp \$b} @a; print \"@b\"" Desktop$ perl -le "@a = qw-0 1 2 01 10 100 ( ) { }-; @b = sort {\$a <=> \$b} @a; print \"@b\"" 0 ( ) { } 1 01 2 10 100

Terminal log from last time Desktop$ perl -le "@a = qw-a b c A B C-; use feature fc; @b = sort {fc(\$a) cmp fc(\$b)} @a; print \"@b\"" a A b B c C Desktop$ perl -le "@a = qw-A B C a b c-; use feature fc; @b = sort {fc(\$a) cmp fc(\$b)} @a; print \"@b\"" A a B b C c Desktop$ perl -le "@a = qw-0 5 3 2 1 99 a A-; @b = sort @a; print \"@b\"" 0 1 2 3 5 99 A a Desktop$ perl -le "@a = (0, 5, 3, 2, 1, 99, 'a', 'A'); @b = sort @a; print \"@b\"" Desktop$

Homework 5 Review Perl Resources: sub https://perldoc.perl.org/perlsub.html substr https://perldoc.perl.org/functions/substr.html length https://perldoc.perl.org/functions/length.html if-then-else https://perldoc.perl.org/perlsyn.html#Compound-Statements

Homework 5 Review palindrome.py Desktop$ python3 pallindrome.py dad True Desktop$ python3 pallindrome.py dada False Desktop$ python3 pallindrome.py racecar Desktop$

Homework 5 Review Let's write the Perl program! length($word) substr($word, -1) substr EXPR,OFFSET,LENGTH substr($word, 0, 1) substr($word, 1, -1) or substr($word, 1, length($word)-2)

Homework 5 Review (Bonus) One student tried '好不好' (good-NOT-good => OK?), but the palindrome code didn't work. Chinese tag questions: 对不对 true (or not)? 有没有 exists (or not)? 是不是 yes (or no)? Perl pre-dates Unicode: and Unicode support is not turned on by default for backwards compatibility. perl -C [number/list] A the @ARGV elements are expected to be strings encoded in UTF-8. O STDOUT will be in UTF-8. I STDIN is assumed to be in UTF-8. S I + O + E.

Quick Homework 6 Due tomorrow night: reviewed on Thursday Question 1: what's the difference between a) and b)? my @a = 4 x 4; my @a = (4) x 4; read https://perldoc.perl.org/functions/split.html Question 2: what does split do here for a) vs. b)? my @a = split " ", 'this is a sentence.'; my @a = split //, 'this is a sentence.'; Due tomorrow night: reviewed on Thursday

Perl Hashes dict in Python Note: Perl array: @ […] Perl hash: % {…}

no quotes needed for strings Perl Hashes Notes on arrays and hashes arrays are indexed using integers hashes are like arrays with user-defined indexing (aka associative array or hash table or dictionary (Python)) initialization (use list notation (or shortcut): round brackets and commas) @a = ("zero", "one", "two", "three", "four"); %h = ("zero", 0, "one", 1, "two", 2, "three", 3, "four", 4); (key/value pairs) access to individual elements (square brackets vs. curly braces) $a[1] “one” $h{zero} 0 Shortcut: no quotes needed for strings 'zero' "zero"

Perl Hashes Notes on arrays and hashes output print @a zeroonetwothreefour print "@a" zero one two three four print %h three3one1zero0two2four4 (note: different order) print "%h" %h (literal, no interpolation done)

More on Hash tables Looping over arrays: Output:

More on Hash tables Exists: Unique key constraint: exists $fruitColor{apple} exists $fruitColor{orange}

Dictionaries (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

Dictionaries (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-particular quirk …

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

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

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