jagomart
digital resources
picture1_Python Pdf 186364 | Programming Tutorial


 143x       Filetype PDF       File size 0.21 MB       Source: courses.ischool.berkeley.edu


File: Python Pdf 186364 | Programming Tutorial
2 programming fundamentals and python 2 1 introduction this chapter provides a non technical overview of python and will give you the know how to survive the next few chapters ...

icon picture PDF Filetype PDF | Posted on 02 Feb 2023 | 2 years ago
Partial capture of text on file.
                    2. Programming Fundamentals and Python
          2.1 Introduction
          This chapter provides a non-technical overview of Python and will give you the know-how to survive
          the next few chapters. It contains many working snippets of code which you should try yourself — in
          fact, we insist! There is no better way to learn about programming than to dive in and try the examples.
          Hopefully, you will then feel brave enough to modify and combine them for your own purposes, and
          so before you know it you are programming!
            For a more detailed overview, we recommend that you consult one of the introductions listed in the
          further reading section below. More advanced programming material is contained in a later tutorial.
          2.2 PythontheCalculator
          One of the newbie friendly things about Python is that it allows you to type things directly into the
          interpreter — the program that will be running your Python programs. We want you to be completely
          comfortable with this before we set off, so let’s start it up:
          Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
          Type "copyright", "credits" or "license()" for more information.
          >>>
            Dependingonyourenvironment,theversionnumberandtheblurbatthetopmightbedifferent. As
          longasyouarerunningPython2.4orgreatereverythingwillbefine. ThePythonprompt>>>indicates
          it is now waiting for input. If you are using the Python interpreter through the Interactive DeveLopment
          Environment (IDLE) then you should see a colorized version, like the brown prompt above. We have
          colorized the notes in the same way, so that you can tell if you have typed the code in correctly.
            So, let’s use the Python prompt as a calculator:
              >>> 3 + 2 5 - 1
                     *
              12
              >>>
            There are several things to notice here, the first being that once the interpreter has finished calcu-
          lating the answer and displaying it, the prompt reappears. This means the Python interpreter is waiting
          for another instruction. The second thing to notice is that Python deals with the order of operations
          correctly (unlike some older calculators), so the 2 5 is calculated before it is added to 3.
                                      *
            Try a few more expressions of your own. You can use asterisk ( ) for multiplication and slash (/)
                                                 *
          for division, and parentheses for bracketing expressions. One strange thing you might come across is
          division doesn’t always behave how you expect:
                                       1
                  Introduction to Natural Language Processing (DRAFT)            2. Programming Fundamentals and Python
                        >>> 3/3
                        1
                        >>> 1/3
                        0
                        >>>
                      Thesecondcaseissurprising because we would expect the answer to be 0.333333. We will come
                  back to why that is the case later on in this chapter.
                      Youprobablyalreadyhaveacalculator, so why is it so important that you can use Python like one?
                  Well, there are two main reasons:
                     • this demonstrates that you can work interactively with the interpreter, which means you can
                        experiment and explore;
                     • yourintuitions about numerical expressions will be very useful for manipulating all kinds of data
                        in Python.
                      Youshouldalso try nonsensical expressions to see how the interpreter handles it:
                      Here we have produced a syntax error because it doesn’t make sense to end an instruction with a
                  plus sign. The Python interpreter indicates on what line and where on the line the particular problem
                  occurred. Be careful about this though because sometimes the interpreter can get confused and point
                  to something after the actual error. If you are in IDLE, you will only see the last line and the previous
                  line will be highlighted where the problem is.
                  2.3 Understanding the Basics
                  2.3.1 Representing text
                  We can’t just type text directly into the interpreter because it would try to interpret it is part of the
                  Python language itself:
                      This is an example of the Python interpreter getting confused about where the syntax error is. It
                  should actually be pointing to the beginning of the Hello.
                      Pythonrepresents text using a type called a string. Strings are delimited or separated from the rest
                  of the program by quotation marks:
                        >>> ’Hello World’
                        ’Hello World’
                        >>> "Hello World"
                        ’Hello World’
                        >>>
                      Wecanuseeither single or double quotation marks, as long as we use the same ones on either end
                  of the string.
                      Wecanperform similar calculator-like operations on strings that we can perform on integers. For
                  example, adding two strings together seems intuitive enough that you could guess what the result is:
                        >>> ’Hello’ + ’World’
                        ’HelloWorld’
                        >>>
                  Bird, Curran, Klein & Loper                      2-2                                       July 9, 2006
                  Introduction to Natural Language Processing (DRAFT)            2. Programming Fundamentals and Python
                      This operation is called concatenation and it returns a new string which is a copy of the two
                  original strings pasted together end to end. Notice that this doesn’t do anything clever like insert a
                  space between the words — because the Python interpreter has no way of knowing that you want a
                  space, it only does exactly what it is told. Based on this it is also possible to guess what multiplication
                  might do (we have given you a hint too!):
                        >>> ’Hi’ + ’Hi’ + ’Hi’
                        ’HiHiHi’
                        >>> ’Hi’ 3
                                   *
                        ’HiHiHi’
                        >>>
                      Thepointtotakefromthis(apartfromlearningaboutstrings)isthatinPythonintuitionaboutwhat
                  should work gets you a long way, so it is worth just trying things to see what happens, you are very
                  unlikely to break something, so just give it a go!
                  2.3.2 Storing and reusing values
                  After a while it gets quite tiresome to be retyping strings and other expressions over and over again.
                  Yourprogramswon’tgetveryinterestingorhelpfuleither. Whatweneedisaplaceforstoringresultsof
                  calculations and other values, so we can access them more conveniently, say with a name for instance.
                  Thenamedplaceiscalled a variable.
                      In Python we create variables by assignment, which involves putting a value into the variable:
                        >>> msg = ’Hello World’
                        >>> msg
                        ’Hello World’
                        >>>
                      Here we have created a variable called msg (short for message) and stored in it the string value
                  ’Hello World’. Notice the Python interpreter returns immediately because assignment returns no
                  value. On the next line we inspect the contents of the variable by naming it on the command line msg.
                  Theinterpreter prints out the contents on the next line.
                      Wecanusevariables in any place where we used values previously:
                        >>> msg + msg
                        ’Hello WorldHello World’
                        >>> msg * 3
                        ’Hello WorldHello WorldHello World’
                        >>>
                      Wecanalsoassignanewvaluetoavariablejust by using assignment again:
                        >>> msg = msg 2
                                          *
                        >>> msg
                        ’Hello WorldHello World’
                        >>>
                      Here we have taken the value of msg, multiplied it by 2 and then stored that new string (Hello
                  WorldHello World)backintothevariable msg. You need to be careful not to treat the = as equality
                  as it would be in mathematics, but instead treat it as taking the result of the expression calculated on
                  the right, and assign it to the variable on the left.
                  Bird, Curran, Klein & Loper                      2-3                                       July 9, 2006
                  Introduction to Natural Language Processing (DRAFT)            2. Programming Fundamentals and Python
                  2.3.3 Printing and inspecting
                  Sofar, when we have wanted to look at the contents of a variable or see the result of a calculation, we
                  have just typed it into the interpreter. For example, we can look at the contents of msg by going:
                        >>> msg
                        ’Hello World’
                        >>>
                      However, this trick only works in the interactive interpreter. When we want users of our programs
                  to be able to see the output, we must use the print statement:
                        >>> print msg
                        Hello World
                        >>>
                      Hmm, that seems to do the same thing! However, on closer inspection you will see that the
                  quotation marks which indicate that Hello World is a string are missing in the second case. That is
                  because inspecting a variable (that only works in the interpreter) prints out the Python representation
                  of a value, whereas the print statement only prints out the value itself, which in this case is just the
                  text in the string.
                      So, if you want the users of your program to be able to see something then you need to use print.
                  If you just want to check the contents of the variable while you are developing your program in the
                  interactive interpreter then you can just type the variable name directly into the interpreter.
                  2.3.4 Exercises
                     1. Start up the Python interpreter (e.g. by running IDLE). Try the examples in the last section,
                        before experimenting with the use of Python as a calculator.
                     2. Try the examples in this section, then try the following.
                          a) Createavariablecalledmsgandputsomemessageofyourowninthisvariable.
                             Rememberthatstrings need to be quoted, so you will need to type:
                             >>> msg = "I like NLP!"
                          b) Now print the contents of this variable in two ways, first by simply typing the
                             variable name and pressing enter, then by using the print command.
                          c) Try various arithmetic expressions using this string, e.g. msg + msg, and 5 *
                             msg.
                          d) Define a new string hello, and then try hello + msg. Change the hello
                             string so that it ends with a space character, and then try hello + msg again.
                  2.4 Manipulating strings and storing multiple values
                  2.4.1 Accessing individual characters and substrings
                  Oftenwewanttomanipulatepartofthetextinastring,beitasingleletter,digit,punctuationorspace(a
                  character) or a smaller part of the whole string (a substring). Accessing individual characters simply
                  involves specifying which character you want to access inside square brackets:
                  Bird, Curran, Klein & Loper                      2-4                                       July 9, 2006
The words contained in this file might help you see if this file matches what you are looking for:

...Programming fundamentals and python introduction this chapter provides a non technical overview of will give you the know how to survive next few chapters it contains many working snippets code which should try yourself in fact we insist there is no better way learn about than dive examples hopefully then feel brave enough modify combine them for your own purposes so before are more detailed recommend that consult one introductions listed further reading section below advanced material contained later tutorial pythonthecalculator newbie friendly things allows type directly into interpreter program be running programs want completely comfortable with set off let s start up mar on win copyright credits or license information dependingonyourenvironment theversionnumberandtheblurbatthetopmightbedifferent as longasyouarerunningpython orgreatereverythingwillbene thepythonprompt indicates now waiting input if using through interactive development environment idle see colorized version like br...

no reviews yet
Please Login to review.