CS 3813/718 Fall Python Programming. Professor Liang Huang.

Similar documents
Programming Languages Fall Prof. Liang Huang

Object-Oriented Programming for Managers

CS 241 Data Organization. August 21, 2018

Announcements. 1. Forms to return today after class:

Senthil Kumaran S

CS 6371: Advanced Programming Languages

CS 241 Data Organization using C

Hello, World. Java. #include <stdio.h> int main(int argc, char ** argv) {! printf( Hello, World!\n ); }

CS240: Programming in C

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS 253: Intro to Systems Programming 1/21

ECE573 Introduction to Compilers & Translators

CIS192: Python Programming

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 113: Introduction to

CS 3030 Scripting Languages Syllabus

6.S096 Lecture 1 Introduction to C

CS 3030 Scripting Languages Syllabus

CS 4349 Lecture August 21st, 2017

Python for Earth Scientists

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

CS61C : Machine Structures

Introduction to Data Structures

Welcome to... CS113: Introduction to C

Beijing Jiaotong University CS-23: C++ Programming Summer, 2019 Course Syllabus

CS457/557 Functional Languages

CS1 Lecture 3 Jan. 18, 2019

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

COMP 117: Internet-scale Distributed Systems Lessons from the World Wide Web

CSE 240 Introduction to Computer Architecture

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CSC 1052 Algorithms & Data Structures II: Introduction

CS 240 Fall Mike Lam, Professor. Just-for-fun survey:

CSE 504: Compiler Design

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

TTh 9.25 AM AM Strain 322

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

The current topic: Python. Announcements. Python. Python

last time in cs recitations. computer commands. today s topics.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

Hello, World. Java. #include <stdio.h> int main(int argc, char ** argv) {! printf("hello, World!\n"); }

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

CS 150 Introduction to Computer Science 1

CS111: Intro to Computer Science

CS1 Lecture 3 Jan. 22, 2018

Advanced Programming Concepts. CIS 15 : Spring 2007

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

CSC 015: FUNDAMENTALS OF COMPUTER SCIENCE I

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

Table of Contents EVALUATION COPY

EL2310 Scientific Programming

CSCI455: Introduction to Programming System Design

COMP322 - Introduction to C++ Lecture 01 - Introduction

Creating and Running Your First C# Program

EEE-425 Programming Languages (2013) 1

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Introduction to Programming in Python (1)

San José State University Computer Science Department CS49J, Section 3, Programming in Java, Fall 2015

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

CS/ENGRD 2110 FALL 2018

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

PLAGIARISM. Administrivia. Compilers. CS143 11:00-12:15TT B03 Gates. Text. Staff. Instructor. TAs. Office hours, contact info on 143 web site

CSE 114, Computer Science 1 Course Information. Spring 2017 Stony Brook University Instructor: Dr. Paul Fodor

CSC116: Introduction to Computing - Java

CS61C : Machine Structures

/ Cloud Computing. Recitation 2 January 19 & 21, 2016

Compilers for Modern Architectures Course Syllabus, Spring 2015

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CSE 240 Introduction to Computer Architecture

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

COMP250: Introduction to Computer Science. Jérôme Waldispühl & Carlos Oliver Gonzalez School of Computer Science McGill University

Programming Languages Part 1. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Algorithms and Programming I. Lecture#12 Spring 2015

Lecture 8. Conditionals & Control Flow

CS Computer Science I

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Welcome to CompSci 201

Outline. Intro. Week 1, Fri Jan 4. What is CG used for? What is Computer Graphics? University of British Columbia CPSC 314 Computer Graphics Jan 2013

Intro. Week 1, Fri Jan 4

Unit 7: Algorithms and Python CS 101, Fall 2018

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

EL2310 Scientific Programming

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

Programming Languages: Part 1. Robert M. Dondero, Ph.D. Princeton University

CSc 372 Comparative Programming Languages

Programming for Engineers in Python

CSE506: Operating Systems CSE 506: Operating Systems

COSE212: Programming Languages. Lecture 0 Course Overview

CSE 351. Introduction & Course Tools

Transcription:

CS 3813/718 Fall 2012 Python Programming Professor Liang Huang huang@cs.qc.cuny.edu http://vision.cs.qc.cuny.edu/huang/python-2012f/

Logistics Lectures: TTh 9:25-10:40 am SB B-141 Personnel Instructor Prof. Liang Huang huang@cs TA Kai Zhao z.kaayy@gmail Admin Xiuyi Huang xiuyi@cs Office Hours (subject to change!) LH -- SB A-328 KZ -- SB A-207 T 4-5 pm, Th 10:50-11:30am T 10:50-11:30 am, Th 4-5pm additional office hours available before quizzes/exams 2

Logistics - cont d Homepage vision.cs.qc.cuny.edu/huang/python-2012f/ schedule, syllabus, homework, handouts, etc. Newsgroup cs3813780-qc@googlegroups.com announcements, Q/A --> post here first! Course Email cs3813780.qc@gmail.com to reach both the instructor and the TA don t email us individually Blackboard -- the [2nd ] worst software I ever used! grades and electronic submissions 3

Grades Programming Homework: 5% + 7% + 8% = 20% electronic submission; work individually: only high-level discussions are OK, and you have to declare who you discussed with grading is mostly black-box: you should follow strict I/O formats! please work on Linux or Mac; we don t support Windows users late penalty: you can submit only one HW late for 32 hours. Quizzes: (7+3)% x 3 = 30% and Midterm: (15+5)% = 20% Final Programming Project: 22% (project designed by me) Class Participation: 8% asking/answering questions in class and newsgroup catching/fixing bugs in slides/exams/hw & other suggestions 4

Academic Integrity an automatic F if you re caught on any of these: cheating during quizzes/exams catching cheating is easier than you thought! :-) copying another person s code for HW/Project copying code from online resources for HW/Project discussions with others for take-home quiz/exams any other cheating behavior defined by University I will report every single case to the University 5

Textbooks (for reference) but we will not follow any textbook. Textbooks How to Think Like a Computer Scientist: Learning Python by Allen B. Downey, Jeffrey Elkner and Chris Meyers strongly recommended! NLTK textbook by Steve Bird and Ed Loper (optional) Tutorials (Official) Python Tutorial by Guido van Rossum (inventor of Python) A Quick, Painless Tutorial on the Python Language by Norm Matloff 6

Course Outline 1. Python Basics Syntax, Control Flow -- quiz 1 Basic Data Structures (list, dict, tuple,...) -- hw 1 2. Object-Oriented and Functional Programming OOP (Objects, Inheritance,...) -- quiz 2 FP (map, filter, reduction, iters, lambdas) -- hw 2 3. Python for Algorithmic Problem Solving -- midterm 4. Combining Python and C/C++/Java -- hw3 5. Basic Text Processing using Python -- quiz 3 7

Why Python? Because it s easy and great fun! less than 10 years old, yet very popular now a wide-range of applications, esp. in AI and Web extremely easy to learn many schools have shifted their intro-courses to Python fast to write much shorter code compared to C, C++, and Java easy to read and maintain more English-like syntax and a smaller semantic-gap 8

On to Python...

C Java #include <stdio.h> Hello, World int main(int argc, char ** argv) {! printf( Hello, World!\n ); } public class Hello {! public static void main(string argv[])! {!! System.out.println( Hello, World! );! } } now in Python print Hello, World! 10

Printing an Array void print_array(char* a[], int len) {! int i; has to specify len, and only for one type (char*)! for (i = 0; i < len; i++)! {!! printf( %s\n, a[i]);! } } C for element in list:! print element only indentations no {... } blocks! or even simpler: print list for... in... :... no C-style for-loops! for (i = 0; i < 10; i++) Python 11

Reversing an Array static int[] reverse_array(int a[]) {! int [] temp = new int[ a.length ];! for (int i = 0; i < len; i++)! {!! temp [i] = a [a.length - i - 1];! }! return temp; } def rev(a): def...(...):! if a == []:!! return []...! else:!! return rev(a[1:]) + [a[0]] or even simpler: a.reverse() a without a[0] built-in list-processing function singleton list Java Python no need to specify argument and return types! python will figure it out. (dynamically typed) 12

Quick-sort public void sort(int low, int high) {! if (low >= high) return;! int p = partition(low, high);! sort(low, p);! sort(p + 1, high); } void swap(int i, int j) {!! int temp = a[i];! a[i] = a[j];! a[j] = temp; } int partition(int low, int high) {! int pivot = a[low];! int i = low - 1;! int j = high + 1;! while (i < j)! {!! i++; while (a[i] < pivot) i++;!! j--; while (a[j] > pivot) j--;!! if (i < j) swap(i, j);! }! return j; } def sort(a):! if a == []:!! return []! else:!! pivot = a[0]!!!! left = [x for x in a if x < pivot ]!! right = [x for x in a[1:] if x >= pivot]!! return sort(left) + [pivot] + sort(right) {x x a, x < pivot} how about return [sort(left)] + [pivot] + [sort(right)] Java Python smaller semantic-gap! got error?? 13

Python is... a scripting language (strong in text-processing) almost like pseudo-code! but also object-oriented (like C++ and Java) interpreted, like Perl, but much more elegant a very high-level language (closer to human semantics) procedural (like C, Pascal, Basic, and many more) and even functional! (like ML/OCaml, LISP/Scheme, Haskell, etc.) from today, you should use Python for everything not just for scripting, but for serious coding! 14

Let s take a closer look...

Python Interpreter Three ways to run a Python program 1. Interactive like DrJava >>> for i in range(5):... print i,... 0 1 2 3 4 2. (default) save to a file, say, foo.py in command-line: python foo.py 3. add a special line pointing to the default interpreter add #!/usr/bin/env python to the top of foo.py make foo.py executable (chmod +x foo.py) in the command-line:./foo.py 16

The right version of Python we will use the latest version 2.7 (e.g. 2.7.3) Python 3.x is a very different experimental branch... your default machine is cs12.cs.qc.cuny.edu, where your default python is already 2.7 or you can install 2.7 on your own mac/windows TA will help you with installations and versions bash-2.0$ python Python 2.7.1 (#1, Jan 22 2010, 18:59:00) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> [<lhuang@mac OS X:~>] which python /Library/Frameworks/Python.framework/Versions/2.7/bin/python 17