Programming Seminar 2009 Night 1. Review: Last Time We covered variables, for loops, functions, modules, and how programming isn’t math – Actually, we.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Python Programming: An Introduction to Computer Science
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Program Design and Development
Python November 14, Unit 7. Python Hello world, in class.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to Python
Chapter 1 Program Design
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
An Introduction to Textual Programming
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
Introduction to Python
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
The Binomial Theorem. Powers of Binomials Pascal’s Triangle The Binomial Theorem Factorial Identities … and why The Binomial Theorem is a marvelous.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Python – May 11 Briefing Course overview Introduction to the language Lab.
Built-in Data Structures in Python An Introduction.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
For loops in programming Assumes you have seen assignment statements and print statements.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
Python Programing: An Introduction to Computer Science
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Containers and Lists CIS 40 – Introduction to Programming in Python
Section 6: Sequences Chapter 4 and 5.
Introduction to Programmng in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture 07 More Repetition Richard Gesick.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Lecture 4B More Repetition Richard Gesick
Creation, Traversal, Insertion and Removal
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Loops CIS 40 – Introduction to Programming in Python
Remembering lists of values lists
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Python Lists and Sequences
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Presentation transcript:

Programming Seminar 2009 Night 1

Review: Last Time We covered variables, for loops, functions, modules, and how programming isn’t math – Actually, we just scratched the surface for most of these This time, we’ll be focusing more on functions – And functionality – With any luck, we will work in Python 3.

For loops and if statements, revisited >>> str = input() >>> words = 1 >>> for c in str: >>>if c == ‘ ’: >>>words = words + 1 What does this do?

Defining a function >>> def cube(n): >>>return n*n*n Remember, return tells Python what value the function should “evaluate” to. Similar to math functions defined in piecewise fashion: abs(x) = {x if x >= 0; -x if x < 0}

Functions take action! Calling a function says: “please do this” – E.g. input() asks for user input, waits for it to be entered, then returns it as the “value” of the function. Most functions are created to make doing a repetitive task easy.

Okay, so what should these “functions” do? Let’s take our example of a calculator program – Inputs two numbers and an operation, the outputs the result Break up this idea into smaller pieces – Which can be accomplished in, say, a couple statements

You tell me First, Then, we Last,

We can turn this into a high-level, function-based overview in = read_input() op = pick_operation(in) a,b = pick_numbers(in) result = carry_out_operation(a,b) print(result) Then we just have to implement each part – Correctly…

(Your calculator program, written collaboratively) >>>

What Python can already do for you We now (formally) introduce the data type Data Type: A combination of information and functionality given a specific name. Now, for a pile of examples

Standard Python Data Types str – a string list – a list of items tuple – a list of items (with a few more restrictions) – All entries have to be of the same type – Length cannot change set – a collection of unique elements dictionaries – a mapping between one set of data and another

Python Strings >>> s = “This is a string” Ask for the 5 th character >>> print(s[5]) Ask for the 3 rd through 8 th characters >>> print(s[3:8]) Ask for the 2 nd character from the end >>> print(s[-2])

Strings, continued Ask for all characters after (and including) the 4 th >>> print(s[4:]) All of these are called slices Can pull the same neat tricks with lists – Plus a few more

Lists >>> l = [4,8,15,16,23,42] Ask for the last three elements >>> print(l[-3:]) Remove the middle two elements >>> del l[2:-2] >>> print(l) Assign the first element to 108 >>> l[0] = 108

Strings and lists can work together >>> print(“Please enter a list of names (first and last).” >>> l = [] # empty list >>> s = “First Last” >>> while s: # so long as s is not empty >>>s = input(“Name please: ”) >>>l.append(s) >>> print(“You entered ” + len(l) + “ items.”)

One more interaction Remember that program from last time which counted the number of words in a sentence? >>> s = input(“Please enter a sentence.”) >>> l = s.split(“ ”) >>> print(len(l)) s.split(t) gives a list of all substrings of s broken around t “This is a sentence.”.split(“ ”) gives [“This” “is”, “a”, “sentence.”] “This is a sentence.”.split(“s”) gives [“Thi ”, “ i”, “ a ”, “entence.”]

Your turn, again! Write a program that asks for a bunch of s as input. – Output a list of all the domains (whatever is after Given a list of numbers, find the list of numbers that are relatively prime to the first one. Given a word, print its Scrabble score – aeioulnrst are 1 point, dg are 2, bcmp are 3, fhvwy are 4, k is 5, jx are 8, and qz are 10

On Files Repeating input is annoying. – You have to type everything every time We can put input into a file. >>> file = open(“README.txt”) >>> for line in file: >>>print(line) >>> file.close()

Files, continued What’s up with open() and close()? – Python has to ask the operating system for help. Details in CIS 240 (more details in CIS 380) or by demand Useful for processing information – Read in from a file, output to a file

Next Time Project Euler Classes? – Not the kind you’ll be attending in a few days Tuples Fancier variations on simple constructs