Python Programming

33
Python Programming

description

Python Programming. Installing Python. We are going to use Jython . Install JES ( Jthon Environment for Students) JES: Like a text editor comes with a bunch of built-in stuff that you can use to work on images and sound files by writing small chunks of code. Installation. - PowerPoint PPT Presentation

Transcript of Python Programming

Page 1: Python Programming

Python Programming

Page 2: Python Programming

Installing Python

• We are going to use Jython.• Install JES (Jthon Environment for Students)• JES:– Like a text editor– comes with a bunch of built-in stuff that you can

use to work on images and sound files by writing small chunks of code.

Page 3: Python Programming

Installation• Installing JES and starting it up

– Go to http://www.mediacomputation.org and get the version of JES for your computer.• If you know that you have a Java compiler (e.g., a “JDK” or an “IDE”)

– Windows users: • Just copy the folder• Double-click JES application

– If trouble, try jes2.bat or jes-customjava.bat– Mac users:

• Just copy the folder• Double-click JES application

• There is always Help– Lots and lots of excellent help

Page 4: Python Programming

The JES Window• Once you install JES and fire it up, you’ll see

something like this:

Program area• write your Jython programs here• provides a text editor•after typing in your program, go to File > Save Program As, using a filename with extension .py (for Python)

Command area• can type in Jython/Python commands here at the “>>>” prompt, hit Enter •can type in names of loaded programs to be run•commands are interpreted by Jython interpreter

Load button• After saving a program created in the Program Area, use the load button to load the file that is currently in the program area so it can be recognized in the command area.

prompt

Page 5: Python Programming

Numbers and Expressions

• The interactive Python interpreter can be used as a powerful calculator. Try the following:>>> 2 + 2

• This ought to give you the answer 4. That wasn’t too hard. Well, what about this:>>> 53672 + 235253288925

Page 6: Python Programming

QUESTION

• What is the output of 1+2*3 ? • Is it 9 or 7?

>>> 1+2*37

• How to add 1 and 2 first and then multiply that with 3?>>> (1+2)*39

Page 7: Python Programming

Operator Precedence• The following table summarizes the operator precedences in

Python, from lowest precedence to highest precedence.

Page 8: Python Programming

• Operators in the same box have the same precedence.

• Operators in the same box group left to right (except for comparisons, which all have the same precedence and chain from left to right and exponentiation, which groups from right to left).

Page 9: Python Programming

• All the usual arithmetic operators work as expected—almost!– One potential trap: integer division

>>> 1/20

• What happened here?– One integer (a nonfractional number) was divided by

another, and the result was rounded down to give an integer result

• How to do ordinary division?

Page 10: Python Programming

• Two solutions:

1. use real numbers (numbers with decimal points) rather than integers• Real numbers are called floats (or floating-point numbers) in Python• if either one of the numbers in a division is a float, the result will be, too:>>> 1.0 / 2.00.5>>> 1/2.00.5>>> 1.0/20.5

2. tell Python to change how division works.>>> from __future__ import division

>>> 1 / 20.5>>> 1 // 20

Page 11: Python Programming

• Now you’ve seen the basic arithmetic operators (addition, subtraction, ultiplication, and division), but one more operator is quite useful at times:>>> 1 % 21

• This is the remainder (modulus) operator—x % y gives the remainder of x divided by y.

Page 12: Python Programming

QUESTION

>>> 10 / 33

>>> 10 % 31

>>> 9 % 30

>>> 2.75 % 0.50.25

Week1

Page 13: Python Programming

• The last operator is the exponentiation (or power) operator:>>> 2 ** 38>>> -3 ** 2-9>>> (-3) ** 29

• Does it makes a difference if you write -3**2?– Yes. It gives -9 as output because exponentiation operator

has higher precedence than negation

Page 14: Python Programming

Large Integers• Python can handle really large integers:

>>> 10000000000000000001000000000000000000L

• What happened here? The number suddenly got an L tucked onto the end.

• Ordinary integers can’t be larger than 2147483647 (or smaller than –2147483648);

• If you want really big numbers, you have to use longs. A long (or long integer) is written just like an ordinary integer but with an L at the end.

Page 15: Python Programming

• Well, can you do math with these monster numbers, too? Sure thing. Consider the following:>>> 1987163987163981639186L * 198763981726391826L + 23

394976626432005567613000143784791693659L

• As you can see, you can mix long integers and plain integers as you like.

• You won’t have to worry about the difference between longs and ints unless you’re doing type checking.

Page 16: Python Programming

Variables• A variable is a name that represents (or refers to) some value. • E.g. name x to represent 3.

>>> x = 3>>> x3

• This is called an assignment. We assign the value 3 to the variable x. • After a variable has had a value assigned to it, you can use the variable in

expressions:• E.g.

>>> x * 26

• Note that you have to assign a value to a variable before you use it. After all, it doesn’t make any sense to use a variable if it doesn’t represent a value, does it?

• Variable names can consist of letters, digits, and underscore characters (_). A variable can’t begin with a digit, so Plan9 is a valid variable name, whereas 9Plan is not.

Page 17: Python Programming

Statements

• Until now we’ve been working (almost) exclusively with expressions. But what about statements—the instructions?

• Print Statement:– E.g :

>>> print "Hello, world!“Hello, world!>>> print 2 * 36

• Assignment statement:– E.g. >>> x = 3

Page 18: Python Programming

• So, what’s the difference between a statement and an expression?– An expression is something, while a statement does something (or,

rather, tells the computer to do something)– E.g. 2*2 is 4, whereas print 2*2 prints 4.

• We can print values, expressions, anything with print

• Consider the following:>>> 2*24>>> print 2*24

Page 19: Python Programming

• As long as you execute this in the interactive interpreter the results are similar, but that is only because the interpreter always prints out the values of all expressions .

• That is not true of Python in general.

• Later in this chapter, you’ll see how to make programs that run without this interactive prompt.

Page 20: Python Programming

Getting Input from the User

• Let’s take a look at the useful function input.>>> input("The meaning of life: ")The meaning of life: 4242

• What happens here?1. The first line (input(...)) is executed in the interactive interpreter.2. interpreter prints out the string "The meaning of life: " as a new

prompt. 3. I type 42 and press Enter. 4. The resulting value of input is that very number, which is

automatically printed out in the last line.• Not very useful.

Page 21: Python Programming

Question

• Write a simple python program in your interactive interpreter to print the value of variables x*y into the screen. The values of x and y must be initialized based on user input.>>> x = input("x: ")x: 34>>> y = input("y: ")y: 42>>> print x * y1428Values entered (34 and 42) would be supplied by some user

Page 22: Python Programming

Functions• In the section on numbers and expressions I used the exponentiation

operator (**) to calculate powers. >>> 2**38

• The fact is that you can use a function instead, called pow: >>> pow(2,3)

8• A function is like a little program that you can use to perform a specific

action. • Python has lots of functions that can do many wonderful things. In fact,

you can make your own functions, too (more about that later).• Therefore we often refer to standard functions such as pow as built-in

functions.

Page 23: Python Programming

• Using a function as I did in the preceding example is called calling the function.

• You supply it with parameters (in this case, 2 and 3) and it returns a value to you.

• Because it returns a value, a function call is simply another type of expression.

• In fact, you can combine function calls and operators to create more complicated expressions.

Function callpow(2,3)

Parameter list

Page 24: Python Programming

QUESTION

• Write an expression to calculate 10 + 23*5/3.0. Use pow function to calculate power.

>>> 10 + pow(2, 3*5)/3.010932.666666666666

Page 25: Python Programming

QUESTION

• Write a program Hello.py that prompts the user message “What is your name” and take user input name and print hello to the user. Here is a sample output:>>> hello()What is your name? JaneHello, Jane!

Page 26: Python Programming

• Answer:

def hello():name = input("What is your name? ")print "Hello, " + name + "!"

Page 27: Python Programming

Comments

• The hash sign (#) is a bit special in Python. • When you put it in your code, everything to the

right of it is ignored• E.g:

• Comments are useful in making programs easier to understand—both for other people and for yourself when you come back to old code.

This is a comment

Page 28: Python Programming

• Make sure your comments say significant things and don’t simply restate what is already obvious from the code.

• Useless, redundant comments may be worse than none.

• E.g. in the following example, a comment isn’t really called for:# Get the user's name:user_name = input("What is your name?")

Page 29: Python Programming

Strings

• Recall the print statement we wrote earlier:print “Hello, world!”

• “Hello world!” in this statement is called a string (as in “a string of characters”).

• Strings are values, just like numbers are:>>> "Hello, world!"‘Hello, world!’

Page 30: Python Programming

• There is one thing that may be a bit surprising about this example, though: When Python printed out our string, it used single quotes, whereas we used double quotes.

• What’s the difference?• Actually, there is no difference:>>> 'Hello, world!''Hello, world!'

Page 31: Python Programming

Concatenating Strings

• Method 1:>>> "hello " "world"'hello world'– I’ve simply written two strings, one after the other, and Python

automatically concatenates them (makes them into one string).– Isn’t used very often.– Only works when you actually write both strings at the same

time, directly following one another:>>> x = "Hello, ">>> y = "world!">>> x ySyntaxError: invalid syntax

Page 32: Python Programming

• Method 2:• Use ‘+’ operator, just like adding numbers:>>> "Hello, " + "world!"'Hello, world!'>>> x = "Hello, ">>> y = "world!">>> x + y'Hello, world!'

Page 33: Python Programming

Examples of Types

12

Integers

Floats

Strings

-12

31,364

0.01

12.99834,654.01

1.01

MarkBarbara Ericson

85 5th Street NWInside the computer,these are all just bits