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.
Example–
a = ['apple', 'banana', 'mango']
for i in a:
print(i)
Output-
apple
banana
mango
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.
Iterables
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.
<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.
Example–
n = 5
while n > 0:
n -= 1
print(n)
Output-
4
3
2
1
0
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.
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: {}.
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.
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
Example:
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:
Operator
Name
Example
+
Addition
x + y
–
Subtraction
x – y
*
Multiplication
x * y
/
Division
x / y
%
Modulus
x % y
**
Exponentiation- left operand raised to the power of right
x ** y(x to the power y)
//
Floor division- division that results into whole number adjusted to the left in the number line
x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator
Example
Same As
=
x = 5
x = 5
+=
x += 3
x = x + 3
-=
x -= 3
x = x – 3
*=
x *= 3
x = x * 3
/=
x /= 3
x = x / 3
%=
x %= 3
x = x % 3
//=
x //= 3
x = x // 3
**=
x **= 3
x = x ** 3
&=
x &= 3
x = x & 3
|=
x |= 3
x = x | 3
^=
x ^= 3
x = x ^ 3
>>=
x >>= 3
x = x >> 3
<<=
x <<= 3
x = x << 3
Python Comparison Operators
Comparison operators are used to compare values. It returns either True or False according to the condition.
Operator
Name
Example
>
Greater than
x > y
<
Less than
x < y
==
Equal to
x == y
!=
Not equal to
x != y
>=
Greater than or equal to
x >= y
<=
Less than or equal to
x <= y
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator
Meaning
Example
and
True if both the operands are true
x < 5 and x < 10
or
True if either of the operands is true
x < 5 or x < 4
not
True 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:
Operator
Description
Example
is
Returns True if both variables are the same object
x is y
is not
Returns True if both variables are not the same object
x is not y
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator
Description
Example
in
Returns True if a sequence with the specified value is present in the object
x in y
not in
Returns True if a sequence with the specified value is not present in the object
x not in y
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator
Name
Description
&
AND
Sets each bit to 1 if both bits are 1
|
OR
Sets each bit to 1 if one of two bits is 1
^
XOR
Sets each bit to 1 if only one of two bits is 1
~
NOT
Inverts all the bits
<<
Zero fill left shift
Shift left by pushing zeros in from the right and let the leftmost bits fall off
>>
Signed right shift
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
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:
int, float, complex
Sequence Types:
list, tuple, range
Mapping Type:
dict
Set Types:
set, frozenset
Boolean Type:
bool
Binary Types:
bytes, bytearray, memoryview
In Python, the data type is set when you assign a value to a variable:
Recent Comments