getting started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/enbody/day02/02... · getting...

29
1 Getting Started CSE 231, Rich Enbody Michigan State University CSE 231, Fall 2013 Getting Started with Python 2 Office Hours After class By appointment – send an email

Upload: others

Post on 31-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

1

Getting Started

CSE 231, Rich Enbody

Michigan State University CSE 231, Fall 2013

Getting Started with Python

2

Office Hours

§  After class §  By appointment – send an email

Page 2: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

2

Michigan State University CSE 231, Fall 2013

Getting Started with Python

3

Project 1

§  Python arithmetic §  Do with pencil, paper and calculator first §  Idle §  Handin §  Help room

What is a Computer Program?

Page 3: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

3

Michigan State University CSE 231, Fall 2013

Getting Started with Python

5

Program §  A program is a sequence of instructions. §  To run a program is to:

o  create the sequence of instructions according to your design and the language rules

o  turn that program into the binary commands the processor understands

o  give the binary code to the OS, so it can give it to the processor

o  OS tells the processor to run the program o  when finished (or it dies :-), OS cleans up.

Michigan State University CSE 231, Fall 2013

Getting Started with Python

6

Python is an interpreted language §  You can simply open the Python interpreter,

and enter instructions one-at-a-time. Or §  You can import a program which causes the

instructions in the program to be executed, as if you had typed them in.

Page 4: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

4

Your First Program Example 1

Michigan State University CSE 231, Fall 2013

Getting Started with Python

8

First Program: number_input.py # input two numbers, add them together, print them out num_str1 = input('Please enter an integer:') num_str2 = input('Please enter a floating point number:') str1_int = int(num_str1) str2_float = float(num_str2) # this is a comment print('The numbers are: ',str1_int,' and ',str2_float) print('Their sum is:',str1_int+str2_float, \ 'and their product is:',str1_int*str2_float)

Page 5: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

5

Michigan State University CSE 231, Fall 2013

Getting Started with Python

9

input The function: input(“Enter a value”) §  prints “Enter a value” in the Python shell

and waits till the user types something (anything), followed by “Enter”

§  Warning, it returns a string (sequence of characters), no matter what is given, even a number (‘1’ is not the same as 1, different types)

Michigan State University CSE 231, Fall 2013

Getting Started with Python

10

What's a string The word "string" is used to indicate a sequence of characters, a compositor's term

Page 6: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

6

Michigan State University CSE 231, Fall 2013

Getting Started with Python

11

print my_var = 12 print(‘My var has a value of:’,my_var)

§  print takes a list of elements to print, separated by commas o  if the element is a string, prints it as is o  if the element is a variable, prints the value

associated with the variable o  after printing, moves on to a new line of output

Michigan State University CSE 231, Fall 2013

Getting Started with Python

12

At the core of any language

§  Control the flow of the program §  Construct and access data elements §  Operate on data elements §  Construct functions §  Construct classes §  Libraries and built-in classes

Page 7: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

7

Michigan State University CSE 231, Fall 2013

Getting Started with Python

13

Save as a “module”

§  Save with a .py suffix, so it becomes a Python module

§  You “run” the module from the IDLE menu (F5) to see the results of the operation

§  A module is just a file of Python commands

Michigan State University CSE 231, Fall 2013

Getting Started with Python

14

Errors

Page 8: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

8

Michigan State University CSE 231, Fall 2013

Getting Started with Python

15

Common Error

§  If you save the file without a .py suffix, it will stop colorizing and formatting the file.

§  Fix: save with the .py

Michigan State University CSE 231, Fall 2013

Getting Started with Python

16

Syntax

§  Lexical components. §  A Python program is:.

o  A module (perhaps more than one) o  Each module has Python statements o  Each statement has expressions

Page 9: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

9

Michigan State University CSE 231, Fall 2013

Getting Started with Python

17

Modules

http://pypi.python.org

Michigan State University CSE 231, Fall 2013

Getting Started with Python

18

Statements are commands in Python. They perform some action,

often called a side effect, but they do not return any values

Page 10: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

10

Michigan State University CSE 231, Fall 2013

Getting Started with Python

19

Expressions perform some operation and return a value.

Expressions can act as statements, but statements cannot act as expressions (more on this later).

Expressions typically do not modify values in the interpreter.

Michigan State University CSE 231, Fall 2013

Getting Started with Python

20

Note that MPL sometimes only asks for an expression

Page 11: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

11

Michigan State University CSE 231, Fall 2013

Getting Started with Python

21

What is the difference between a side effect and a return?

§  1+2 returns a value (it’s an expression).

You can “catch” the return value (x=1+2). However, nothing else changed.

§  print(“hello”) doesn’t return anything, but something else, the side effect, did happen. Something printed!

Michigan State University CSE 231, Fall 2013

Getting Started with Python

22

Python name conventions §  must begin with a letter or _

o  Ab123 is OK, but 123ABC is not. §  may contain letters, digits, and underscores

o  this_is_an_identifier_123

§  may be of any length §  upper and lower case letters are different

o  LengthOfRope is not lengthofrope §  names starting with _ have special meaning.

Be careful

Page 12: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

12

Michigan State University CSE 231, Fall 2013

Getting Started with Python

23

A comment begins with a “#” §  From the “#” to the end of that line:

nothing will be interpreted by Python. §  Comments help the human reader.

Michigan State University CSE 231, Fall 2013

Getting Started with Python

24

Code as essay.

Page 13: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

13

Michigan State University CSE 231, Fall 2013

Getting Started with Python

25

Knuth, Literate Programming (‘84)

Let us change our traditional attitude to the construction of programs:

Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.

Michigan State University CSE 231, Fall 2013

Getting Started with Python

26

Python types

§  integers (int): 5 §  floating point (float): 1.2 §  Booleans (bool): True §  Strings (str): “anything” or ‘something’ §  lists: [‘a’,1,1.3] Others…

Page 14: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

14

Michigan State University CSE 231, Fall 2013

Getting Started with Python

27

A type in Python defines two things: o  what it contains o  the kinds of operations you can perform on it

Michigan State University CSE 231, Fall 2013

Getting Started with Python

28

Fundamental Types §  Integers (int)

o  1, -27

§  Reals (float) o  3.14, 10., .001, 3.14e-10, 0e0

§  Booleans o  True, False note the capital

Page 15: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

15

Example 2

Worksheet 1

Page 16: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

16

Michigan State University CSE 231, Fall 2013

Getting Started with Python

31

A character ‘1’ is not an integer 1. §  You need to convert the value returned by

the input command (characters) into an integer (int)

§  int(“123”) yields the integer 123

Michigan State University CSE 231, Fall 2013

Getting Started with Python

32

Type conversion

§  int(some_var) converts to an integer §  float(some_var) converts to a float §  str(some_var) converts to a string

Page 17: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

17

Michigan State University CSE 231, Fall 2013

Getting Started with Python

33

import math imports the math module §  Brings in Python statements to support math §  Use math prefix: math.xxx

math.pi is π math.sqrt(2) finds the square root of 2

Michigan State University CSE 231, Fall 2013

Getting Started with Python

34

When = doesn’t mean equal

my_int = my_int + 7

Page 18: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

18

Michigan State University CSE 231, Fall 2013

Getting Started with Python

35

= is assignment

lhs = rhs

What “assignment” means is:

o  evaluate all the “stuff” on the rhs of the = o  take the resulting value and

associate it with the name on the lhs

Michigan State University CSE 231, Fall 2013

Getting Started with Python

36

Variable Objects §  Python maintains a list of pairs for every variable:

o  variable’s name o  variable’s value

§  A variable is created when a value is first assigned. It associates a name and a value.

§  Subsequent assignments update the associated value. §  We say a name references a value. §  A variable’s type depends on what is assigned.

X = 7 Name Value X 7

Page 19: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

19

Michigan State University CSE 231, Fall 2013

Getting Started with Python

37

Namespace: a table that associates a name with a value.

Namespace

X 7

Michigan State University CSE 231, Fall 2013

Getting Started with Python

38

Assignment Statement §  Example: x = 2 + 3 * 5

o  evaluate expression (2+3*5): 17 o  change the value of x to reference 17

§  Example (y has value 2): y = y + 3 o  evaluate expression (y+3): 5 o  change the value of y to reference 5

Page 20: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

20

Michigan State University CSE 231, Fall 2013

Getting Started with Python

39

Namespace

x 17

x = 2 + 3 * 5 y = 2 y = y + 3

y 2

5

Michigan State University CSE 231, Fall 2013

Getting Started with Python

40

More on types

§  Python does not require you to pre-define the type of a variable

§  What type a variable holds can change §  Nonetheless, knowing the type can be

important for using the correct operation on a variable.

Page 21: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

21

Michigan State University CSE 231, Fall 2013

Getting Started with Python

41

Namespace

x 2

x = 2 x = 7.5 x = ‘abc’

7.5

‘abc’

Michigan State University CSE 231, Fall 2013

Getting Started with Python

42

What can go on the lhs?

§  The lhs must indicate a name with which a value can be associated.

§  Must follow the naming rules my_int = 5, Yes my_int + 5 = 7, No

Page 22: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

22

Example 3

Michigan State University CSE 231, Fall 2013

Getting Started with Python

44

More on whitespace

§  For the most part, you can place “white space” (spaces) anywhere in your program

§  use it to make a program more readable 1 + 2 - 4

Page 23: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

23

Michigan State University CSE 231, Fall 2013

Getting Started with Python

45

continuation

Python is sensitive to the end of line. To make a line continue, use: \

print(“this is a test”, \

“ of continuation”)

prints this is a test of continuation

Michigan State University CSE 231, Fall 2013

Getting Started with Python

46

Python is sensitive to tabs. (indentation)

Page 24: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

24

Michigan State University CSE 231, Fall 2013

Getting Started with Python

47

Syntax: tokens

Michigan State University CSE 231, Fall 2013

Getting Started with Python

48

Python Keywords: cannot use as variable names

and del from not while

as elif global or with

assert else if pass yield

break except import print

class exec in raise

continue finally is return

def for lambda try

Page 25: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

25

Michigan State University CSE 231, Fall 2013

Getting Started with Python

49

Python Operators

+ - * ** / // %

<< >> & | ^ ~

< > <= >= == !=

+= -= *= /= //= %=

&= |= ^= >>= <<= **=

Michigan State University CSE 231, Fall 2013

Getting Started with Python

50

Python Punctuators and Delimiters

($ and ? not allowed)

‘ “ # \

( ) [ ] { } @

, : . ` = ;

Page 26: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

26

Michigan State University CSE 231, Fall 2013

Getting Started with Python

51

Operators §  Integer (int)

o  addition and subtraction: +, - o  multiplication: * o  division: 3 kinds!

•  division: / •  quotient: // •  remainder: %

o  exponent: ** §  Floating point (float)

o  add, subtract, multiply, divide, exp: +, -, *, /, **

Michigan State University CSE 231, Fall 2013

Getting Started with Python

52

Types and division

int 5/3 is division, yields float: 1.66666 5//3 is the quotient, yields integer: 1 5%3 is the remainder, yields integer: 2

float 5.0/3.0 is division, yields float: 1.66666

Page 27: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

27

Michigan State University CSE 231, Fall 2013

Getting Started with Python

53

Mixed Types

§  4//3 is 1 (integer quotient) §  4.0/3.0 is 1.3333333 (float division) §  What is 4/3.0?

o  No mixed-type operations. Must convert. o  Python automatically converts to float.

Thus 4 → 4.0 so the result is 1.3333333

Michigan State University CSE 231, Fall 2013

Getting Started with Python

54

Try 5//2 5//2.0 3+4.0 1234//100 1234%100 1234//100%10 int(1.234) float(2)

Page 28: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

28

Michigan State University CSE 231, Fall 2013

Getting Started with Python

55

Collections of data types (later in semester)

§  list o  sequence of any data elements

§  dictionary o  a collection of name:value pairs. Very powerful!

§  class o  a user-defined data type

Michigan State University CSE 231, Fall 2013

Getting Started with Python

56

Collections and building blocks (examples of what’s to come later)

§  Point (x,y,z are real numbers) §  Plane (3 points in a plane) §  Line segment (2 end points) §  Face (N points on a plane) §  Solid (N faces) §  Solid (N planes)

Page 29: Getting Started - web.cse.msu.eduweb.cse.msu.edu/~cse231/lectures/Enbody/day02/02... · Getting Started with Python 13 Save as a “module” ! Save with a .py suffix, so it becomes

29

Michigan State University CSE 231, Fall 2013

Getting Started with Python

57

Note §  The interpreter translates Python into

machine (computer) language. The first stage of that process is determining if it is valid Python code.

§  If the program is not a valid Python program, the interpreter cannot translate it.

§  If the interpreter cannot translate your code, it is not a Python program so it is not worth any points.