To execute the python script copy the blue text from the screen
and paste it into a file. Save the file, for example, under the
name "hello.py". Then type at the Unix prompt:
python hello.py
Explanations
# | indicates a comment; the line is ignored |
a command; prints the string between the quotes |
Exercises
1.1) Add a second print statement to the script from the example. (For example print "How are you?".)
1.2) Make some random changes, for example, delete the "#" before "This program says ...". In most cases you will get an error message. Fix the errors until the script executes again successfully.
1.3) Type just python at the command line.
This starts the interactive mode. You can now type commands. To
leave the interactive mode press the control key followd by
D. Note: in the interactive mode,
you don't need the word print.
Just typing "Hello World" will print it.
Explanations
name | a variable for a "name" |
= | an assignment operator |
raw_input | input a string from the keyboard |
+ | concatenates strings |
, | separates elements in a print statement |
Exercises:
2.1) What is the difference between using "+" and "," in a print statement?
Try it!
2.2) Write a program that
asks two people for their names; stores the names in variables
called name1 and name2; says hello to both of them.
Exercises
3.1) Execute the script. Make sure you understand every line.
3.2) Write a script that asks a user for a number. The script
adds 3 to that number. Then multiplies the result by 2, subtracts 4,
subtracts twice the original number, adds 3, then prints
the result.
chmod u+x program_name
and then execute it by simply typing the program_name.