Skip to content

Latest commit

 

History

History
148 lines (129 loc) · 4.78 KB

File metadata and controls

148 lines (129 loc) · 4.78 KB

1. Introduction

A power to create anything.

  • Python is a simple programming language
  • It is cross-platform
  • A high level langauge
  • An interpreted language
  • It is extensible

Python language is named after the BBC show “Monty Python’s Flying Circus”

2. Installing Python

Download the latest version for

3. Python Interpreter

Linux/Unix Systems

Usually in Terminal based systems it is installed in path /usr/local/bin/python.Set /usr/local/bin in Linux/Unix shell search path.

On Windows

The Python installation is usually placed in C:\Python.Setting path in Environment variables or set path=%path%;C:\Python36 in CMD DOS will complete the installation.

Type python in terminal shell or command prompt to start working.

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

This is the python interactive shell. It indicates the python version 3.6.2 installed on Windows 32-bit. three greater-than signs (>>>) is the primary prompt.

4. Whitespaces and indentation

Whitespace is used to denote blocks. In other languages curly brackets ({ and }) are common. When you indent, it becomes a child of the previous line. In addition to the indentation, the parent also has a colon following it.

In Python whitespace is an important thing. We divide different identifiers using spaces. Whitespace in the beginning of the line is known as indentation, but if you give wrong indentation it will throw an error. Below are some examples:

>>> a = 12
>>> a = 12
File "<stdin>", line 1
a = 12
^
IndentationError: unexpected indent

So we can have few basic rules ready for spaces and indentation.

    • Use 4 spaces for indentation.
    • Never mix tab and spaces.
    • One blank line between functions.
    • Two blank lines between classes.

There are more places where you should be following the same type of whitespace rules:

    • Add a space after ”,” in dicts, lists, tuples, and argument lists and after ”:” in dicts.        
    • Spaces around assignments and comparisons (except in argument list)        
    • No spaces just inside parentheses.

5. Basic Commands

We use print method to output on console.

>>> print("Hello")
Hello
>>> print('Hello World')
Hello World
>>> print(123+456)
579

Variable creation is easy without declaring the type of variable.

>>> num=1
>>> num
1
>>> a = 2
>>> print('a prints out ', a)
a prints out 2
>>> str="Character & String"
>>> str
'Character & String'

6. Comments

Comments in Python start with the hash character, # , and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash character within a string literal is just a hash character.

>>># This is a comment
>>># The next line will add two numbers
>>>a = 12 + 34
>>>print(c) #this is a comment too :)

7. Modules

Modules are Python files that contain different function definitions or variables that can be reused. Module files should always end with a .py extension. Python itself has a vast module library with the default installation. We will use some of them later. To use a module you have to import it first.

>>> import math
>>> print(math.e)
2.71828182846

8. Basic Functions

Stucture of function defination Syntax would go like this-

>>> def FunctionName(parameter1, parameter2,....):
algorithm statements..
return SomeData

To creat user define functions and import the functions

>>> def defaultArg( name): 
        print name

function call

>>> defaultArg(aniket)

Function call with arguments

>>> def default(name, msg="Hello"):
        print name

Function Call

>>> default(name)

9. Tuples

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example −

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

The empty tuple is written as two parentheses containing nothing −

tup1 = ();

To write a tuple containing a single value you have to include a comma, even though there is only one value −

tup1 = (50,);