Python Dark Programming - First Hello World Program - Dark World Dimension

From the whole other world to earthlings there, The gate of our universe will lay here!

Dark-World ||

  • Ready
  • Responsive
  • Fast Loading
  • Amazing!

Post Top Ad

Wednesday, August 9, 2017

Python Dark Programming - First Hello World Program

Your Ad Spot
Remember, you should have spent a good amount of time in Exercise 0, learning how to install
a text editor, run the text editor, run the Terminal, and work with both of them. If you haven’t
done that, then do not go on. You will not have a good time. This is the only time I’ll start an
exercise with a warning that you should not skip or get ahead of yourself.
Type the following text into a single fi le named ex1.py. This is important, as Python works best
with files ending in .py.
ex1.py
  1. print "Hello World!"
  1. print "Hello Again"
  1. print "I like typing this."
  1. print "This is fun."
  1. print 'Yay! Printing.'
  1. print "I'd much rather you 'not'."
  1. print 'I "said" do not touch this.'

If you are on Mac OSX, then this is what your text editor might look like if you use TextWrangler:
1




















If you are on Windows using Notepad++, then this is what it would look like:

1















Don’t worry if your editor doesn’t look exactly the same; the key points are as follows:
1. Notice I did not type the line numbers on the left. Those are printed in the tutorial so I can
talk about specific lines by saying, “See line 5 . . .” You do not type those into Python scripts.
2. Notice I have the print at the beginning of the line and how it looks exactly the same
as what I have above. Exactly means exactly, not kind of sort of the same. Every single
character has to match for it to work. But the colors are all different. Color doesn’t mat-
ter; only the characters you type.
Then in Terminal, run the fi le by typing:
python ex1.py
If you did it right, then you should see the same output I have below. If not, you have done some-
thing wrong. No, the computer is not wrong.


What You Should See

On Max OSX in the Terminal, you should see this:
1
























On Windows in PowerShell, you should see this:
1

You may see different names, the name of your computer or other things, before the python ex1.py,
but the important part is that you type the command and see the output is the same as mine.
If you have an error, it will look like this:
$ python ex/ex1.py
File "ex/ex1.py", line 3
print "I like typing this.
^
SyntaxError: EOL while scanning string literal
It’s important that you can read these, since you will be making many of these mistakes. Even I
make many of these mistakes. Let’s look at this line by line.
1. Here we ran our command in the Terminal to run the ex1.py script.
2. Python then tells us that the fi le ex1.py has an error on line 3.
3. It then prints this line for us.
4. Then it puts a ^ (caret) character to point at where the problem is. Notice the missing "
(double- quote) character?
5. Finally, it prints out a SyntaxError and tells us something about what might be the error.
Usually these are very cryptic, but if you copy that text into a search engine, you will fi nd
someone else who’s had that error and you can probably fi gure out how to fi x it.
WARNING!
If you are from another country and you get errors about ASCII encodings,
then put this at the top of your Python scripts:
# - *- coding: utf- 8 - *-
It will fix them so that you can use Unicode UTF- 8 in your scripts without a problem.

Study Drills

Each exercise also contains Study Drills. The Study Drills contain things you should try to do. If you
can’t, skip it and come back later.
For this exercise, try these things:
1. Make your script print another line.
2. Make your script print only one of the lines.
3. Put a “#” (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.

From now on, I won’t explain how each exercise works unless an exercise is different.
NOTE:
An “octothorpe” is also called a “pound,” “hash,” “mesh,” or any number of
names. Pick the one that makes you chill out.

Common Student Questions

These are actual questions by real students in the comments section of the tutorial when it was
online. You may run into some of these, so I’ve collected and answered them for you.

Can I use IDLE?

No, you should use Terminal on OSX and PowerShell on Windows, just like I have here. If you don’t
know how to use those, then you can go read the Command Line Crash Course in the appendix.

How do you get colors in your editor?

Save your file first as a .py file, such as ex1.py. Then you’ll have color when you type.

I get SyntaxError: invalid syntax when I run ex1.py.

You are probably trying to run Python, then trying to type Python again. Close your Terminal, start
it again, and right away type only python ex1.py.

I get can't open file 'ex1.py': [Errno 2] No such file or directory.

You need to be in the same directory as the file you created. Make sure you use the cd command to
go there fi rst. For example, if you saved your fi le in lpthw/ex1.py, then you would do cd lpthw/
before trying to run python ex1.py. If you don’t know what any of that means, then go through
the Command Line Crash Course (CLI- CC) mentioned in the fi rst question.

How do I get my country’s language characters into my file?

Make sure you type this at the top of your file: # - *- coding: utf- 8 - *- .

My file doesn’t run; I just get the prompt back with no output.

You most likely took the previous code literally and thought that print "Hello World!" meant
to literally print just "Hello World!" into the fi le, without the print. Your fi le has to be exactly
like mine in the previous code and all the screenshots; I have print "Hello World!" and print
before every line. Make sure your code is like mine and it should work.

Post Top Ad