An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.

Slides:



Advertisements
Similar presentations
Franklin Township Elementary School Career Day: Computer Science
Advertisements

Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Selection (decision) control structure Learning objective
Python Programming: An Introduction to Computer Science
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
ITEC113 Algorithms and Programming Techniques
Introduction to Computer Programming I CSE 113
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Program Design and Development
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 2 Writing Simple Programs
Chapter 2: Algorithm Discovery and Design
Chapter 3 Planning Your Solution
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Fundamentals of Python: From First Programs Through Data Structures
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Fundamentals of Python: First Programs
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009.
CIS Computer Programming Logic
Introduction to Python
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana NWLAPCUG - May 15th 2008.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Algorithms and Pseudocode
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
GCSE Computing: Programming GCSE Programming Remembering Python.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Conditionals.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
Chapter 2 Writing Simple Programs
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
UMBC CMSC 104 – Section 01, Fall 2016
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to pseudocode
Intro to Computer Science CS1510 Dr. Sarah Diesburg
A look at Python Programming Language 2018.
Flowcharts and Pseudo Code
CMPT 120 Lecture 4 – Unit 1 – Chatbots
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary College of Louisiana

Assignments Read the first chapter of the textbook Should be done! Assignment 1: Using IDLE Turn in assignment sheet today or Monday Only required to do steps 1 – 5 See revised steps 6 – 7 on the course web page Follow links from Calendar of Lectures, Workshops, & Labs Doing these steps is optional We will learn more about modules later in the course Workshops start: September 13/14/15 Required labs start: September 15/17 Why Computer Science 2

I’m Thinking of a Number… Can we guess the computer’s secret number between 0 and 100? Can the computer guess our secret number between 0 and 100? We need a common language to communicate.

Guess A Number Pseudocode Pick a random number for the computer Ask the user to guess the computer’s number While they guess wrong, Give feedback if number is too high or low Ask for another guess from the user Tell the user they are correct

Language of Programming English is a natural language Casual, slang, vague “I went to the bank.” (money or river?) Computers need formal languages Strict, specific, clear, unambiguous Our choice: Python In principle, all programming languages can do the same things 1 line of Python = 5 lines of Java = 20 lines of C = 100 lines of machine code Pseudocode Simple, (mostly) unambiguous English A way of getting from an English description to a real programming language

Elements of Programming Five basic pieces to all programs Data User Interaction Decisions Repetition Libraries

Data Storage and Memory Numbers / 4 * 5.6 Strings "The quick brown fox" "$5.48" Variables age = 31 cat = "Felix" length = 5 width = 10 area = length * width

User Interaction We need to ask the user questions age = input("How old are you? ") name = raw_input("What is your name? ") We want to tell the user the results print " In dog years, you are " + str(age * 7)

Decisions Relate variables with logic (True, False) temperature > 90 legs == 2 and not human Logic decides program path if temperature > 90: print " Must be summer again..." else: print " Looks like good weather." ?

Repetition Repeats commands while a condition is true count = 10 while count > 0: print count count = count - 1 print "Blastoff!" ?

Including Libraries Import functions from other places import random import math Use these functions to help our program if (random.random() < 0.5): print "Heads" else: print "Tails"

Guess A Number Translation Pick a random number for the computer Ask the user to guess the computer’s number While they guess wrong, Give feedback if number is too high or low Ask for another guess from the user Tell the user they are correct

Guess A Number Translation import random num = random.randrange(100) Ask the user to guess the computer’s number While they guess wrong, Give feedback if number is too high or low Ask for another guess from the user Tell the user they are correct

Guess A Number Translation import random num = random.randrange(100) guess = input("Try to guess my number 0-99: ”) While they guess wrong, Give feedback if number is too high or low Ask for another guess from the user Tell the user they are correct

Guess A Number Translation import random num = random.randrange(100) guess = input("Try to guess my number 0-99: ”) while num != guess: Give feedback if number is too high or low Ask for another guess from the user Tell the user they are correct

Guess A Number Translation import random num = random.randrange(100) guess = input("Try to guess my number 0-99: ”) while num != guess: if guess < num: print "Too Low!" else: print "Too High!" Ask for another guess from the user Tell the user they are correct

Guess A Number Translation import random num = random.randrange(100) guess = input("Try to guess my number 0-99: ”) while num != guess: if guess < num: print "Too Low!" else: print "Too High!" guess = input("Guess again: ”) Tell the user they are correct

Guess A Number Translation import random num = random.randrange(100) guess = input("Try to guess my number 0-99: ”) while num != guess: if guess < num: print "Too Low!" else: print "Too High!" guess = input("Guess again: ”) print "Correct!"

Part II - Computer Guesses How can the computer guess our number? The same way we guessed Start out with a range of numbers Each guess, split the answers in half Eventually, the range will be one number We’re now moving past programming to computer science...

Guess A Number II Print instructions to the user Initialize high and low boundaries and status of guess While guess is incorrect, Formulate a new guess halfway between boundaries Ask for user feedback on guess If guess too high, reset upper boundary If guess too low, reset lower boundary If correct, update status of guess Otherwise ask for valid input from the user When guess is correct, tell the user and exit

Elements of Python Literals Numbers Strings “Henry Kautz” Variables Holders for numbers, strings, lists, and other kinds of information Must begin with a letter, followed by letters, numbers, or underscore _ characters GradePointAverage grade_point_average gradePointAverage Why Computer Science 21

Expressions Made out of Literals Variables Operators (MidtermGrade + FinalGrade) / 2.0 FirstName + “ “ + LastName Functions (we’ll see one soon…) Assignment Statements = AverageGrade = (MidtermGrade + FinalGrade) / 2.0 FullName = FirstName + “ “ + LastName Why Computer Science 22 We use to indicate any thing that is of that type

How Assignments Work Evaluate the right-hand (expression) side Put the value in the left hand (variable) side Count = 1 Count = Count + 1 what is the value of Count? Assigning Input = input( ) guess = input(“What is your guess?”) Why Computer Science 23 the first function we have seen today

Conditional Execution if guess < num: print "Too Low!” else: print "Too High!” General form: if : else: Why Computer Science 24

Loops while num != guess: if guess < num: print "Too Low!” else: print "Too High!” General form: while : Why Computer Science 25

For Next Class Read Chapter 2 Pre-read Chapter 3 Finish Assignment 1 parts 1-5 if not already done Try new version of parts 6-7 if you like Remember: Lab sessions become mandatory starting next week Workshops start Sunday (or Mon/Tue) Why Computer Science 26