Beginning Python

14 Feb 2012 15:27

Objectives

By the time you have completed these exercises, you should have:

  1. Installed an application
  2. learned the first bits about the Python language and how to run python programs

Exercise 1

If you have Python version 2 installed, you may want to remove it first. Python 2 is not being updated any more, and although support for Python 3 is still a little patchy, version 3 is clearly the future.

If you don't uninstall version 2, be aware that you will have to either:

  1. Change the link "python" to point to the version 3 python, or
  2. every time you see me give the command "python", instead use the command "python3.2".

Go to [http://python.org/download/] and download version 3 python for your computer. Then install it.
In Windows, that means just download the file somewhere and then execute it. Windows will probably complain about it not being safe, but relax; the authentic Python website is safe.

If you are already using Linux, you should instead use your package installer to install python3.

Exercise 2

To start python, go to the command prompt and type "python".

Go to The Python Tutorial, Section 3 and work your way through it.

We'll go over variables and stuff later but follow it through and you should be able to get some distance into the string stuff before you get totally lost.

Exercise 3

If you are using Linux you will need to install IDLE (for Python 3 remember) from the package installer. On Windows, and probably OS/X, it's already installed.

On Linux you access IDLE from the command-line (the command is "idle".) From Windows it's from the Start menu.

  • Start IDLE
  • File->New to open a new window
  • Write your program; the tutorial should have given you some ideas.
  • File->Save, save the program in "computing/projects/start python".
    It should have the extension ".py", for example "myfirstprogram.py"
  • Run->Run Module

Now modify your program or write a new one. Have fun.

Exercise 4

Shut down IDLE. Go to the command line. cd to "computing/projects/start python".
type
$ python myfirstprogram.py
or whatever else you called it.

Your program should run, in the command line, just like any other application.

No files attached to this page.

Edit


Re: Beginning Python on 17 Feb 2012 18:09

I've got Python 2 and 3 installed. Have left /usr/bin/python pointing to /usr/bin/python2.7, just in case using python3 for everything upsets anything else I use. Will bear in mind that my interpreter is in a different location if future lessons refer to it!

I've had a crack at the tutorial, which went fine. I thought some of the sting slicing was very neat.. and who would have thought you could multiply a string*2 to get stringstring!

The bit that meant nothing to me was the numbers bit. I can follow the tutorial and get the right output but the complex numbers, floating point, etc is totally over my head. Is this because my maths is rubbish?

tim@ubuntu:~/computing/bin$ cat test.py 
#!/usr/bin/python3
import sys
age = sys.argv[1]
float(age)
days = float(age)*365
hours = float(days)*24
mins = float(hours)*60
seconds = float(mins)*60
print ("You are " + age + " years old.")
print ("You have been alive for roughly", days)
print ("Approximate hours =", hours)
print ("Approximate mins =", mins)
print ("Approximate seconds =", seconds)
tim@ubuntu:~/computing/bin$ ./test.py 27
You are 27 years old.
You have been alive for roughly 9855.0
Approximate hours = 236520.0
Approximate mins = 14191200.0
Approximate seconds = 851472000.0

Not sure I needed the float stuff in the end..? Think I was just getting errors due to print syntax differences in Python2/3. (Not that I have experience of 2, but Google did!). Also multiple print lines doesn't seem smart, but I'm out of time!

Thanks for all your hard work on this!

EditAdmin

Re: Beginning Python on 22 Feb 2012 11:33

I've go a load of cardboard boxes full of books that I'm sending to the Philippines. The shipper needs to know the volume of each box in cubic feet. Programming for a purpose!


print ("a program to find the size in cubic ft of a box")
 
h = int (input ("what is the height of the box?: "))
 
# I had real problems with this until I understood that "h" 
# would be saved in the string(inside inverted commas) as a 
# letter and not an integer (strings and variables?)
# So now h = a number value (int) that is inputted after 
# the colon in the string! If that makes sense.
 
l = int (input ("what is the length of the box?: "))
w = int (input ("what is the width of the box?: "))
 
print ("the box has a volume of ", h*l*w/1728 , "cubic feet")
 
# all measurements are inputted to the nearest inch! 
# It doesn't seem to matter if I make the divisor a float 
# or not, the answer is always a float .... ("fractions 
# aren't lost when dividing integers!")
# It works!

It would be nice if:
Every box volume could be added to a list, labeled box 1, 2, 3, etc.
I got a running total of combined volumes.
Inputting exact measurement would automatically "round up or down" to nearest inch.
Will have a go later.

EditAdmin

Re: Beginning Python on 22 Feb 2012 14:42

@Tim: You don't need the float stuff. — they're all integers anyway and if they weren't

days = years*365.2425

it would convert to float when it needed to.

@Russ: take a look at dictionaries.

EditAdmin

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License