Your First Program in Python

By: Zed A. Shaw Emailed: 1730 times Printed: 2344 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

1 print "Hello World!"
2 print "Hello Again"
3 print "I like typing this."
4 print "This is fun."
5 print 'Yay! Printing.'
6 print "I'd much rather you 'not'."
7 print 'I "said" do not touch this.'

Type the above into a single file named ex1.py. This is important as python works best with files ending in .py.

Warning: Do not type the numbers on the far left of these lines. Those are called “line numbers” and they are used by programmers to talk about what part of a program is wrong. Python will tell you errors related to these line numbers, but you do not type them in.

Then in Terminal run the file 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 something wrong. No, the computer is not wrong.

What You Should See

$ python ex1.py
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.
$

You may see the name of your directory before the $ which is fine, but if your output is not exactly the same, find out why and fix it.

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 file 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 find someone else who’s had that error and you can probably figure out how to fix it.

python Home | All python Tutorials | Latest python Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Zed A. Shaw
Formatted printing in Python
Printing in Python
Strings And Text in Python
Variables And Printing in Python
Variables And Names in Python
Numbers And Math in Python
Comments And Pound Characters in Python
Your First Program in Python
Installing gedit for python programming in Linux
Installing gedit for python programming in Mac OS X

More Tutorials in python
What is the need for Python language?
How to compile python script and create .pyc file?
How to find the current module name in python
How to force rereading of a changed module in python
UnboundLocalError in python
call by reference in python
Callable objects in python
Ternary operator in python
Hexadecimal and Octal integers in python
Convert string to number in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert between tuples and lists in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python

More Latest News
Most Viewed Articles (in python )
How to compile python script and create .pyc file?
Unknown command: 'migrate'
Callable objects in python
Formatted printing in Python
How to find the current module name in python
Installing gedit for python programming in Windows
Convert string to number in python
Remove duplicates from a list in python
goto in Python
Installing gedit for python programming in Linux
Hexadecimal and Octal integers in python
Convert number to string in python
Convert between tuples and lists in python
Schwartzian Transform in python
Your First Program in Python
Most Emailed Articles (in python)
How to find the current module name in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Comments And Pound Characters in Python
Numbers And Math in Python
Printing in Python
Formatted printing in Python
How to compile python script and create .pyc file?
call by reference in python
Callable objects in python
Hexadecimal and Octal integers in python
Convert string to number in python
Installing gedit for python programming in Windows
Installing gedit for python programming in Linux
Your First Program in Python
Variables And Names in Python