ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python...

13
ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk with you to the exciting world of Python This is a course In designing a program using Python to perform a task on or through computer It is intended for beginners moving on to becoming professionals, that is, people who can produce systems that others will use who are willing to work hard

Transcript of ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python...

Page 1: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1

Welcome!

This is Professor Jai P. Agrawal.I will walk with you to the exciting world of Python

This is a course

• In designing a program using Python to perform a task on or through computer

• It is intended for beginners moving on to becoming professionals, that is, people who can produce systems that others will use– who are willing to work hard

Page 2: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 2

Course Organization• To access the course website go to http://ecet.purduecal.edu/moodle/.• Click on Spring 2015.• Click on ECET 49900 – Python • Log in with the username and password that you have received in the email from the course

administrator.• Go to My profile in the left side bar and change your password. • Save the username and the new password for future use. Enjoy all features of the website now

while you are there.• The course website is organized in a weekly format – 17 weeks with one week mid-semester

recess.• Download the following before you start this class:

– http://www.python.org/download/ and download Python 2.7.3 Windows Installer

• You must visit the class website on the scheduled dates and times of the class, which is 3 times a week. More visits are welcome.

• The lectures are posted in each week.• There may be one or more activities in each week which may require submission by a due date.

Submit your assignment etc. on the related activity only.• All activities will be graded, a late submission will cost 20% of the grade. The submissions later

than one week will be accepted but not graded.• Read every Lecture on your pace, there are 3 lectures in every week, each 50 minutes long.• Write your questions in the News Forum simultaneously wherein may be one of your co-students

can answer for you. It is called horizontal learning.• Write email to your instructor if you have a question. You should get an answer within two days. It

is called vertical learning.

Page 3: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 3

Python is a powerful programming language.

It has efficient high-level data structures and a simple but effective approach to object-oriented programming.

Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

By the way, the language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles.

Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than other programming languages such as C.

Page 4: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 4

Python offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries.

Python allows us to split a large program into modules that can be reused in other Python programs.

It comes with a large collection of standard modules that we can use as the basis of our programs — or as examples to start learning to program in Python.

Python is an example of a high-level language.

Two kinds of programs process high-level languages into low-level languages: 

interpreters and compilers.

Page 5: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 5

An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations.

A compiler reads the program and translates it completely before the program starts running. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable. Once a program is compiled, you can execute it repeatedly without further translation.

Page 6: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 6

Many modern languages use both processes. They are first compiled into a lower level language, called byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but because of the way programmers interact with it, it is usually considered an interpreted language. I hope that you have downloaded all softwares necessary to go through this course.

Python is a truly object-oriented languages.Python operates on objects using methods.

Examples of Objects: Student, teacher, bolleyball, book etc.

Examples of methods: exponent, power, squareroot, insert in a string, sort numbers and words etc.

Objects have attributes and special methods.Objects have relatives and they interact with each other.

Page 7: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 7

Python organization:

The python objects are organized as

Packages: equivalent to directoriesModules: equivalent to files such as .c, .cpp, .m in other languages

Comprised of instructions to be executed in an order. The instructions are also called statements

Functions: equivalent to functions, procedures or subroutines in other languagesMethods: equivalent to operations in other languages

A module is saved as xxx.pyA module can be used as an object. There are some built-in modules such as math.py.A programmer can write own modules.

Page 8: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 8

We will use open source Python Integrated Development Environment (IDLE) for this course.

There are two ways to use the Python interpreter: a) shell mode and b) script mode.

In shell mode, you type Python statements into the Python shell in IDLE and the interpreter immediately prints the result.

In script mode, you can write a program in a file and use the interpreter to execute the contents of the file. Such a file is called a script.

Page 9: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 9

First Python Program in Shell ModeOpen the IDLE (PYTHON GUI) program, such as shown on the right:

We will call this window as the Interpreter Window.

Go back to the Interpreter and please keep it open throughout the lecture.

The >>> is a prompt which appears in the interpreter window of the Python IDLE to prompt the user to enter a command (a python statement ) followed by a return. The python executes the statement and returns the result.

To exit the Interpreter, type in the <Ctrl-D> on the python prompt, press control key, press D while keep pressing the control key.

Page 10: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 10

The statement in the blue background is a python statement. Press <Enter> at the end of the statement in the line.

This program prints a string enclosed within single quotes (‘ ‘) or double quotes (“ “).

Python is sensitive to indents, specially at the beginning of a line, therefore type in the statements exactly as given. A new statement opening with an indent (or more) will generate an error in the interpreter’s result.

Let us write a welcome message in shell mode, enclosed in ‘…’:

>>> print 'Hello Friends!! It is a wonderful world!!!'

Hello Friends!! It is a wonderful world!!!

The Python executes and returns the result, as shown in the yellow window below.

If the string is longer than one line, it can be broken by adding a back slash ( \ ) at the end of line. However, the printed result continues without a break, does not introduce a new line, in the result. Try it out.

Page 11: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 11

More in the shell mode,

>>> #Program to calculate the cost of apples>>> #the price of an apple is $0.20, find the cost of 25 apples.>>> price = 0.20 # the price per apple>>> Number_apples = 25>>> cost = price * Number_apples>>> print 'the cost of 25 apples is $', cost # print shell consists of a string and an \ expression involving a variable, separated by a comma.

the cost of 25 apples is $ 5.0

The lines 1 and 2 are single-line comments, ignored by the interpreter.The comment line can have initial indent as in line 2. The comment line can also be after a python shell as shown in line 3 and line 6-7.Lines 4 and 5 are python shells, assign the value of expression to the right of = to the variable on the left. The variable name must not have any space in the middle. Words in a variable name can be joined using underscore ( _ ), but not a dash ( - ).

The print shell can have any combination of strings and expressions, separated by commas.The interpreter returns the result as shown below:

Page 12: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 12

Let us write a python script to do the same. Open file/new window (also called the new module window) and type in the following:

#apples.py#the price of an apple is $0.20, find the cost of 25 apples.price = 0.20Number_apples = 25cost = price * Number_applesprint 'the cost of 25 apples is $', cost

>>> ================= RESTART ================================>>> the cost of 25 apples is $ 5.0

The code in the module is given in green window. Python ignores any blank spaces in a line but does not want any indent at beginning of a line. Please do not add any indent carelessly.

Save it as a module apples.py. Run it in the module window. The Python executes the whole file and presents the result in the Interpreter window, shown below in yellow.

Page 13: ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.

ECET 49900 – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 13

Exercise 1: Due Now. Write a python program in the script mode using a module to do the following:

Calculate and print the area of a circle of radius = 4 meters, in square meters. Use the following formula:

Area of circle = 3.1415 x radius * radius

A break of 10 minutes. Try to write the code yourself. Come back and see my script.

Multiple assignments are allowed likeR=4; g=3OrR, g = 4, 3