First Program – Python Hello Program

In our previous Chapter we learn how to install Python in windows and MAC. In this chapter we will create our own very first Python3 program. We call it Python Hello Program (well this is what we call our first program in every programming language).

Create First Program

Now open your any text editor I have MAC I will use atom editor and copy past below code in file named “hello.py” .py is extension for python .

print ("Hello World")

Remember like all other programming language python is syntax sensitive, so you need to use exact syntax I am using or else you will get an syntax error.

Running Python Hello Program

Once saved open terminal (CMD in windows and bash in Linux) and run command

python3 hello.py (For MAC/Linux)
python hello.py (For Windows)

This should gives you output as “Hello World”.

Congratulation you have created your very first Python Hello Program.

Adding Comments in code

You can add comments in your code using #, if you put # in front of any line or anywhere in line then anything after that will become comment.

In this code in line 1, 2 and 3 anything after # is comment.

But in line 4 your first # is not comment, why? because it is closed in “” . So if you type # inside of print it will be treated as comment.

 

New Functions we used today:-

print

Print function is used to print any data on screen its basic format is

print(" DATA ")

you can replace <DATA> with any thing you want. (“ is syntax of starting print function and “) as ending.

Some Possible error

For today’s function you can get syntax error only, in case if you get syntax error check syntax of function above and use it properly. i.e.

It is important that you read these error messages. You will be making many of these mistakes. Even I make many of these mistakes. Let’s look at this line by line.

  1. We ran our command in the Terminal to run the hello.py script.
  2. Python tells us that the file hello.py have error at line 1.
  3. It prints this line of code for us to see it.
  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. If you don’t understand this error then 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.

 

Exercise for today:-

Try creating a program with output

 

comments (2)

Leave me comment