# Python for Beginners 1: Basic Simple Scripts

If you are just starting out in programming, python is a good language to start out with. Once you understand the basic concept of object oriented programming, they (programming languages) are all basically the same. Most programming languages are very similar, just a few differences in the syntax. I have put together a few small, simple basic scripts to help you get the hang of it.

First I will show a simple script for Celsius to Fahrenheit conversion.

I like to start with a blank line to make it easier to read when running the script in command line, but it’s not necessary.

` Print '' `

In this script I start out with a variable called “cel” to represent Celsius. Notice I used a float type for the input. You could use “int” for integer, however, if the user enters anything but a whole number, it would not work correctly.

` cel = float(input('Enter a temprature in Celsius to convert to Fehrenheit: ')) `

fah is the variable for Fahrenheit. This is where the formula is to convert Celsius to Fahrenheit.

` fah = ((9*cel)/5)+32 `

Again, I like adding spaces between lines in the results that show on the command line interface.

` print '' `

This will print the Celsius temperature that the user initially entered “cel”, then “Celsius is equal to” then the temperature in Fahrenheit. Notice also the format statement that creates one decimal place.

``` 'print cel, 'degrees Celsius is equal to ',\
format(fah, '.1f'), ' degrees Fahrenheit'' ```

The result should look something like this:

Enter a temperature in Celsius to convert to Fahrenheit: 10

10 degrees Celsius is equal to 50.0 degrees Fahrenheit

This is another example of a simple, straight forward script. Sales tax calculation

` print '' `

Use a float type when dealing with decimals. Declare a variable for the input. Instruct the user to enter a price.

` item = float(input('Enter the purchase price: \$')) `

Here we are assuming that the state sales tax is 5% or .05. statetax is the variable declared to hold the calculation for the state sales tax. Remember, item is the name that was given to the user’s input price.

` statetax = (item*.05) `

The following line calculates the county tax at 2.5% or .025.

` cntytax = (item*.025) `

All of the calculations are basically the same. You have to declare a variable then show the calculation the variable represents.

` ttltax = (statetax+cntytax) `
` total = (item+ttltax) `

This will add a blank line before the actual printout on the screen.

` print '' `

Be sure to add the format function to control the decimal places when using float numbers

``` print 'Purchase price \$', \
format(item, '.2f') ```

print 'State Tax \$', \

`format(statetax, '.2f') </pre>`
``` print 'County Tax \$', \
format(cntytax, '.2f') ```
``` print 'Total Sales Tax \$', \
format(ttltax, '.2f') ```
``` print 'Total \$', \
format(total, '.2f') ```

The program should print something like the following:

Enter the purchase price: \$1.99

Purchase price \$1.99

State Tax \$0.10

County Tax \$0.05

Total Sales Tax \$0.15

Total \$2.14

Whether you are learning Python for a class or for yourself, I hope you find my lessons helpful.

You can view my other articles at sharkness

Category: Programming