pythonnotes_1

  • Upload
    manasj

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 7/27/2019 pythonnotes_1

    1/3

    Python

    Python is an interpreted programming language. This year we will be replacing C++ with

    Python. Python, a general-purpose, object-oriented interpreted language you can use for countless

    standalone projects or scripting applications. Python is open source software i.e can be easily

    downloaded from net. We will be using Python 2.74 version.(for downloading visit

    www.python.org and select 2.7.4 version for window installer)

    Python is a case-sensitive. Python is a powerful, flexible programming language you can

    use in web/Internet development, to write desktop graphical user interfaces (GUIs), create

    games, and much more. Python is:

    High-level, meaning reading and writing Python is really easyit looks a lot like regular

    English!

    Interpreted, meaning you don't need a compiler to write and run Python! You can write it

    on your own computer

    Object-oriented, meaning it allows users to manipulate data structures called objects in

    order to build and execute programs. We'll learn more about objects later.

    Fun to use. Python is named after Monty Python's Flying Circus, and example code andtutorials often refer to the show and include jokes in order to make learning the language

    more interesting.

    Beginning With Python1.1. Context

    You have probablry used computers to do all sorts of useful and interesting things. In each application,the computer responds in different ways to your input, from the keyboard, mouse or a file. Still theunderlyingoperations are determined by the design of the program you are given. In this set of tutorials you will learnto write your own computer programs, so you can give the computer instructions to react in the way youwant.

    1.1.1. Low-Level and High-Level Computer Operations. First let us place Python programmingin the context of the computer hardware. At the most fundamental level in the computer there areinstructions built into the hardware. These are very simple instructions, peculiar to the hardware of yourparticular type of computer. The instructions are designed to be simple for the hardware to execute, notfor humans to follow. The earliest programming was done with such instructions. If was difficult and error-prone. A major advance was the development of higher-level languages and translators for them. Higher-

  • 7/27/2019 pythonnotes_1

    2/3

    level languages allow computer programmers to write instructions in a format that is easier for humans tounderstand. For example

    z = x+yis an instruction in many high-level languages that means something like:(1) Access the value stored at a location labeled x(2) Calculate the sum of this value and the value stored at a location labeled y(3) Store the result in a location labeled z.No computer understands the high-level instruction directly; it is not in machine language. A specialprogram must first translate instructions like this one into machine language. This one high-levelinstruction might be translated into a sequence of three machine language instructions corresponding tothe three step description above:

    000001001000000100000000100000100000010110000011

    Obviously high-level languages were a great advance in clarity!If you follow a broad introduction to computing, you will learn more about the layers that connect

    1.1.3. Obtaining Python for Your Computer. If you are not sure whether your computer alreadyhas Python, continue to Section 1.2.2, and give it a try. If it works, you are all set.

    If you do need a copy of Python, go to the Downloads page linked to http://www.python.org. Becareful to choose the version for your operating system and hardware. Chosse a stable version, 3.1 orlater. Do not choose a version 2.X, which is incompatible

    1.1.4. Windows in Idle. Idle has several parts you may choose to display, each with its own window.Depending on the configuration, Idle can start up showing either of two windows, an Edit Window or aPython Shell Window. You are likely to first see an Edit window, whose top left corner looks somethinglike in Windows:

    In the Shell the last line should look like>_>_>The >_>_> is theprompt, telling you Idle is waiting for you to type something. Continuing on the same lineenter

    6+3Be sure to end with the Enter key. After the Shell responds, you should see something like>_>_> 6+39>_>_>The shell evaluates the line you entered, and prints the result. You see Python does arithmetic. At theend you see a further prompt >_>_> where you can enter your next line .... The result line, showing 9, thatis

    produced by the computer, does not start with >_>_>.

    1.2. Integer Arithmetic1.2.1. Addition and Subtraction. We start with the integers and integer arithmetic, not becausearithmetic is exciting, but because the symbolism should be mostly familiar. Of course arithmetic is

    important in many cases, but Python is probably more often used to manipulate text and other sorts ofdata, as in the sample program in Section 1.2.2.Python understands numbers and standard arithmetic. For the whole section on integer arithmetic,where you see a set-off line in typewriter font, type individual lines at the >_>_> prompt in the PythonShell. Press Enter after each line to get Python to respond:772 + 35 - 7Python should evaluate and print back the value of each expression. Of course the first one does notrequire any calculation. It appears the shell just echoes back what you printed. Do note that the line with

  • 7/27/2019 pythonnotes_1

    3/3

    the valueproducedby the shell does not start with >_>_> and appears at the left margin. Hence you candistinguish what you type (after the >_>_> prompt) from what the computer responds.The Python Shell is an interactive interpreter. As you can see, after you press Enter, it is evaluatingthe expression you typed in, and then printing the result automatically. This is a very handy environmentto check out simple Python syntax and get instant feedback. For more elaborate programs that you wantto save, we will switch to an Editor Window later.1.2.2. Multiplication, Parentheses, and Precedence. Try in the Shell:2 x 3You should get your first syntaxerror. The x should have become highlighted, indicating the locationwhere the Python interpreter discovered that it cannot understand you: Python does not use x formultiplication as you may have done in grade school. The x can be confused with the use of x as avariable (more on that later). Instead the symbol for multiplication is an asterisk *. Enter each of thefollowing. You may include spaces or not. The Python interpreter can figure out what you mean eitherway. Try in the Shell:2*52 + 3 * 4If you expected the last answer to be 20, think again: Python uses the normal precedence of arithmeticoperations:Multiplications and divisions are done before addition and subtraction, unless there are parentheses.Try

    (2+3)*42 * (4 - 1)Now try the following in the Shell, exactly as written, followed by Enter, with no closing parenthesis:5 * (2 + 3 Look carefully. There is no answer given at the left margin of the next line and no prompt >_>_>to start a newexpression. If you are using Idle, the cursor has gone to the next line and has only indentedslightly.Python is waiting for you to finish your expression. It is smart enough to know that opening parenthesesare always followed by the same number of closing parentheses. The cursor is on a continuation line.Type just the matching close-parenthesis and Enter,)and you should finally see the expression evaluated.(In some versions of the Python interpreter, the interpreterputs ... at the beginning of a continuation line,rather than just indenting.) Negation also works. Try in the Shell:-(2 + 3)1.2.3. Division and Remainders. If you think about it, you learned several ways to do division.

    Eventually you learned how to do division resulting is a decimal. Try in the Shell:5/214/4As you saw in the previous section, numbers with decimal points in them are of type float in Python. Theyare discussed more in Section 1.14.1.In the earliest grades you would say 14 divided by 4 is 3 with a remainder of 2. The problem here is that the answer is in two parts, the integer quotient 3 and the remainder 2, and neither of these results isthe same as the decimal result. Python has separate operations to generate each part. Python uses thedoubled division symbol // for the operation that produces just the integer quotient, and introduces thesymbol % for the operation of finding the remainder. Try each in the Shell14/414//414%4

    Now predict and then try each of23//523%520%5

    6//8