16-May-15 Sudden Python Drinking from the Fire Hose.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Python Programming Language
Introduction to Computing Science and Programming I
Introduction to C Programming
Introduction to C Programming
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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 C Programming
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.
Recitation 1 Programming for Engineers in Python.
Fundamentals of Python: From First Programs Through Data Structures
Python Programming Fundamentals
Fundamentals of Python: First Programs
An Introduction to Textual Programming
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
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.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Variables and Expressions CMSC 201 Chang (rev )
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Python Conditionals chapter 5
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
Python Let’s get started!.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
GCSE Computing: Programming GCSE Programming Remembering Python.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Whatcha doin'? Aims: To start using Python. To understand loops.
Python Let’s get started!.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
2.5 Another Java Application: Adding Integers
EGR 2261 Unit 4 Control Structures I: Selection
IF statements.
The Selection Structure
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Line Continuation, Output Formatting, and Decision Structures
And now for something completely different . . .
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Drinking from the Fire Hose
CISC101 Reminders All assignments are now posted.
Beginning Python Programming
Unit 3: Variables in Java
Just Enough Java 17-May-19.
Using C++ Arithmetic Operators and Control Structures
Class code for pythonroom.com cchsp2cs
Presentation transcript:

16-May-15 Sudden Python Drinking from the Fire Hose

Get Python (and IDLE) We will be using Python 2 (version 2.7.3), not Python 3 Get it from The download includes an Integrated Development Environment (IDE), named “IDLE” 2

Running IDLE IDLE opens a window in which you can enter and run Python commands This window is called a REPL (Read-Eval- Print-Loop) Choose File -> New Window to open a window in which you can type entire programs To execute the program, hit the F5 key 3

Commands and data A program consists of commands (more commonly called “statements”) that manipulate data Here are the four most common kinds of data: Integers (whole numbers), such as 5, 17, or -300 “Floating point” numbers, such as “Strings” are character sequences enclosed in either single quotes or double quotes, such as "Madam, I'm Adam" and '"Too soon," she said.' Straight quotes only ( " and ' ), not curly quotes ( “” ‘’ ) “Boolean” (logical) values—there are only two of these, True and False 4

Program components Programs can read in data Programs can write results In between reading and writing, programs can compute, that is, do arithmetic (or logic) test, that is, decide what to do next loop, that is, do the same actions a number of times delegate, that is, ask other parts of the program to perform some task Also, programs can ignore comments 5

Doing simple arithmetic Here are the arithmetic operators: + performs addition - performs subtraction * performs multiplication / performs division When dividing two integers, the result is an integer: 14 / 5 is 2 % performs modulus (remainder of division): 14 % 5 is 4 ** performs exponentiation The result of doing arithmetic is often assigned to a variable: sum = Variables can be used in arithmetic: average = sum / 5 6

Reading in data Here’s how to ask the user to enter a string: name = raw_input("What is your name? ") Whatever the user types in, up to a press of the Enter key, is a string that is assigned to the variable name Here’s how to ask the user to enter a number: age = input("What is your age? ") Whatever the user types in, up to a press of the Enter key, is converted to a number and assigned to the variable age input and raw_input are functions (or methods) For now, we will treat “function” and “method” as synonyms 7

Printing results To print results, use the print statement print "Two plus two is four" You can print multiple things separated by commas print 2, "plus", 2, "is", Each print statement writes a “newline” at the end (so that the next print statement goes to a new line) You can omit the newline by ending with a comma: print 2, "plus", 2, print "is",

Comments A comment is a note to any human looking at the program; comments are ignored by the computer. A comment begins with # and extends to the end of the line Good uses of comments: At the beginning of a program, to tell what the program does When using someone else’s code, to say where you got it from To explain any code that’s hard to understand Bad uses of comments: To explain something that’s obvious anyway To explain code that’s hard to understand, but could be made simpler To add irrelevant comments, like # Go Eagles! When you should instead use a doc string (described on a later slide) 9

Layout Every statement goes on a line by itself Put spaces around operators, including the assignment operator ( = ) average = sum / 5 Put spaces after commas (but not before commas) print 2, "plus", 2, "is", When using a function, do not put spaces on either side of the parentheses age = input("What is your age? ") 10

Decisions and tests Your program can decide what to do by making a test The result of a test is a boolean value, True or False Here are tests on numbers: < means “is less than” <= means “is less than or equal to” == means “is equal to” != means “is not equal to” >= means “is greater than or equal to” < means “is greater than” These same tests work on strings All capital letters are “less than” all lowercase letters 11

Compound tests Boolean values can be combined with these operators: and – gives True if both sides are True or – gives True if at least one side is True not – given True, this returns False, and vice versa Examples score > 0 and score <= 100 name == "Joe" and not score >

The if statement The if statement evaluates a test, and if it is True, performs the following indented statements; but if the test is False, it does nothing Examples: if grade == "A+": print "Congratulations!" if score 100: print "That’s not possible!" score = input("Enter a correct value: ") 13

if with else The if statement can have an optional else part, to be performed if the test result is False Example: if grade == "A+": print "Congratulations!" else: print "You could do so much better." print "Your mother will be disappointed." 14

if with elif The if statement can have any number of elif tests Only one group of statements is executed—those controlled by the first test that passes Example: if grade == "A": print "Congratulations!" elif grade == "B": print "That's pretty good." elif grade == "C": print "Well, it's passing, anyway." else: print "You really blew it this time!" 15

Indentation Indentation is required and must be consistent Standard indentation is 4 spaces or one tab IDLE does this pretty much automatically for you Example: if != 4: print "Oh, no!" print "Arithmethic doesn't work!" print "Time to buy a new computer." 16

Lists and ranges A list is a sequence of values enclosed in brackets Example: courses = ['CIT 591', 'CIT 592', 'CIT 593'] You can refer to an individual value by putting a bracketed number (starting from 0) after the list Example: courses[2] is 'CIT 593' The len function tells you how many things are in a list Example: len(courses) is 3 range is a function that creates a list of integers, from the first number up to but not including the second number Example: range(0, 5) creates the list [0, 1, 2, 3, 4] If you give range a third number, it is used as the step size Example: range(2, 10, 3) creates the list [2, 5, 8] 17

The for loop A for loop performs the same statements for each value in a list Example: for n in range(1, 4): print "This is the number", n prints This is the number 1 This is the number 2 This is the number 3 The for loop uses a variable (in this case, n ) to hold the current value in the list 18

The while loop A while loop performs the same statements over and over until some test becomes False Example: n = 3 while n > 0: print n, "is a nice number." n = n – 1 prints 3 is a nice number. 2 is a nice number. 1 is a nice number. If the test is initially False, the while loop doesn't do anything. If the test never becomes False, you have an "infinite loop." This is usually bad. 19

Calling a function A function is a section of code that either (1) does some input or output, or (2) computes some value. A function can do both, but it's bad style. Good style is functions that are short and do only one thing Most functions take one or more arguments, to help tell them what to do Here's a function that does some input: age = input("How old are you? ") The argument, "How old are you?", is shown to the user Here's a function that computes a value (a list): odds = range(1, 100, 2) The arguments are used to tell what to put into the list 20

Defining a function 1. def sum(numbers): 2. """Finds the sum of the numbers in a list.""" 3. total = 0 4. for number in numbers: 5. total = total + number 6. return total 1. def defines a function numbers is a parameter: a variable used to hold an argument 2. This doc string tells what the function does 6. A function that computes a value must return it sum(range(1, 101)) will return

Summary Arithmetic: + - * / % = > Logic (boolean): True False and or not Strings: "Double quoted" or 'Single quoted' Lists: [1, 2, 3, 4] len(lst) range(0, 100, 5) Input: input(question) raw_input(question) Decide: if test: elif test: else: For loop: for variable in list: While loop: while test: Calling a function: sum(numbers) Defining a function: def sum(numbers): return result 22

Advice to beginners Programming is hard! The individual building blocks are all pretty simple, but they go together in complex patterns You will make many mistakes, and they will (almost) all be stupid mistakes That doesn’t mean you are stupid! Experts make just as many stupid mistakes, but they are more experienced at finding and correcting them Therefore: Don’t be shy about letting other people see your mistakes In a few weeks you will find that it suddenly all starts to make sense, and you'll wonder what the problem was Don’t panic! There’s lots of help available 23

Advice to non-beginners A lot is known about how to program well You probably have a lot of bad habits to unlearn The following are important habits to learn: Thorough testing is essential; anything less is amateurish Concentrate on clarity (not efficiency) at all times If code is hard to understand, simplify it Use comments to explain code you aren’t able to simplify Remember that the best way to learn something is to teach it 24

The End “Programming is an art form that fights back.” -- Anonymous 25