Python Documentation
(For Programmers By Programmers)
What Is Python ?
- It is a recent, general-purpose and high level programming language.
- It is available for free.
- It was created by Guido van Rossum and first released released in 1991.
- He named it after the television comedy show Monty Python's Flying Circus.
- The code can be run on multiple platforms(operating systems).
Installing Python
To install Python, you must first download the installation package of the required version (3.8.2) from the
link given below
https://www.python.org/downloads/. Double click the file to
install it.
Features of Python
- High level programming language .
- Cross platform language .
- Free and open source language .
- Python is a case-sensitive language .
In Python, there are two modes for running code:
- Interactive mode (Python Shell)
- Script mode
Interactive mode –
- It is handy when you just want to execute basic Python commands.
- The “>>>” is the prompt used in the Python interactive mode which indicates that the prompt is
waiting for
us to give a command.
- The result is immediately displayed on the Python shell as soon as you type them and hit the enter key.
Pros and Cons of Script Mode :
The following are the advantages of running your code in script mode:
- It is easy to run large pieces of code.
- Editing your script is easier in script mode.
- Good for both beginners and experts.
The following are the disadvantages of using the script mode:
- Can be tedious when you need to run only a single or a few lines of code.
- You must create and save a file before executing your code.
Key Differences Between Interactive and Script Mode
- In script mode, a file must be created and saved before executing the code to get results. In
interactive mode, the result is returned immediately after pressing the enter key.
- In script mode, you are provided with a direct way of editing your code. This is not possible in
interactive mode.
Variables
A variable is a reserved memory location to store values.
Rules For Naming A Variable
- It should not start with a number but it can be followed by a number. Eg. Sub1, Sub2
- Space, dot and hyphen are not allowed between a variable name. But underscore is allowed. Eg Sub_1
- Variable name is case sensitive. ‘a’ and ‘A’ are two different variables.
Some invalid variables
- 1num - As it is starting with a number
- Num 1 - As there is a space in between
- S.P - As there is dot in between
- F-name - as there is hyphen in between
- Comment- It is used to tell us about the code
- A comment line starts with # (hash)
Operators
-
+ (Addition) - Adds two operands.
- - (Subtraction) - Subtracts two operands.
- * (Multiplication) - Multiplies two operands.
- / (Division) - Divides the first operand by the second.
- // (Floor Division) - Divides the first operand by the second.
- % (Modulus) - Returns the remainder when first operand is divided by the second.
- ** (Exponent) - Returns first raised to power second
Id()
- Python id() function returns the “identity” of the object.
- id() is an inbuilt function in Python.
- An Id is a memory address of each object.
Tokens
The smallest element of a Python program is known as Token.
Some Of The Parts Of Tokens Are As
Follows:
- Keywords
- Identifiers
- Literals
- Operators
Keywords
Keywords are the reserved words of Python that have a special fixed meaning for the interpreter.Commonly used
key words are True, False, break, continue, print, import, continue etc.
Identifiers
Identifiers are names given to the variables, constant, function or module. A keyword cannot be used as an
identifier.
Valid examples of indentifiers- ABC, Num_1, num1
Literals
A fixed numeric or non-numeric value is called a literal.
Examples – 2, -23.6, “MCS” , ‘Class 8’
Operators
A symbol or a word that performs some kind of operation on given values and returns the result.
Examples
are: +, -, **, *
Python is case sensitive
It means you have to take care whether you are naming the variables with capital letters or small letters.