Python Introduction Workshop: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
Please keep in mind that Python cares about whitespace (spaces, returns, etc...), if you just paste the code in, be aware of what spaces you are also pasting in... ;-) | Please keep in mind that Python cares about whitespace (spaces, returns, etc...), if you just paste the code in, be aware of what spaces you are also pasting in... ;-) | ||
Create a file | |||
$ nano myprog.py | $ nano myprog.py | ||
Add the following line | |||
print 'Hello World!' | print 'Hello World!' | ||
Save the file and exit. Ctrl+O, Ctrl+X | |||
Run the program. | |||
$ python myprog.py | $ python myprog.py | ||
Add a comment to the top of your program. Comments are to help people read your program. | |||
# Program by [username] | # Program by [username] | ||
Create a variable to store input into the program. | |||
# Import adds a library (addition functionality) to your program | # Import adds a library (addition functionality) to your program | ||
# sys is the system library | # sys is the system library | ||
Line 29: | Line 29: | ||
print 'Hello ' + instr + '.' | print 'Hello ' + instr + '.' | ||
Run the program, this time with input. | |||
$ whoami | python myprog.py | $ whoami | python myprog.py | ||
Use the input to alter the output, before your print message add: | |||
if instr == 'jbg': | if instr == 'jbg': | ||
Line 39: | Line 39: | ||
instr = 'Writer' | instr = 'Writer' | ||
Save and run. | |||
It doesn't work! This is because there is actually a return character in the string. Change the following: | |||
instr = sys.stdin.readline().strip() | instr = sys.stdin.readline().strip() | ||
Save and run. | |||
$ whoami | python myprog.py | $ whoami | python myprog.py | ||
$ echo 'Ray Bradbury' | python myprog.py | $ echo 'Ray Bradbury' | python myprog.py | ||
Create a new program (myprog2.py) which loops through all the lines coming from stdin. | |||
import sys | import sys | ||
Line 54: | Line 54: | ||
print line.strip() | print line.strip() | ||
Run it. | |||
$ cat /pub/451.txt | python myprog2.py | $ cat /pub/451.txt | python myprog2.py | ||
That's a lot of lines, how many exactly? | |||
$ cat /pub/451.txt | python myprog2.py | wc -l | $ cat /pub/451.txt | python myprog2.py | wc -l | ||
Simple loop. | |||
for i in range(0, 10): | |||
print i | |||
Let's store those lines in an array. | |||
import sys | import sys | ||
Line 69: | Line 73: | ||
print 'Stored ' + str(len(lines)) + ' lines.' | print 'Stored ' + str(len(lines)) + ' lines.' | ||
451 remixed via a function. Add the following to the top of your program. | |||
def remix(lines): | def remix(lines): | ||
Line 80: | Line 84: | ||
remix(lines) | remix(lines) | ||
Run it. | |||
$ cat /pub/451.txt | python myprog2.py | more | $ cat /pub/451.txt | python myprog2.py | more | ||
Try other splits. | |||
print line[0:10] | print line[0:10] | ||
print line[10:15] + line[0:10] | print line[10:15] + line[0:10] | ||
A more powerful remix. | |||
$ python /pub/shaney.py /pub/451.txt | $ python /pub/shaney.py /pub/451.txt | ||
Scrape the internet into a file. | |||
$ python /pub/scraper.py > internet.txt | $ python /pub/scraper.py > internet.txt | ||
Eat the internet w/ eat.py | |||
import sys | import sys | ||
Line 114: | Line 118: | ||
lines = eat(lines) | lines = eat(lines) | ||
Run it. | |||
$ cat internet.txt | python eat.py | $ cat internet.txt | python eat.py | ||
Revision as of 13:24, 9 February 2016
Workshop
Please keep in mind that Python cares about whitespace (spaces, returns, etc...), if you just paste the code in, be aware of what spaces you are also pasting in... ;-)
Create a file
$ nano myprog.py
Add the following line
print 'Hello World!'
Save the file and exit. Ctrl+O, Ctrl+X
Run the program.
$ python myprog.py
Add a comment to the top of your program. Comments are to help people read your program.
# Program by [username]
Create a variable to store input into the program.
# Import adds a library (addition functionality) to your program # sys is the system library import sys # Read from stdin into a variable called instr instr = sys.stdin.readline() # Print message w/ variable print 'Hello ' + instr + '.'
Run the program, this time with input.
$ whoami | python myprog.py
Use the input to alter the output, before your print message add:
if instr == 'jbg': instr = 'Programmer' else: instr = 'Writer'
Save and run.
It doesn't work! This is because there is actually a return character in the string. Change the following:
instr = sys.stdin.readline().strip()
Save and run.
$ whoami | python myprog.py $ echo 'Ray Bradbury' | python myprog.py
Create a new program (myprog2.py) which loops through all the lines coming from stdin.
import sys for line in sys.stdin: print line.strip()
Run it.
$ cat /pub/451.txt | python myprog2.py
That's a lot of lines, how many exactly?
$ cat /pub/451.txt | python myprog2.py | wc -l
Simple loop.
for i in range(0, 10): print i
Let's store those lines in an array.
import sys lines = [] for line in sys.stdin: lines.append(line.strip()) print 'Stored ' + str(len(lines)) + ' lines.'
451 remixed via a function. Add the following to the top of your program.
def remix(lines): lines = reversed(lines) for line in lines: print line[::-1] # This is extended slice syntax. It works by doing [begin:end:step]
Add the following to the end of your program.
remix(lines)
Run it.
$ cat /pub/451.txt | python myprog2.py | more
Try other splits.
print line[0:10] print line[10:15] + line[0:10]
A more powerful remix.
$ python /pub/shaney.py /pub/451.txt
Scrape the internet into a file.
$ python /pub/scraper.py > internet.txt
Eat the internet w/ eat.py
import sys def eat(lines): new_lines = [] for line in lines: if len(line) > 0: new_line = line[0:len(line) - 1] print new_line new_lines.append(new_line) return new_lines lines = [] for line in sys.stdin: lines.append(line.strip()) while len(lines) > 0: lines = eat(lines)
Run it.
$ cat internet.txt | python eat.py
Why is it called Python?
When he began implementing Python, Guido van Rossum was also reading the published scripts from “Monty Python’s Flying Circus”, a BBC comedy series from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.