Introduction to Programming with Python

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Introduction to Programming with Python Marty Stepp University of Washington Special thanks to Scott Shawcroft, Ryan Tucker,
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
INTRODUCTION TO PYTHON PART 1 CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Computer Science 101 Introduction to Programming.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Variables, Expressions, and Statements
Variables, Expressions and Statements
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Computing and Statistical Data Analysis Lecture 2
Lecture 4: Expressions and Variables
Building Java Programs
Presented By S.Yamuna AP/IT
Primitive Data, Variables, Loops (Maybe)
Variables, Expressions, and IO
basic Python programs, defining functions
PHP Introduction.
Building Java Programs Chapter 2
Introduction to C++ Programming
basic Python programs, defining functions
Building Java Programs
Building Java Programs
Introduction to programming with Python
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs Chapter 2
Compilers and Interpreters
Core Objects, Variables, Input, and Output
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Programming with Python
Building Java Programs
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Programming with Python

Languages Some influential ones: FORTRAN COBOL LISP BASIC science / engineering COBOL business data LISP logic and AI BASIC a simple language

Python Created in 1991 by Guido van Rossum (now at Google) Named for Monty Python Useful as a scripting language script: A program meant for use in small/medium projects Used by: Google, Yahoo!, Youtube Many Linux distributions Games and apps (e.g. Eve Online)

Installing Python Windows: Download Python from http://www.python.org Install Python. Run Idle from the Start Menu. Mac OS X: Python is already installed. Open a terminal and run python or run Idle from Finder. Linux: Chances are you already have Python installed. To check, run python from the terminal. If not, install from your distribution's package system. Note: For step by step installation instructions, see the course web site.

Interpreted Languages Not compiled like many other languages (Java, C, C++) Code is written and then directly executed by an interpreter Type commands into interpreter and see immediate results Computer Runtime Environment Compiler Code Java: Interpreter Python:

The Python Interpreter Allows you to type commands one-at-a-time and see results A great way to explore Python's syntax

Basic Programs and Numeric Data

The print Statement A Python program's code is just written directly into a file print "text" print (a blank line) hello.py 1 print "Hello, world!" swallows.py 1 2 3 4 print "Hello, world!" print print "Suppose two swallows carry it together." print "African or European swallow?"

Comments Syntax: # comment text (one line) swallows2.py 1 2 3 4 5 6 # Suzy Student, CSE 142, Fall 2097 # This program prints important messages. print "Hello, world!" print # blank line print "Suppose two swallows \"carry\" it together." print 'African or "European" swallows?'

Expressions expression: A value or operation(s) to compute a value. Example: 1 + 4 * 3 Arithmetic operators: + - * / add, subtract/negate, multiply, divide ** exponentiate % modulus, a.k.a. remainder precedence: Order in which operations are computed. * / % ** have a higher precedence than + - 1 + 3 * 4 is 13 (1 + 3) * 4 is 16

Integer division When we divide integers with / , the quotient is an integer. 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 35 / 5 is 7 84 / 10 is 8 The % operator computes a remainder from integer division. 3 43 4 ) 14 5 ) 218 12 20 2 18 15 3 Dividing by 0 crashes the program.

Variables variable: A named piece of memory that stores a value. assignment: Stores a value into a variable. Syntax: name = expression Examples: x = 5 gpa = 3.14 x 5 gpa 3.14 A variable can be used in expressions. x + 4 is 9

Variables A variable is a named place in the memory where a programmer can store data and later retrieve the data using the variable “name” Programmers get to choose the names of the variables You can change the contents of a variable in a later statement 12.2 x = 12.2 y = 14 x 100 x = 100 14 y

Python Variable Name Rules Can consist of letters, numbers, or underscores (but cannot start with a number) Case Sensitive Good: spam eggs spam23 _speed Bad: 23spam #sign var.12 Different: spam Spam SPAM

Data Types type: A category or set of data values. Constrains the operations that can be performed on the data Examples: integer, real number, text string Python is relaxed about types. A variable's type does not need to be declared. A variable can change types as a program is running. Value Python type 42 int 3.14 float "ni!" str

Some Data Types in Python Integer (Examples: 0, 12, 5, -5) Float (Examples: 4.5, 3.99, 0.1 ) String (Examples: “Hi”, “Hello”, “Hi there!”) Boolean (Examples: True, False) List (Example: [ “hi”, “there”, “you” ] ) Tuple (Example: ( 4, 2, 7, 3) )

Statements x = 2 x = x + 2 print x Assignment Statement Assignment with expression Print statement Variable Operator Constant Reserved Word

Assignment Statements We assign a value to a variable using the assignment statement (=) An assignment statement consists of an expression on the right hand side and a variable to store the result x = 3.9 * x * ( 1 - x )

Numeric Expressions + Addition - Subtraction * Multiplication / Division ** Power % Remainder Because of the lack of mathematical symbols on computer keyboards - we use “computer-speak” to express the classic math operations Asterisk is multiplication Exponentiation (raise to a power) looks different from in math.

Parameters parameter: A value supplied to a command as you run it. Syntax: command ( value ) command ( value, value, ..., value ) Example: print sqrt(25) print sqrt(15 + 10 * 10 + 6) x = 5 print sqrt(x + sqrt(16)) You have already called System.out.println and passed parameters to it.

Integer Division >>> print 10 / 2 5 >>> print 9 / 2 Integer division truncates Floating point division produces floating point numbers >>> print 10 / 2 5 >>> print 9 / 2 4 >>> print 99 / 100 >>> print 10.0 / 2.0 5.0 >>> print 99.0 / 100.0 0.99 This changes in Python 3.0

Math commands To use these commands, place this line atop your program: from math import * Function name Description abs(value) absolute value ceil(value) rounds up cos(value) cosine, in radians floor(value) rounds down log10(value) logarithm, base 10 max(value1, value2) larger of two values min(value1, value2) smaller of two values round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root Constant Description e 2.7182818... pi 3.1415926...

input input : Reads a number from the user's keyboard. You can store the result of input into a variable. Example: age = input("How old are you? ") print "Your age is", age print "You have", 65 - age, "years until retirement" Output: How old are you? 53 Your age is 53 You have 12 years until retirement Exercise: Modify the restaurant program to ask the user how much of a tip to leave.

Numeric Types $ python >>> 3 + 2 5 >>> 18 % 5 3 >>> abs(-7) 7 >>> float(9) 9.0 >>> int(5.3) 5 >>> complex(1,2) (1+2j) >>> 2 ** 8 256 Numeric int: equivalent to C’s long int in 2.x but unlimited in 3.x. float: equivalent to C’s doubles. long: unlimited in 2.x and unavailable in 3.x. complex: complex numbers. Supported operations include constructors (i.e. int(3)), arithmetic, negation, modulus, absolute value, exponentiation, etc.

Exercise Write a program to prompt the user for hours and rate per hour to compute gross pay. Enter Hours: 35 Enter Rate: 2.75 Pay: 96.25