1 / 18

LING/C SC/PSYC 438/538

LING/C SC/PSYC 438/538. Lecture 6 Sandiway Fong. Adminstrivia. A note on Windows 10 PowerShell for Perl Coercion (again) Conditionals Looping Sorting in Perl and Python. PowerShell (Windows). PowerShell (Windows). Finish Python install. Four versions present:

alicee
Télécharger la présentation

LING/C SC/PSYC 438/538

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Adminstrivia • A note on Windows 10 PowerShell for Perl • Coercion (again) • Conditionals • Looping • Sorting in Perl and Python

  3. PowerShell (Windows)

  4. PowerShell (Windows)

  5. Finish Python install Four versions present: Python command line (2.7.x) IDLE Python (2.7.x) Python 3.6 IDLE Python 3.6

  6. PowerShell (Windows)

  7. Environment Variables

  8. Implicit Coercion Note: qw = quote word 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";

  9. 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)

  10. Conditionals and Looping • Conditionals • 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?

  11. Conditionals and Looping

  12. ARGV Command line: perlargv.perl 1 2 3

  13. General Looping

  14. Perl Arrays • by default sort works according to the ASCII chart, character by character asciitable.com

  15. Perl Arrays: sorting Numeric sort? • Idea: to pass an inline comparison function as parameter… • Note: fc (fold case) use feature 'fc';

  16. Python: sorting • $ python3 -itest.py • >>> items • ['A', 'a', 'B', 'b', 'C', 'c'] • >>> items.sort() • >>> items • ['A', 'B', 'C', 'a', 'b', 'c'] • >>> items.sort(reverse=True) • >>> items • ['c', 'b', 'a', 'C', 'B', 'A'] • >>> items.sort(reverse=True, key=str.lower) • >>> items • ['c', 'C', 'b', 'B', 'a', 'A'] • >>>  Using method (not function) .sort()

  17. Python: sorting

  18. Python: sorting

More Related