CS - Python

   _____           ____  __.      .__       .__     __    
  /     \_______  |    |/ _| ____ |__| ____ |  |___/  |_

 /  \ /  \_  __ \ |      <  /    \|  |/ ___\|  |  \   __\

/    Y    \  | \/ |    |  \|   |  \  / /_/  >   Y  \  |  
\____|__  /__|    |____|__ \___|  /__\___  /|___|  /__|  
        \/                \/    \/  /_____/      \/




This is my Python page    

  • What is Python?
  • What is the Python Logo?
  • Which Devices run on Python?
  • What is it mainly used to control?
  • When and why was Python developed?
  • What type of Language is Python?
  • How much does it cost?
  • References (Which sites did you use?)

Below are example of ASCII Art To create your own ASCII ART text click here

     ."".    ."",
     |  |   /  /
     |  |  /  /
     |  | /  /
     |  |/  ;-._ 
     }  ` _/  / ;
     |  /` ) /  /
     | /  /_/\_/\
     |/  /      |
     (  ' \ '-  |
      \    `.  /
       |      |
       |      |


        ^^          |         ^^
          ::          |         ::

 ^^     ::         |         ::     ^^

   ::     ::         |         ::     ::
    ::     ::        |        ::     ::
      ::    ::       |       ::    ::
        ::    ::   _/~\_   ::    ::
          ::   :::/     \:::   ::
            :::::(       ):::::
                  \ ___ /
             :::::/`   `\:::::
           ::    ::\o o/::    ::
         ::     ::  :":  ::     ::
       ::      ::   ` `   ::      ::
      ::      ::           ::      ::
     ::      ::             ::      ::
   ^^      ::             ::      ^^
             ::             ::
             ^^             ^^


(
       (_)
       ###       .
       (#c    __\|/__
        #\     wWWWw
        \ \-. (/. .\)
        /\ /`\/\   /\
        |\/   \_) (_|
        `\.' ; ; `' ;`\
          `\;  ;    .  ;/\
            `\;    ;  ;|  \
             ;   .'  ' ;  /
             |_.'   ;  | /)
             (     ''._;/`
             |    ' . ;
             |.-'   .:)
             |        |
             (  .'  : |
             |,-  .:: |
             | ,-'  .;|
            _/___,_.:_\_
           [I_I_I_I_I_I_]
           | __________ |
           | || |  | || |
          _| ||_|__|_|| |_
         /=--------------=\
        /                  \
       |                    |

       
To try out your Python skills click here

Python Dictionary Here

I spent the afternoon teaching my 14-year-old niece Kim about programming in Python. She hadn't done any programming before, but was very interested, so I figured we could just give it a shot. I didn't have a text to follow, and hadn't known she was interested, so I hadn't prepared anything.


But the path we took seemed to work, so here's what I remember of it, in rough outline form. As you look at the code samples, remember the goal here isn't to use the best or most Pythonic way to write the code, but to use simple tools to teach beginning programming.
The text below is not for the teacher to read to the student, or for the student to read on their own. This is just of list of topics we discussed, and programs we wrote.
•    •    •
What is a computer program? Instructions for a computer to follow. Computers are really dumb, so you have to spell out everything precisely.
Install Python (IDLE doesn't work well on a Mac, we ended up with plain-old textedit and a terminal.)
Try some arithmetic in the interactive prompt. Assign values to names:
>>> 2 + 2
4
>>> 10 * 3 + 5 * 3
45
>>> x = 10
>>> 2 * x
20
>>> x = x+1
>>> x
11
>>> 2 * x
22
"x = 1" in Python is not like "x = 1" in algebra. You can't say, "x + 1 = 2" in Python, and you can't say "x = x + 1" in algebra.
Create and run a simple program:
print "Hello, world!"
You can ask for input from the user:
name = raw_input("What's your name? ")
print "Hi", name
If statements let you make decisions:
name = raw_input("What's your name? ")
print "Hi", name
age = int(raw_input("How old are you? "))
if age > 25:
    print "Wow, you are old"
An else clause lets you go either way:
name = raw_input("What's your name? ")
print "Hi", name
age = int(raw_input("How old are you? "))
if age > 25:
    print "Wow, you are old"
else:
    print "Don't worry, you're still cool"
An elif clause lets you keep trying different conditions:
name = raw_input("What's your name? ")
print "Hi", name
age = int(raw_input("How old are you? "))
if age > 25:
    print "Wow, you are old"
elif age < 6:
    print "You're just a baby!"
else:
    print "Don't worry, you're still cool"
We've used two kinds of values: numbers and text, called strings. You have to be clear which kind you have. 12 != "12".
Loops: when you want to do something over and over, you can use a while loop. It checks a condition and if the condition is true, runs the statements, then checks the condition again etc, over and over, until the condition is false.
count = 10
while count > 0:
    print "Count:", count
    count = count - 1
print "Blastoff!"
Also, we can write "count = count - 1" as "count -= 1"
Play around with that first number. Computers can do a lot of boring stuff really quickly.
Let's write a program to add up all the numbers up to 1000. You have to think like a computer, which means you can't just say, "Add them all up," you need to break it down into smaller steps.
total = 0
num = 0
while num <= 1000:
    total += num
    num += 1
print "total is", total
Now let's add a twist: let's only include the number in the sum if the number is divisible by 3 or by 5. To see if a number is divisible by 3, you can test if N % 3 == 0. "%" gives you the remainder after dividing, and "==" is how you test for equality.
This program is where we start to get opportunities for real logic errors. If they happen, make sure you talk about them and how they happened before moving on.
total = 0
num = 0
while num <= 1000:
    if num % 3 == 0:
        total += num
    elif num % 5 == 0:
        total += num
    num += 1
print "total is", total
Try a new kind of value: lists. In the interactive prompt, try this:
>>> nums = [1, 2, 3]
>>> print nums
[1, 2, 3]
>>> print len(nums)
3
>>> nums.append(17)
>>> print nums
[1, 2, 3, 17]
Lists can hold any kind of value, like text strings:
>>> cats = ["penny", "kiki", "naomi"]
>>> print cats
['penny', 'kiki', 'naomi']
>>> print len(cats)
3
>>> cats.append('arwen')
>>> print cats
['penny', 'kiki', 'naomi', 'arwen']
Let's write a program to catalog cats (Kim has 5 cats). It will ask the user for the name of a cat, and keep asking until the user types nothing, then will show how many cats the user has.
If you want to loop forever, or test a condition that you can't test immediately, you can use "while True". Remember while tests its condition and runs the loop if the condition is true. True is always true. Later, if you want to end the loop, you can use the "break" statement.
cats = []
while True:
    name = raw_input("Tell me the name of a cat (just plain enter to end): ")
    if name == "":
        break
    cats.append(name)
print "You have", len(cats), "cats"
Hmm, it looks kind of funny if you only have one cat, because it says, "You have 1 cats". Fix that.
cats = []
while True:
    name = raw_input("Tell me the name of a cat (just plain enter to end): ")
    if name == "":
        break
    cats.append(name)
if len(cats) == 1:
    print "You have 1 cat"
else:
    print "You have", len(cats), "cats"
or:
# ...
if len(cats) == 1:
    group = "cat"
else:
    group = "cats"
print "You have", len(cats), group
We'll add one more feature to our cat cataloguer: print the names of the cats. There's another way to loop in Python beside "while". A "for" loop will go around the loop once for each element in a list. We can use it to print the names of the cats:
#.. tack this on the end of the cat cataloguer from above..
print "Here are their names:"
for cat in cats:
    print cat
Now we make a mighty leap to madlibs, which isn't that much more complicated, but needs a few more new concepts.
Play in the interactive prompt with building a string by adding strings together:
>>> s = ""
>>> s = s + "Hello"
>>> s += " world"
>>> print s
'Hello world'
>>> s += " I have something"
>>> s += " to say."
>>> print s
'Hello world I have something to say.'
If we have a list of things, we can examine individual elements in the list:
>>> cats = ["penny", "kiki", "naomi"]
>>> print cats[0]
'penny'
>>> print cats[1]
'kiki'
>>> print cats[2]
'naomi'
To do madlibs, the story will be represented as a list of pieces, where each piece can be a literal piece of text in the story, or a slot to be filled in. A piece will be a 2-element list: for text, the first element will be 'text', and the second element will be the actual text to go in the story. For a slot, the first element will be 'slot', and the second element will be the prompt for the user. The madlib will be a list of these little lists:
madlib = [
    ['text', "Once upon a time there was a "],
    ['slot', "an adjective"],
    ['text', ", "],
    ['slot', "another adjective"],
    ['text', " "],
    ['slot', "an animal"],
    ['text', ". He liked to "],
    ['slot', "a verb in the present tense"],
    ['text', " all day. One day, he went to "],
    ['slot', "an adjective"],
    ['text', " "],
    ['slot', "a place"],
    ['text', " to meet "],
    ['slot', "a person"],
    ['text', "."],
    ]
When writing a madlib this way, it's really hard to get the spacing and punctuation right...
The madlib program will scan the madlib. Each 'text' piece will be added to the story we're building. For each 'slot' piece, we'll prompt the user to give us someething to go in the slot, and we'll add their answer to the story. When it's all done, we'll print the story.
madlib = [
    #.. from above ..
    ]

story = ""

for piece in madlib:
    if piece[0] == 'text':
        story += piece[1]
    elif piece[0] == 'slot':
        prompt = "Give me " + piece[1] + ": "
        answer = raw_input(prompt)
        story += answer

print story
•    •    •




No comments:

Post a Comment

Wildern Pupils if you log onto your school email account you can leave a comment via that ID.