In this module you can learn
- How to read, process, and output text
- How to read from the keyboard
- How to write to the screen
- How to repeat things
- How to create your own modules
Challenge
Game of instruction
You will be given the drawing of a shape and in group you should write instructions to draw the same shape from scratch The instructions will be given to another group that haven't seen the shape and should draw it following the instructions
Challenge
Questions for getting started (given to small groups on cards)
- Write examples of 4 programs you know
- What do programs do?
- Write a definition of program.
It is a text file that contains Python commands or, in other words, lines of code
- Open a text file, write:
"This is the output of my first program"
- save the file with the name my_print.py and exit
- Open a terminal, go to the directory where you saved
my_print.py
and type at the cursor:python my_print.py
Challenge
> ---Your first Python commands: create pairs
a = 3
print a
>>> a = raw_input("Type a number: ")
Type a number: 3
>>> print a
3
Write a program that reads something from the keyboard and print it to the screen.
See the solution to challenge #2
- We need to “access” an existing input file
- And read its content
Infile = open("insulin.txt")
content = Infile.read()
print content
- A Python module is a text file (with the
.py
extension) that contains (Python) definitions/assignments - Python modules can be accessed from programs using the import statement
insulin = "GIVEQCCTSICSLYQLENYCNFVNQHLCGSHL\
VEALYLVCGERGFFYTPKT"
from insulin import insulin
print insulin
Write a program that reads a sequence from a file and print it to the screen. Run it.
Which command we use?
- We need to “open” a text file in the “writing” mode
- We have to write to it.
from insulin import insulin
outfile = open("my_output.txt", "w")
outfile.write(insulin)
outfile.close()
>Challenge --- >Your first Python commands: create pairs > ----
Calculate DNA base occurrences
Write a program that counts how many times the four bases occur in a DNA sequence. The program should:
- Store the DNA sequence in a variable.
- Count how often each base occurs.
- Write all four numbers to the screen. Test it with a DNA sequence for which you know the result, for instance “AAAACCCGGT”. This approach makes it much easier to discover small program errors.
See the solution to challenge #4
- string variables contain text
print
writes to the screen- you can use functions to do things
- you can enter text with
raw_input()
write()
writes to an open file- for loops repeat commands
- comments starts with
#
or'''