Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming with Python

Similar presentations


Presentation on theme: "Introduction to Programming with Python"— Presentation transcript:

1 Introduction to Programming with Python

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

3 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)

4 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.

5 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:

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

7 Basic Programs and Numeric Data

8 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?"

9 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?'

10 Expressions expression: A value or operation(s) to compute a value.
Example: * 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 * is 13 (1 + 3) * 4 is 16

11 Integer division When we divide integers with / , the quotient is an integer. 4 ) ) 1425 54 21 35 / 5 is 7 84 / 10 is 8 The % operator computes a remainder from integer division. 4 ) ) 218 15 3 Dividing by 0 crashes the program.

12 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 gpa A variable can be used in expressions. x + 4 is 9

13 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

14 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: spam #sign var.12 Different: spam Spam SPAM

15 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

16 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) )

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

18 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 = * x * ( x )

19 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.

20 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( * ) x = 5 print sqrt(x + sqrt(16)) You have already called System.out.println and passed parameters to it.

21 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

22 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 pi

23 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.

24 Numeric Types $ python >>> >>> 18 % >>> abs(-7) 7 >>> float(9) >>> int(5.3) 5 >>> complex(1,2) (1+2j) >>> 2 ** 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.

25 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


Download ppt "Introduction to Programming with Python"

Similar presentations


Ads by Google