An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana NWLAPCUG - May 15th 2008.

Slides:



Advertisements
Similar presentations
Summer 2012 Instructor: Hassan Khosravi
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Conditional Statements Introduction to Computing Science and Programming I.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 1 Program Design
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
ALGORITHMS AND FLOWCHARTS
Computer Programming-1 CSC 111 Chapter 1 : Introduction.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
An Introduction to Textual Programming
High-Level Programming Languages: C++
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Intro to Programming and JavaScript. What is Programming? Programming is the activity of creating a set of detailed instructions (Program) that when carried.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
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.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
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.
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.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
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.
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 1 st semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
Please log on The. AN INTRODUCTION TO ‘Python is a high-level, general purpose programming language’ Python is one of the many programming languages.
Course A201: Introduction to Programming 09/16/2010.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Sequencing The most simple type of program uses sequencing, a set of instructions carried out one after another. Start End Display “Computer” Display “Science”
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
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.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
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.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Algorithms and Pseudocode
GCSE Computing: Programming GCSE Programming Remembering Python.
Selection Part 2 Using Logical Operators, how strings are compared and random numbers.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Conditionals.
Selection Using IF THEN ELSE CASE Introducing Loops.
Chapter 1 Introduction 2nd Semester H
GCSE COMPUTER SCIENCE Practical Programming using Python
Software Development.
Writing algorithms Introduction to Python.
Lesson 4 - Challenges.
Lesson 1 An Introduction
Python: Control Structures
Programming 101 Programming for non-programmers.
Lesson 3 - Repetition.
Logical Operators and While Loops
ALGORITHMS AND FLOWCHARTS
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to pseudocode
ALGORITHMS AND FLOWCHARTS
Loops CIS 40 – Introduction to Programming in Python
Introduction to TouchDevelop
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
Faculty of Computer Science & Information System
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm and Ambiguity
Introduction In today’s lesson we will look at: why Python?
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
Logical Operators and While Loops
ICT Gaming Lesson 2.
Data Types and Maths Programming Guides.
Presentation transcript:

An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana NWLAPCUG - May 15th 2008

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 Many language choices: C++, Java, Prolog, Scheme, Perl, Fortran, Cobol, Ruby, etc…

Python Programming

How to Talk to your Computer ApplicationCPU User Programmer

Python Interpreter

Python and IDLE

Programming Components 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 tall 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!"

Running our Code Go to IDLE Open guess1.py Press F5 to run the program

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

Guess A Number II print "Pick a number between 0 and 99, I will guess it." print "If I am high, type H, low, type L, and correct type C." low, high, correct = 0, 100, False while not correct: guess = (high + low) / 2 answer = raw_input("I guess " + str(guess) + ", HLC? ") if answer == "H": high = guess elif answer == "L": low = guess elif answer == "C": correct = True else: print "I don't understand, please enter H, L or C." print "I found it!"

Other Examples Chaos and Fractals Can’t Stop the Monkeys

Further References Python Home Page How to Think Like a (Python) Programmer Graphics Package for Python