Python For Loops

 28 Aug   Admin

for <var> in <iterable>:

  is a collection of objects—for example, a list or tuple. The  in the loop body are denoted by indentation, as with all Python control structures, and are executed once for each item in . The loop variable  takes on the value of the next element in  each time through the loop.


 a = ['apple', 'banana', 'mango']
 for i in a:

In this example, <iterable> is the list a, and <var> is the variable i. 
Each time through the loop, i takes on a successive item in a, 
so print() displays the values 'apple''banana', and 'mango', respectively.
 A for loop like this is the Pythonic way to process the items in an iterable.


In Python, iterable means an object can be used in iteration. If an object is iterable, it can be passed to the built-in Python function iter(), which returns something called an iterator. Yes, the terminology gets a bit repetitive. 

Read More

Python While Loops

28 Aug   Admin

while <expr>:

<statement(s)> represents the block to be repeatedly executed, often referred to as the body of the loop. This is denoted with indentation, just as in an if statement.

The controlling expression, <expr>, typically involves one or more variables that are initialized prior to starting the loop and then modified somewhere in the loop body.

When a while loop is encountered, <expr> is first evaluated in Boolean context. If it is true, the loop body is executed. Then <expr> is checked again, and if still true, the body is executed again. This continues until <expr> becomes false, at which point program execution proceeds to the first statement beyond the loop body.


  n = 5
  while n > 0:
     n -= 1



Here’s what’s happening in this example:

  • n is initially 5. The expression in the while statement header on line 2 is n > 0, which is true, so the loop body executes. Inside the loop body on line 3, n is decremented by 1 to 4, and then printed.
  • When the body of the loop has finished, program execution returns to the top of the loop at line 2, and the expression is evaluated again. It is still true, so the body executes again, and 3 is printed.
  • This continues until n becomes 0. At that point, when the expression is tested, it is false, and the loop terminates. Execution would resume at the first statement following the loop body, but there isn’t one in this case.
Read More

Python If….Else

28 Aug  Admin

Python Conditions and If statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in “if statements” and loops.

An “if statement” is written by using the if keyword.


x = 22
y = 160

if y > x:
  print(“y is greater than x”)


y is greater than x


The elif keyword in pythons is used to check “if the previous conditions were not true, then try this condition”.


a = 12
b = 18
if a == b:
 print(“a and b are equal”)

elif b > a:

print(“b is greater than a”)


b is greater than a

Read More

Python Dictionaries

28 Aug  Admin

A dictionary is a collection which is unordered, changeable and indexed. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.


thisdictionarie =    {
  “name”: “rohan”,
  “age”: “23”,
  “year”: 1996


{‘name’: ‘rohan’, ‘age’: ’23’, ‘year’: 1996}

Read More

Python Sets

 12 Apr  Admin

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

 Sets are unordered, so you cannot be sure in which order the items will appear.


theset = {“bus”, “car”, “aeroplane”}


{‘aeroplane’, ‘bus’, ‘car’}

Read More

Python Lists

28 Aug admin

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.


A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

alist = [“america”, “india”, “japan”]

['america', 'india', 'japan']

Access Items

You access the list items by referring to the index number:

alist = [“apple”, “banana”, “cherry”]


Read More

Python Tuples

28 Aug  Admin

A tuple is an immutable sequence of 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 (or round bracket), whereas lists use square brackets.


atuple = (“rabbit”, “lion”, “cow”)


(‘rabbit’, ‘lion’, ‘cow’)

Read More

Python Operators

12 Apr Admin

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.


8+6 = 14

Here, + is the operator that performs addition. 8 and 6 are the operands and 5 is the output of the operation.

There are different type of operators use in python:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

+Additionx + y 
Subtractionx – y 
*Multiplicationx * y 
/Divisionx / y 
%Modulusx % y 
**Exponentiation- left operand raised to the power of rightx ** y(x to the power y) 
//Floor division- division that results into whole number adjusted to the left in the number linex // y 

Python Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame As 
=x = 5x = 5 
+=x += 3x = x + 3 
-=x -= 3x = x – 3 
*=x *= 3x = x * 3 
/=x /= 3x = x / 3 
%=x %= 3x = x % 3 
//=x //= 3x = x // 3 
**=x **= 3x = x ** 3 
&=x &= 3x = x & 3 
|=x |= 3x = x | 3 
^=x ^= 3x = x ^ 3 
>>=x >>= 3x = x >> 3 
<<=x <<= 3x = x << 3

Python Comparison Operators

Comparison operators are used to compare values. It returns either True or False according to the condition.

>Greater thanx > y 
<Less thanx < y 
==Equal tox == y 
!=Not equal tox != y 
>=Greater than or equal tox >= y 
<=Less than or equal tox <= y

Python Logical Operators

Logical operators are used to combine conditional statements:

and True if both the operands are truex < 5 and  x < 10 
orTrue if either of the operands is truex < 5 or x < 4 
notTrue if operand is false (complements the operand)not(x < 5 and x < 10)

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

is Returns True if both variables are the same objectx  is y 
is notReturns True if both variables are not the same objectx is not y 

Python  Membership Operators

Membership operators are used to test if a sequence is presented in an object:

in Returns True if a sequence with the specified value is present in the objectx in y 
not inReturns True if a sequence with the specified value is not present in the objectx not in y

Python  Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
 ^XORSets each bit to 1 if only one of two bits is 1
NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Read More

Python Data Types

28   Admin

Data types are the classification or categorization of data items.Data types represent a kind of value which determines what operations can be performed on that data. Numeric, non-numeric and Boolean (true/false) data are the most used data types.

Python has the following standard or built-in data types:

Text Type:str
Numeric Types:intfloatcomplex
Sequence Types:listtuplerange
Mapping Type:dict
Set Types:setfrozenset
Boolean Type:bool
Binary Types:bytesbytearraymemoryview

In Python, the data type is set when you assign a value to a variable:

ExampleData Type 
x = “Welcome”str 
x = 14int 
x = 21.5float 
x = 4jcomplex 
x = [“monkey”, “dog”, “cow”]list 
x = (“orange”, “mango”, “apple”)tuple 
x = range(8)range 
x = {“name” : “Mohan”, “age” : 27}dict 
x = {“mango”, “apple”, “banana”}set 
x = frozenset({“sparrow”, “parrot”, “hen”}) frozenset 
x = Truebool 
x = b”Welcome”bytes 
x = bytearray(6)bytearray 
x = memoryview(bytes(6))memoryview 
Read More

Python Variables

28Aug   Admin

A variable can have a short name (like x and y). In Python:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

          Example-     mvar = “Mohan”

Read More