First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!

Slides:



Advertisements
Similar presentations
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Advertisements

Code: from cisc106 import * def myfunc(value1,value2): return (value1 + value2) assertEqual(myfunc(3,2),5) assertEqual(myfunc(7,4),11) assertEqual(myfunc(9,8),17)
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Software Lesson #1 CS1313 Spring Software Lesson 1 Outline 1.Software Lesson 1 Outline 2.What is Software? A Program? Data? 3.What are Instructions?
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to Python
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.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
COMPSCI 101 Principles of Programming
Computer Science 101 Introduction to Programming.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 2 Fundamentals of Programming Languages 4/5/09 Python Mini-Course: Day.
Python Programming Fundamentals
An Introduction to Textual Programming
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
Computer Science 101 Introduction to Programming.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Introduction to Programming with RAPTOR
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PROGRAMMING In. Objectives  We’re learning to develop basic code with the use of the correct syntax and variables. Outcomes  Explain what syntax is.
Variables, Expressions and Statements
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.
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.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
COMPUTER PROGRAMMING Year 9 – Unit 9.04 Week 3. Open the Python INTERPRETER Can you use the interpreter to solve these maths problems? 156 add
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Programming In Python. Starter Using the internet… Find what a programming language is.
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.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Thinking about programming
Math operations 9/19/16.
Fundamentals of Programming I Overview of Programming
Introduction to Computing Science and Programming I
Topics Designing a Program Input, Processing, and Output
A Playful Introduction to Programming by Jason R. Briggs
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Variables, Expressions, and IO
Thinking about programming
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Functions CIS 40 – Introduction to Programming in Python
Topics Designing a Program Input, Processing, and Output
Writing Functions( ) (Part 4)
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Unit 3: Variables in Java
General Computer Science for Engineers CISC 106 Lecture 03
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!

Atomic Data  the lowest level of detail from which aggregate data is computed.  the smallest unit of data you can do something with  if we give Python atomic data, Python spits it back at us  Try :  42   “puddle”

Expressions  Use atomic data to build compound expressions.  Compound expressions consist of two expressions (either atomic or compound), with an operator between them * 2 3 / 2 3 ** 2 (3 + 2) ** 3 (3 * 4) / 2  We’re starting to acquire TOOLS!

Order of operators: 1. (x+y)  parentheses 2. x ** y  Power (right associative) 3. x * y, x / y, x // y, x % y  Multiplication, division, floor division, modulo 4. x + y, x - y  Addition, subtraction Shall we try this?  /  7//3  -7//3  5*2**3  7%2  18%4  3 + 2**3  (3 + 2) ** 3  12/2 ** 2

Files 1. Open a file  In IDLE, go to File->New Window 2. In New Window:  Type: File->Save As  Save the file as first.py 4. Run the file:  Run->Run Module Now you’ve saved your work so you can run it later When saving a python program, it must have a.py extension!!!  So interpreter knows it’s python code.

Functions  We have a file: we can save our work.  Now, let’s create “functions” to name code we might want to use again  Math: function takes a number or numbers and transforms it to another number  E.g., f(x) = 2x f(3) = 6 f(5) = 10 g(x) = x g(2) = 9 g(5) = 126

Creating a function: Function (mathematical) Consists of 3 parts and a name: -> name: g (not a good name! Tells us nothing about what this function does) -> input parameter in the example, integers 2 or 5 -> instructions (code) in the example, x** > output in the example, the integer 9 or 126 g(x) = x g(2) = 9 g(5) = 126

Function (in Python) def g(x): return(x ** 3 + 1) g(x) = x g(2) = 9 g(5) = 126

Creating a function: Function (programming)  Function: a set of instructions (code) identified with a name  Every function in a program has a unique name  The instructions inside the function do not get executed until the function’s name is called  Can be called again and again  Or can never be called  …which is kind of pointless

Function (in Python) def g(x): return(x ** 3 + 1) To Call the Functions (to make them run): g(2) g(5) To see what the function calculates (returns): print (g(2)) print (g(5)) g(x) = x g(2) = 9 g(5) = 126

Input Values: Parameters values into the function are known as parameters 3,2 addfunc 5 7,4 addfunc11 9,8 addfunc 17 Code: def addfunc(value1,value2): return (value1 + value2) print(addfunc(3,2)) print(addfunc(7,4)) print(addfunc(9,8))  We should know what we want to come out of the function, so we can check to make sure the function is working correctly  Print allows us to check the value that is coming out of the function.

Function:  Calculate the area of a rectangle? 1. Name of function? 2. Input? 3. Output? 4. Test cases? 5. Calculations? Can we now write the function? def arearectangle(len,width): return(len*width) func(x,y) = x*y func(2,7) = 14 func(5,4) = 20

Function names:  Naming Rules:  Must start with a letter  not a number!  Can only contain letters and numbers  NO SPACES!!!!  NO SPECIAL CHARACTERS!  Anything that isn’t a letter or a number  * & ^ % $ $ ! `~ + = - ) ( “ ‘ : ; ><?/, etc.  Other than _ (the underscore)   4Cats  Cat_n_Mouse  Cat-tail  Cat123  cats4sale  Cat n Mouse

Try: 3 newfunc 27 5 newfunc newfunc 4 5,3 newfunc2 2 18,6 newfunc2 0 7,2 newfunc2 1 3,4,2 newfunc3 5 7,6,2 newfunc3 10 4,21,6newfunc3 7

Code: def newfunc(par1): return(par1**par1) def newfunc2(par1,par2): return (par1 % par2) def newfunc3(par1, par2, par3): return(par1+(par2//par3)) print(newfunc(3)) print(newfunc(5)) print(newfunc(2)) print(newfunc2(5,3)) print(newfunc2(18,6)) print(newfunc2(7,2)) print(newfunc3(3,4,2)) print(newfunc3(7,6,2)) print(newfunc3(4,21,6))

Would it have been easier if I’d said: “This function takes an integer as an input value and returns an integer. It calculates the square of the input and returns that value” 3 newfunc 27 5newfunc newfunc 4 “This function takes as input 2 integers and returns an integer. It divides the first input integer by the second input integer and returns the remainder. In other words, it calculates the following: num1 % num2.” 5,3 newfunc2 2 18,6 newfunc2 0 7,2newfunc2 1 “This function takes 3 integers as input and returns an integer. It floor- divides the second input integer by the third input integer. It takes the result of that and adds it by the first input integer. In other words, it calculates the following: num1+(num2//num3)” 3,4,2 newfunc3 5 7,6,2 newfunc3 10 4,21,6 newfunc3 7

Comments #This function calculates the square of the input value #and returns that squared value #input: an integer #output: an integer #Test Cases: # print(newfunc(3)) -> 27 # print(newfunc(5)) -> 3125 # print(newfunc(2))-> 4 #Author: Debra Yarrington #Sept 6, 2011 def newfunc(par1): return(par1**par1) # returns the square  Comments aren’t executed (aren’t converted to machine language). Python’s compiler ignores them. They’re for people who are reading your code.  They also can be used to help you (and others) understand what you are doing and why

What we’ve learned about writing functions:  We should come up with test cases first  How many parameters go into the function in order to get the output?  We should include comments that clearly describe how the function works.  These comments should include our test cases  After we’ve got the test cases and the function description, then we write the function.  Basically, you have to think it through before you write the function.