1 Programming for Engineers in Python Autumn 2011-12 Lecture 2: More Basics.

39
1 Programming for Engineers in Python Autumn 2011- 12 Lecture 2: More Basics

Transcript of 1 Programming for Engineers in Python Autumn 2011-12 Lecture 2: More Basics.

1

Programming for Engineers in

Python

Autumn 2011-12

Lecture 2: More Basics

2

Lecture 1: Highlights

• Functions

• Control structures (if/else)

• Strings

3

Today

• Leftovers from last week

• Compiler, interpreter

• Variables, types, operators, scope

4

Installing and Running Python 2.7

• Python 2.7 is already installed in the computers’ classroom

• Install:• Windows 32 bit: http://www.python.org/ftp/python/2.7.2/python-2.7.2.msi

• A “Python 2.7” folder will be created in your Start Menu:• IDLE (Python GUI) - recommended working environment

• Python (command line) - textual Python interpreter (a little faster, less convenient)

• Working with IDLE video: http://www.youtube.com/watch?v=lBkcDFRA958

5

String Type

http://docs.python.org/tutorial/introduction.html#strings

6

Strings Structure

7

SlicingHello

01234

-5-4-3-2-1

8

Lists

9

Iterating Over Listsfor var in list:

print(var)value in list

while loop in tirgul

10

Lists Built In Methods

11

Range, Sum

12

Abstruse Goose, Computer Programming 101,

13

Abstruse Goose, Under The Hood

14

Machine Code (Language)

• Computers understand only machine language• Basically looks like a sequence of 1’s and 0’s• Very inconvenient to work with and non intuitive

• All other computer languages were created for human convenience• The computer does not understand C/C#/Java/Scala• Must be “translated” into machine language

15

Programming Languages Basics

• A computer program is a sequence of instructions (texts) that can be “understood" by a computer and executed by it

• A programming language is a machine-readable artificial language designed to express computations that can be performed by a computer

16

Compiler

17

Hello World (in C) ;-)

18

Interpreter

19

Interpreter

• Python is an interpreted programming language

• The Read/Evaluate/Print Loop• Read an expression

• Compute its value• Print the result• Repeat the above

20

Language Selection and Python

• Goal• Runtime vs. Development time• Operating systems• Platforms

•Python is appropriate for quick development of small-medium tasks:

• Experiments• Data analysis• Small projects• Short development-execution rounds

21

Memory

22

Memory (Cont.)

• The computer memory is composed of a long list of bits

• Bits are grouped into bytes and words

• Every byte is numbered sequentially

• This number is called an address

23

Variables and Types

): יחידת מידע המאכסנת ערך variable (משתנה•במהלך ריצת התכנית

ניתן ל"גשת" למשתנה: לשנות (לא תמיד) או לקבל את •ערכו

ניתן לבצע פעולות חשבוניות (ואחרות) בעזרת משתנים•שקובע את אופן ביצוע )type (טיפוסלכל משתנה מוגדר •

הפעולות על המשתנה

24

Define Variables

variable name

valueassignment operator

Variable x “is of type” Int and is assigned the value 4

25

Why do We Need Variables?

• Computer programs manipulate data• Data is given as input or calculated throughout

the program• To be later accessed, variables must be

remembered• Thus, variables are stored in the memory• Variable name memory address

26

Types

• Determine number of bytes in memory (“locker size”)

• Determine the operations that are defined for a given variable

27

Some Basic Types

28

Variables and Types

29

Why Do We Need Different Types?

• Saving memory

• Execution speed

• Defined operations

• Makes compiler “life” easier (not relevant in Python)

30

Arithmetic Operators

31

Example 1

32

Types and Casting

33

Example 3

34

Comparisons

35

Example 1

36

Example 2

37

Logical Operators

01

000

101

01

001

111

01

10

and ornot

Truth Tables (1 – True, 0 - False)

38

Scope

Next Week – A Big Jump!

A cape will not help – work hard!