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. >>>

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'

Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250