Python Introduction #2
Results 1 to 5 of 5

Thread: Python Introduction #2

  1. #1
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914

    Python Introduction #2

    Python Introduction #2

    Hey Hey Everyone,


    Here's another one of these.... I'm not sure how long this will be but I'll attempt a few more tricks and such in it.

    This tutorial will assume you have already read my first Python Tutorial (Python Introduction).


    I'm not real sure yet what I'm going to cover, I figure I'll pick at these for a couple of days and post it at the end of the weekend. Unless I get really ambitious tonight.


    Before I get into some examples, I'm going to include a few charts

    Source: Python Reference Manual - 2.4.1 String Literals

    Escape Sequences

    \newline Ignored
    \\ Backslash (\)
    \' Single quote (')
    \" Double quote (")
    \a ASCII Bell (BEL)
    \b ASCII Backspace (BS)
    \f ASCII Formfeed (FF)
    \n ASCII Linefeed (LF)
    \N{name} Character named name in the Unicode database (Unicode only)
    \r ASCII Carriage Return (CR)
    \t ASCII Horizontal Tab (TAB)
    \uxxxx Character with 16-bit hex value xxxx (Unicode only)
    \Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)
    \v ASCII Vertical Tab (VT)
    \ooo ASCII character with octal value ooo
    \xhh ASCII character with hex value hh

    Python Operators

    + addition
    - subtraction
    * multiplication
    / division
    % module
    ** To the Power Of
    & Bitwise And
    | Bitwise Or
    == equals
    != Does not Equal
    <> Does not Equal (Outdated)
    < Less Than
    > Greater Than
    <= Less Than or Equal To
    >= Greater Than or Equal To
    not Logical Negation
    and Logical And
    is, is not, in, not in Comparison

    Now for some examples..


    Let's start with the while statement.

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    from random import *
    answer = randint(1,25)
    guess = 0
    counter = 0
    print "Welcome to the Number Guessing Game"
    while guess != answer :
            guess = input("Please Guess a Number between 1 and 25: ")
            if guess < answer :
                    print "To low."
            elif guess > answer :
                    print "To high."
            counter = counter + 1
    print "Congrats you guessed the correct number (",answer,") in",counter,"tries."
    3. Save the script as numberguess.py
    4. Open a command prompt and type python numberguess.py
    5. You will see Welcome to the Number Guessing Game, followed by a new line which states Please Guess a Number between 1 and 25. I can't walk you though this, since it uses a random number but as you guess you will see To low. or To high. Until you guess the correct number, when it will then display Congrats you guessed the correct number ( <number> ) in <tries> tries.

    This program incorporates a lot of firsts. We've made use of the Random module which allows us to call the command randint(bottom number, top number) to randomly generate an integer. We've created a few additional variables to store values and entered into a While Statement. This is also the first time we've looked at white space. Python makes use of white space quite frequently. As you notice there is no End while or End if statements, instead the tabs (white space) tell the interpreter where each statement ends.
    The while statement follows the format while <condition> : <code>.

    This next example will be relatively small as I just want to introduce the for statement and nothing else.

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    print "This program will list the first 10 powers of a number."
    usednum = input("Enter the number to use: ")
    for x in range(10) :
            print usednum, "^", x, "=", usednum ** x
    3. Save the script as powers.py
    4. Open a command prompt and type python powers.py
    5. You will see This program will list the first 10 powers of a number.. Followed by Enter the number to use: . For this example, enter the number 2 and press enter. You will then see.

    2 ^ 0 = 1
    2 ^ 1 = 2
    2 ^ 2 = 4
    2 ^ 3 = 8
    2 ^ 4 = 16
    2 ^ 5 = 32
    2 ^ 6 = 64
    2 ^ 7 = 128
    2 ^ 8 = 256
    2 ^ 9 = 512


    This for statement uses and undeclared variable (which initializes to 0) and then cycles threw 10 times, leaving us with a last value of 9. Each time threw the cycle it prints the resulting power.


    Many of you who have used C/C++ in the past have probably noticed a few similarities and this is because they exist. I have already shown you the print statement, but I will now show you another way to accomplish the same thing that is closer to the way C/C++ works.

    ********************
    Step-by-Step Process
    ********************
    1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
    2. Type
    Code:
    name = raw_input("What is your name: ")
    print "Your name is %s?" % name
    3. Save the script as print.py
    4. Open a command prompt and type python print.py
    5. You will see a prompt which reads What is your name: . After Entering your name and pressing enter. You will see Your Name is <name>?.

    This is a great method for joining text and variables because it doesn't add a single space like the comma did in past print statements.

    That's all for this tut. people. Stay tuned for the next edition which will focus on executing system files, reading and writing text files and error handling.


    Peace

    HT

  2. #2
    Dead Man Walking
    Join Date
    Jan 2003
    Posts
    810
    wow HTRegz nice tutorials man. To bad you trojan'd them from my machine hehehehe. just kidding. I never really messed with python before i might take a closer look at it now. Thanx again

  3. #3
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    Definately look into it dude.... Tutorial 3 is under way right now.. I just don't want to post them all as a single tutorial because it'd be way too long. Today is actually my 10 day anniversary of using python heh.... It's consumed me... I'm completely awestruck by the simplicity, beauty and power of the language. I dunno the deadline for the next newsletter, but I may try to do a write-up for it on Python. Maybe code the same thing in Python and Perl and do a comparison.

  4. #4
    Junior Member
    Join Date
    Oct 2005
    Posts
    1

    Number Guessing Game

    HTRegz,

    Any idea if there is a way to make the computer guess a number you are thinking of and you tell the computer "too high" or "too low" and the number of tries?

  5. #5
    Senior Member
    Join Date
    Oct 2005
    Posts
    197
    Nice tut HTRegz! I cant wait to read more, like Python Introduction #randint(1,25)
    meh. -ech0.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •