Python Tutorial : Part 1 - Introduction

Python Tutorial for beginner: Introduction :

What is Python?

Python is an easy to learn, interactive, object-oriented and open-sourced (under GPL license) programming language. It was created by Guido van Rossum during 1985- 1990.

Installation :

Many UNIX and LINUX distribution already include a recent version of python. You can open one terminal and type “python” to check if it is installed or not. Latest versions of Python 3 can be downloaded from https://www.python.org/downloads/. (In this tutorial we are going to use python 3.x as most of the Linux and Mac OS are currently using python 3.x as default). Download and Install Python 3.x from the above link on your system.

Basics :

We can use Interactive mode or Script Mode in python programming :

a) Interactive Mode :

Type “**python3_ _” on Terminal and Hit enter. Next type “**print (“Hello World__”) , you will get the outputs like the following :

$ python3
Python 3.7.5 (default, Nov  1 2019, 02:16:23)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World !!")
Hello World !!
>>>

b)Script Mode Programming:

Create one file test_python.py and write the following :

print("Hello, Python!")

Next, you can run this script from the terminal like “python3 test_python.py”.

The output will be :

Hello, Python !

Python Keywords :

Keywords are lowercase reserved words that cannot be used as variable, constants or as anything. Following are the list of keywords currently python 3.3 contains : else, import , pass, assert , raise, in , except, break, yield, or, if, elif, as, with, not , global, del, and, while, nonlocal, from, def, True, try, lambda, for,continue, None, class, finally, is , False and return.

Python Identifiers :

The identifier is a name used to identify class, variable, functions etc. Following rules should be followed while creating an identifier :

  1. The identifier is a combination of lowercase (a to z), uppercase (A to Z) , digits (0 to 9 ) and underscore ( _). You can create an identifier with any combinations but remember: it should not start with a digit.

  2. Only class name starts with an uppercase letter.

  3. Python Keywords we have seen above cannot be used as an identifier.

  4. If an identifier has :

  • One leading underscore: private identifier
  • Two leading underscore: strongly private identifier
  • Two trailing underscore: special name

Similar tutorials :

Indentation :

Unlike C or Java, we cannot use braces to indicate code blocks in python. In python, code blocks are defined by their indentation. No. of indentation may be different, but it should be consistent throughout that block.You cannot use one tab for the first line and four whitespaces for the second line.

Statements :

In python, instructions that the python interpreter can execute are known as statements. e.g. _count =10 _is an assignment statement. If a newline character is received, it is considered as the end of a statement.

e.g. count = 1+2+3+4+5

Multi-Line statements :

Can we write the above statement in multi-lines? The answer is yes.

Explicit line continuation :

Using line continuation character ( \ )

Count = 1+\
              2+\
              3+\
              4+\
              +5

Implicit line continuation :

Statements inside {},() or [.] are not required to use the line continuation character.

Count = [ 1+
                2+3+4+
                5]

We can also put multiple statements on a single line using a semicolon :

Count =1 ; var1 = 2; var2 = 3

Comments in python :

Comments are really important in any programming language. In python, **hash symbol (#)_ _is used to start a comment. For multi-line comments, you can use hash on each line or triple quotes **( ‘ ‘ ‘ or “ ” ” ).__

#this is a comment
Count = 10 #this is also a comment

“”” This is a
Multi line comment “”"

You might also like :