Introduction to Python

Introduction to Python

Python is an interactive programming language. It was developed by Guido Van Rossum in 1991 at National Research Institute for Mathematics and Computer Science in Netherlands. He named Python by getting inspired from his favourite comedy show Monty Python's Flying Circle. Python is used in various application areas such as web, gaming, scientific and numeric computing, text processing and network programming.

IDLE - Integrated Development and Learning Environment. Python IDLE comprises of Python Shell and Python Editor. While Python Shell is an interactive interpreter, Python Editor allows us to work in script mode(all the instructions necessary for a task are stored in a file often called a source file script, program, source code, or a module and such a file must have extension .py or .pyw).

In python, all forms of data are called objects or values.

Arithmetic Operators

Arithmetic Operators are used to perform mathematical operations such as addition, subtraction, multiplication, division.

Python uses + as addition operator, - as subtraction operator, * as multiplication operator, / as division operator, // as integer division (however while using //, if any one of the two operands is a real number; the result is a real number), % also known as modulus operator, yields the remainder of the division. ** as exponentiation operator yields raise to the power operation.

  • Left associative operators: +, -, *, /, //, %

  • Right associative operators: **

Precedence of Arithmetic Operators -

Arithmetic_op.jpg

Relational Operators

Relational Operators are used for comparing two expressions (an expression is a valid combination of operators and operands) and yield True or False.

Relational Operators -

  1. 1.jpg
  2. 2.jpg
  3. 3.jpg
  4. 4.jpg
  5. 5.jpg
  6. 6.jpg

A relational operator applied on expressions takes the following form : 7a.jpg Arithmetic Operators have higher precedence than Relational Operators.

When the relational operators are applied to strings, strings are compared left to right, character by character, based on their ASCII codes, also called ASCII values.

Logical Operators

The logical operators not, and and or are applied to logical operands True or False, also called Boolean values, and yield either True or False. The operator not is a unary operator, i.e. it requires only one operand. The expressions not True and not False yield False and True, respectively. An expression involving two Boolean operands and the and operator yields True if both the operands are True, and False otherwise. Similarly, an expression involving two Boolean operands and the or operator yields True if at least one operand is True, and False otherwise.

Precedence of Logical Operators -

8.jpg While evaluating an expression involving an and operator, the second sub-expression is evaluated only if the first sub-expression yields True. For example, in the expression (10 < 5) and ((5 / 0) < 10), since the first sub-expression (10 < 5) yields False, and thus determines the value of the entire expression as False, Python does not evaluate the second sub-expression. This is called short circuit evaluation of boolean expression. However, the expression (10 > 5) and ((5 / 0) < 10) yields an error since the first sub-expression yields True, and Python attempts to evaluate the second sub-expression ((5 / 0) < 10) that yields an error because division by zero is an illegal operation.

Precedence of Operators

9.jpg