First Python Program

In this tutorial, we will write our first python program. Open your terminal / command prompt and type python. You will get something like this:

  1. Python 3.4.0 (default, Mar 22 2014, 22:59:56)
  2. [GCC 4.8.2] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>>

Your output may be different because of version or operating system. If you are using linux or mac and want to run python 3 then type python3 instead of python. But common thing is >>>. You will get this sign before every new command. In this tutorial series whenever you get this sign '>>>', skip it because you will get it already printed in your terminal.

It is a python interpreter which execute code without passing it to the file. We will start from here and also learn how to run program by saving in file because interpreter does not save the code.

Print hello world:

Now write a script to print hello world. Type this command:

  1. >>> print("hello world")

you will get output:

  1. >>> print("hello world")
  2. hello world

NOTE: In python 2, you can also write just print "hello world" for printing.

Try these commands and see result:

  1. >>> print(2)
  2. 2
  3. >>> print(2.4)
  4. 2.4
  5. >>> print("2.4")
  6. 2.4
  7. >>> print("2.4)
  8. File "<stdin>", line 1
  9. print("2.4)
  10. ^
  11. SyntaxError: EOL while scanning string literal

You will notice that line 3 and 5 both are giving same result 2.4 but in face these are different results. These are of different data types. First one is a float and second is a string. A float can be used in mathematics calculation but not string. We will read more about data types in next tutorial. Line 7 is giving an error because of wrong syntax. We have to use " in pair. If we want to print " then we have to escape it ( means we have to tell that we want to print "). We escape characters by writing a back slash before them.

  1. >>> print("\"2.4")
  2. "2.4

Assigning Variables:

Variable are places to put data. Look at these examples:

  1. >>> name = "harish"
  2. >>> print(name)
  3. harish
  4. >>> number = 2
  5. >>> print(number)
  6. 2
  7. >>> a=b=c=2
  8. >>> print(a)
  9. 2
  10. >>> print(b)
  11. 2
  12. >>> print(d)
  13. Traceback (most recent call last):
  14. File "<stdin>", line 1, in <module>
  15. NameError: name 'd' is not defined
  16. >>> print(name)
  17. harish
  18. >>> name = "another name"
  19. >>> print(name)
  20. another name

It is just a preview we will study in detail in future tutorials.

Writing in a file

For writing python code in a file install rich text editors like notpad++ or sublime text. These both are free. Then write your code in a file and save it with an extension .py. If you are using linux or mac then make file executable by changing permissions. Open this file in terminal / command prompt by writing this command.

  1. python 'path to file'

In next tutorial, we will read about Variable Type.

Don't forget to Subscribe for latest Tutorials.

About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this tutorial, we will write our first python program. We will start from python interpreter and also learn how to...
First Python Program
Python is a general purpose, high-level, interpreted, interactive and object-oriented language. Easy to learn, read ...
Introduction to Python
Python has five standard data types: Numbers, Strings, Lists, Tuple and Dictionary.Every data type has its own proper...
Variable and Data Types in Python

Login or Sign up to leave comment.