As with all programming languages (and languages in general), practice makes perfect. These exercises are meant for you to practice your programming skills.
This first exercise is just meant to get you a little bit familiar with Python and with programming in general.
Note: all Python programs should be saved with
the
Python Installation and First Steps
IDLE vs. editors
Dawson works with an IDLE (Integrated Development Environment). This is not quite what we did in class. You can either work with IDLE as Dawson describes, or work with an editor and call up Python from the shell/terminal. The following exercises assume you are working with an editor and call Python up from the shell/terminal while working with MacOSX.
Calling up Python in the Shell
Python 3.4.3 (default, Jul 1 2015, 14:12:32) [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
print("Hello World!")
exit()
quit()or CTRL-d (Control button together with "d"). This is in fact a general strategy for exiting Unix programs. If you are ever stuck in a Unix shell, try CTRL-c or CTRL-d.
Python 2 vs. Python 3
There has been a period of change-over between Python
Versions. Most of the material found on-line (and the new books)
assume Python 3. In this course, we will use Python 3, but we will be confronted
by Python 2 every now and then. In fact, there are only a few major
differences between the two and they are not hard to remember.
Differences between Python 2 and Python 3 | |
---|---|
Python 2 | Python 3 |
print "Hello World" | print ("Hello World") |
raw_input("\nPlease enter something:\n") | input("\nPlease enter something:\n") |
Creating and Saving a Python Program: this is best done by using editors that work well for text/utf-8. Examples are emacs, Aquaemacs, vi, TextEdit, Notepad, Kommodo, Textwrangler). Another option are so-called IDE (integrated development environment). Students in the past have found that they prefer Kommodo.
Now write the following lines of code into an editor of your choice and save the program in a file called world.py.
A First Program
#world.py #simple first program for Python 3 print("Hello World!") #Output |
Exercise 1.1. Copy or type the program into an editor, save it and try it out. |
Exercise 1.2. Add more print commands and try out the new version of the program. |
You can run programs you have written and saved in a number of ways.
cd my-programs
python printing.py
Dawson Exercises
Do the "Challenges" at the end of Dawson's Chapter 1.
Pretty Printing
Now let's move on to some pretty printing. The aim is to get a nicely
formatted list with names and office information. Like this:
Name Place ----------- ------- Miriam Butt G220 MacRoom G209 Common Room G119
Exercise 1.3. Type in or copy the program below into an editor, save it and try it out. |
Exercise 1.4. What do the comma, the "\n" and the "\t" do? (\n generally stands for "newline") |
Exercise 1.5. Now write a program that produces the above list of names with places, all nicely formatted. |
Printing
#printing.py print("Miriam") print("Yassin") print("Miriam"), print("Yassin") print("Miriam "), print("Yassin") print("Lexi\t"), print("Levi") print("Lexi\tLevi") print("Miriam\nYassin") |