Python Introduction Workshop: Difference between revisions

From Hackers & Designers
No edit summary
No edit summary
 
(22 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Article
|MainNavigation=No
}}
[[File:Python meme.jpg|350px|]]


== Workshop ==
== Workshop ==


1.) Create a file
https://summer.hackersanddesigners.nl/451.txt
 
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... ;-)
 
First alias python3 so you don't go nuts!  Server by default uses python 2, but for this workshop we will use python 3.
 
$ alias python=python3
 
Create a file
  $ nano myprog.py
  $ nano myprog.py


2.) Add the following line
Add the following line
  print 'Hello World!'
  print('Hello World!')


3.) Save the file and exit.  Ctrl+O, Ctrl+X
Save the file and exit.  Ctrl+O, Ctrl+X


4.) Run the program.
Run the program.
  $ python myprog.py
  $ python3 myprog.py


5.) Add a comment to your program.  Comments are to help people read your program.
Add a comment to the top of your program.  Comments are to help people read your program.
  # Program by [username]
  # Program by [username]


6.) Create a variable to store input into the program.
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
  import sys
  import sys
  # Read from stdin into a variable called instr
  # Read from stdin into a variable called instr
  instr = sys.stdin.readline()
  instr = sys.stdin.readline()
  # Print message w/ variable
  # Print message w/ variable
  print 'Hello ' + instr + '.'
  print('Hello ' + instr)


7.) Run the program, this time with input.
Run the program, this time with input.
  $ whoami | python myprog.py
  $ whoami | python myprog.py


8.) Treat yourself well!  Before your print message add:
Use the input to alter the output, before your print message add:


  if instr == 'jbg':
  if instr == 'jbg':
Line 35: Line 49:
   instr = 'Writer'
   instr = 'Writer'


9.) Save and run.
Save and run.


10.) It doesn't work!  This is because there is actually a return character in the string.  Change the following:
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()


11.) Save and run.
Save and run.
  $ whoami | python myprog.py
  $ whoami | python myprog.py
  $ echo 'Ray Bradbury' | python myprog.py
  $ echo 'Ray Bradbury' | python myprog.py


12.) Create a new program (myprog2.py) which loops through all the lines coming from stdin.  
Create a new program (myprog2.py) which loops through all the lines coming from stdin.  
   import sys
   import sys
 
   for line in sys.stdin:
   for line in sys.stdin:
     print line
     print(line.strip())


13.) Run it.
Run it.
  $ cat /pub/451.txt | python myprog2.py
  $ cat /pub/451.txt | python myprog2.py


14.) That's a lot of lines, how many exactly?
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


Simplest loop.
while True:
  print("hello.")


Simple loop.
for i in range(0, 10):
  print("hello")


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


Run it.
$ cat /pub/451.txt | python myprog2.py


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.


What to talk about...
$ cat /pub/451.txt | python myprog2.py | more


* Variables (integers, arrays)
Try other splits.
* Conditionals (if/elif/else)
print(line[0:10])
* Loops (for, while)
print(line[10:15] + line[0:10])
* Functions (def function_name(args):)


----
Why is it called Python?


* Exception handling? (try/catch)
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.
* File I/O...reading and writing from files:


f = open('handd-book.wiki', 'w') # write
[[Category:Tools]]
img_file = open(filename, 'wb') # i think append?
f = open('handd-book.wiki', 'r') # read

Latest revision as of 19:47, 5 January 2020

MainNavigation No

Python meme.jpg

Workshop

https://summer.hackersanddesigners.nl/451.txt

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

First alias python3 so you don't go nuts! Server by default uses python 2, but for this workshop we will use python 3.

$ alias python=python3

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.

$ python3 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

Simplest loop.

while True:
  print("hello.")

Simple loop.

for i in range(0, 10):
  print("hello")

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

Run it.

$ cat /pub/451.txt | python myprog2.py

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])

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.