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

- 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..

********************
Step-by-Step Process
********************
2. Type
Code:
```from random import *
guess = 0
counter = 0
print "Welcome to the Number Guessing Game"
guess = input("Please Guess a Number between 1 and 25: ")
print "To low."
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
********************
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
********************
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