dreamsys software

Python Scripting Tutorial

Variables


Variables are an important part of any program or script. A variable is a simple way to refer to a chunk of data in memory that can be modified. A variable in a python script can be assigned any type of value, such as a text string or a number. In python to create a variable, we simply put in our script:

variableName = value

The text "variableName" can be anything you want, as long as it only contains numbers, letters and/or an underscore "_". A variable name also cannot start with a number. After the equal sign you put the value. Let's revise our original script to use a couple of variables as such:

#!/usr/bin/env python
# This is my second script.

hello = 'Hello'
python = 'Python!'

print hello, python

This will give the output "Hello Python!". Notice that we created two variables in this script. One variable "hello" which holds the text "Hello" and a second variable named "python" which holds the text "Python!".

When we use the print keyword this time, we don't include the quotes as we did in the first script. If you included quotes this time, it would print the literal string "hello, python" instead of the values contained in the variables. You will also notice that we used a comma to separate the variables in the print statement. This will put a space between the variables and display them. If you wanted no space between the varaibles, you could use the + operator as such: print hello + python which would print the text "HelloPython!"

This is not a very exciting use of variable I will admit, let's try something more interesting. Let's create a python script that will take a name and age as parameters. Then it will tell you how long it will be until you are 100 years old. In order to access the command line arguments in a python script, you must first use the import keyword and import the sys package. The arguments can then be accessed by the sys.argv variable, which is an array containing the command line arguments.

To access the values in an array, you use the [] operator. For instance, to access the first item in the command line, you would access sys.argv[0]. Which would return the name of the script being run. The arguments passed to the script are stored in the array starting with index 1. Let's make our script take the 2 arguments name and age which will be indexes 1 and 2 respectively. We can assign these values to a named variable and then print them. We will create a 3rd variable called "diff", which will hold the number of years until you will be 100 years old.

One more thing to note, when setting the variable "age", we must first tell python that the incoming sys.argv variable is an integer, so we use the int keyword to typecast the string sys.argv[2] to an int.

#!/usr/bin/env python
# Years till 100
import sys

name = sys.argv[1]
age = int(sys.argv[2])
diff = 100 - age

print 'Hello', name + ', you will be 100 in', diff, 'years!'

Give this program a name of years.py, be sure to give it execute permission and then run it with the following command:

./years.py Joe 25

This will print the text "Hello Joe, you will be 100 in 75 years!"

This script works well, but it does have some problems. For instance, what if you are 102 years old? What if you don't enter a name or an age on the command line? The script will either crash or print nonsense data. In order to fix this script, we will need to make use of the if and else keywords, which are covered in the next section.

Prev (Introduction) | Next (If/Else)


Blog Entries
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6