Python 1st Unit
Python 1st Unit
Every value in Python has a datatype. Since everything is an object in Python programming, data
types are actually classes and variables are instance (object) of these classes.
There are various data types in Python. Some of the important types are listed below.
Python Numbers
Integers, floating point numbers and complex numbers falls under Python numbers category. They
are defined as int, float and complex class in Python.
We can use the type() function to know which class a variable or a value belongs to and the
isinstance() function to check if an object belongs to a particular class.
Python List
Python Tuple
Python Strings
String is sequence of Unicode characters. We can use single quotes or double quotes to represent
strings. st
Python Set
Set is an unordered collection of items. Set is defined by values separated by comma inside braces
{ }. Items in a set are not ordered.
Since, set are unordered collection, indexing has no meaning. Hence the slicing operator [ ] does
not work.
P. Veera Prakash 3|Page
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
We can perform set operations like union, intersection on two sets. Set have unique values. They
eliminate duplicates.
Python Dictionary
We can convert between different data types by using different type conversion functions like int(),
float(), str() etc.
In this article, you'll learn everything about different types of operators in Python, their syntax and
how to use them with examples.
Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.
For example:
>> 2 + 3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the
operation.
Arithmetic Operators
Comparison (Relational) Operators
Logical (Boolean) Operators
Bitwise Operators
Assignment Operators
Special Operators
Arithmetic operators:
Comparison operators are used to compare values. It either returns True or False according to the
condition.
Logical operators:
Bitwise operators:
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit,
hence the name.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Special operators:
Python language offers some special type of operators like the identity operator or the membership
operator.
Identity operators:
is and is not are the identity operators in Python. They are used to check if two values (or variables)
are located on the same part of the memory. Two variables that are equal do not imply that they are
identical.
Membership operators:
P. Veera Prakash 8|Page
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
in and not in are the membership operators in Python. They are used to test whether a value or
variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
Decision making is required when we want to execute a code only if a certain condition is
satisfied.
The if…elif…else statement is used in Python for decision making.
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the test
expression is True.
If the test expression is False, the statement(s) is not executed.
In Python, the body of the if statement is indicated by the indentation. Body starts with an
indentation and the first unindented line marks the end.
Python interprets non-zero values as True. None and 0 are interpreted as False.
P. Veera Prakash 12 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Example of if:
Syntax of if...else
if test expression:
Body of if
else:
Body of else
The if..else statement evaluates test expression and will execute body of if only when test
condition is True.
If the condition is False, body of else is executed. Indentation is used to separate the blocks.
P. Veera Prakash 13 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Example of if..else
Python if...elif...else:
Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, body of else is executed.
Only one block among the several if...elif...else blocks is executed according to the condition.
P. Veera Prakash 14 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
The if block can have only one else block. But it can have multiple elif blocks.
Flowchart of if...elif...else
Example of if...elif...else
In this article, you'll learn to iterate over a sequence of elements using the different
variations of for loop.
P. Veera Prakash 15 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called traversal.
Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from
the rest of the code using indentation.
P. Veera Prakash 16 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
We can generate a sequence of numbers using range() function. range(10) will generate
numbers from 0 to 9 (10 numbers).
We can also define the start, stop and step size as range(start,stop,step size). step size
defaults to 1 if not provided.
This function does not store all the values in memory, it would be inefficient. So it
remembers the start, stop, step size and generates the next number on the go.
To force this function to output all the items, we can use the function list().
We can use the range() function in for loops to iterate through a sequence of numbers. It can be
combined with the len() function to iterate though a sequence using indexing.
Here is an example.
P. Veera Prakash 17 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
A for loop can have an optional else block as well. The else part is executed if the items in
the sequence used in for loop exhausts.
break statement can be used to stop a for loop. In such case, the else part is ignored.
Hence, a for loop's else part runs if no break occurs.
Here, for loop prints items of the list until the loop exhausts. When the for loop exhausts, it
executes the block of code in the else and prints.
Loops are used in programming to repeat a specific block of code. In this article, you will
learn to create a while loop in Python.
The while loop in Python is used to iterate over a block of code as long as the test
expression (condition) is true.
We generally use this loop when we don't know beforehand, the number of times to iterate.
while test_expression:
Body of while
In while loop, test expression is checked first. The body of the loop is entered only if the
test_expression evaluates to True. After one iteration, the test expression is checked again.
This process continues until the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.
Body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.
P. Veera Prakash 18 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Flowchart of while Loop:
P. Veera Prakash 19 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Same as that of for loop, we can have an optional else block with while loop as well.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement.
In such case, the else part is ignored. Hence, a while loop's else part runs if no break occurs
and the condition is false.
Here, we use a counter variable to print the string inside loop three times.
On the forth iteration, the condition in while becomes False. Hence, the else part is executed.
In this article, you will learn to use break and continue statements to alter the flow of a
loop.
In Python, break and continue statements can alter the flow of a normal loop.
Loops iterate over a block of code until test expression is false, but sometimes we wish to
terminate the current iteration or even the whole loop without cheking test expression.
P. Veera Prakash 20 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Syntax of break
break
Flowchart of break:
The working of break statement in for loop and while loop is shown below.
P. Veera Prakash 21 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Example: Python break
In this program, we iterate through the "string" sequence. We check if the letter is "i", upon which
we break from the loop. Hence, we see in our output that all the letters up till "i" gets printed. After
that, the loop terminates.
The continue statement is used to skip the rest of the code inside a loop for the current iteration
only. Loop does not terminate but continues on with the next iteration.
Syntax of Continue
continue
Flowchart of continue -
P. Veera Prakash 22 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
The working of continue statement in for and while loop is shown below.
P. Veera Prakash 23 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
This program is same as the above example except the break statement has been replaced
with continue.
We continue with the loop, if the string is "i", not executing the rest of the block. Hence, we
see in our output that all the letters except "i" gets printed.
Syntax of pass
pass
P. Veera Prakash 24 | P a g e
B.TECH-CSE-IV YEAR II SEM-R13 REGULATION-JNTUA
Suppose we have a loop or a function that is not implemented yet, but we want to implement it in
the future. They cannot have an empty body. The interpreter would complain. So, we use the pass
statement to construct a body that does nothing.
def function(args):
pass
class example:
pass
P. Veera Prakash 25 | P a g e