ADVANCE PYTHON
NOTES
Variables:
Variables in Programming act like containers that can store different types of data or any data value
in them, a variable in a python program gives data to the computer for processing.
Eg: a=20
b= 2.5
c= “computer”
Data Types:
Operators :
Iteration:
A loop statement allows us to execute a statement or group of statements multiple times as long as the
condition is true.
Statements:
In Python Iteration (Loops) statements are of three types:
1. While Loop
2. For Loop
3. Nested For Loops
While loop:
The while loop contains a boolean expression and the code inside the loop is
repeatedly executed as long as the boolean expression is true.
The statements that are executed inside while can be a single line of code or a block of
multiple statements.
Syntax:
while(expression):
Statement(s)
Example:
number = 1
while number <= 3:
print(number)
number = number + 1
Output:
1
2
3
Nested While Loop:
Nested while loop simply place a loop structure inside of the block of statements that is repeated by
another loop structure.
Example.
i=1
while i <= 5:
j=1
while j <= 9:
print(j, end=" ")
j=j+1
i=i+1
print()
output:
123456789
123456789
123456789
123456789
123456789
For loop:
For loop is used for repeated execution of a group of statements for the desired number of times.
Syntax: for var in sequence:
A sequence of values assigned to var in each iteration
Holds the value of item
in sequence in each iteration
Ex.
color = ['red', ‘green’, ‘blue’]
for i in color:
print(i)
Output: red
green
blue
Nested For loop:
When one Loop defined within another Loop is called Nested Loops.
Syntax:
for val in sequence:
for val in sequence:
Example:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end=" ")
output
*
* *
* * *
* * * *
* * * * *
Break and continue:
In Python, break and continue statements can alter the flow of a normal loop.
The break and continue statements are used in these cases.
Break:
The break statement terminates the loop containing it and control of the program flows to the
statement immediately after the body of the loop.
Example:
for num in [11, 9, 88, 10, 90, 3, 19]:
print(num)
if(num==88):
print("The number 88 is found")
break
Output:
11
9
88
The number 88 is found
Continue:
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.
Example:
for i in range(10):
if i == 5:
continue
print(i)
output:
0
1
2
3
4
6
7
8
9
List
Lists are mutable or changeable and consist of a sequence of elements.
1. Operations on Lists
a) Joining Lists: Two lists can be added using concatenation operator(+).
Example
List_1 = [10,11,12,13]
List_2 = [14,15,16,17]
List_join = List_1+ List 2
print(“The new list after joining both Lists:”, List_join)
b) Replicating Lists: Like string replication, you can replicate a list as many times as you need.
Example
List_1 = [10,11,12,13]
List_2= List_1*3
print(“List after replication is:”, List_2)
c) Slicing Lists: Slicing refers to the process of extracting a slice from a string.
Example
List_1 = [10,11,12,13]
List_2 = List_1[2:4]
print(“The slicing list is: “, List_2)
List Built-in Functions
a) Append(): This function is used to add a new item to the end of the list.
Example
List_1= [10,11,12,13]
List_1.append(14)
print(“List after adding new element:”, List_1)
b) Extend(): This function is used to add multiple elements in an existing list.
Syntax: Listname.extend([new items])
Example
List_1 = [10,11,12,13]
List_1.extend([14,15,16])
print(“List after adding new set of elements:”, List_1)
c) Insert(): This function is used to insert an element in the existing list at a specified
location.
Syntax: Listname.insert(index_value, new_item)
Example
List_1 = [10,11,12,13]
List_1.insert(2,15)
print(“list after inserting new element at 2nd index:”, List_1)
Deleting Elements from List
a) pop(): This function is used to remove the desired element from the list using index
value as parameter.
Syntax: Listname.pop(index_value)
Example:
List _1 = [40,20,19,13,14,13]
List_ 1.pop(3)
print(“list after popping element from the specified index:” List _1)
b) remove(): Used to remove the first occurrence of the specified item from the list when we have no
idea about the index value.
Syntax: Listname.remove(list_item)
Example:
List_1 = [40,20,19,13,14,13]
List_1.remove(13)
print(“list after removing element 13 from the list:”. List_1)
c) del: Used to remove the individual element, the sub list of elements or delete the list
completely.
Syntax:
del Listname[index_value],
Example :
List_1 = [40,20,19,13,14,13]
del List_ 1[2]
print(“List after deleting element from the desired index:”, List_1)
Some Other List Functions
a) count(): Used to count the number of duplicated items available in the defined list.
Syntax: Listname.count (list_item)]
Example:
List_1 =[10,11,12,13,13,13]
List_2= List_1.count (13)
print (“The result is: “, List_2)
b) len(): It can be used to find the length of an object.
Syntax: len(list_name)
c) index(): Used to search the location or the index value of the element in the defined list.
Syntax: Listname.index (list_item)
Example:
List_1 = [40,20,19,13,13,13]
print (“The index value of element 19 in the list is:”, List1.index (19))
Tuple:
A tuple is a collection of data similar to a Python list. The only difference is that we cannot modify a
tuple once it has been created.
Python tuples are immutable (unchangeable). We cannot add, change, or delete items of a tuple.
If we try to modify a tuple, we will get an error.
Tuple Characteristics
Ordered - They maintain the order of elements.
Immutable - They cannot be changed after creation.
Allow duplicates - They can contain duplicate values.
The len() Function
This function returns the number of elements present in a tuple.
>>>tup = (22, 45, 23, 78, 6.89)
>>> len(tup)
The count() Function
This function will help us to fund the number of times an element is present in the tuple.
>>>tup = (22, 45, 23, 78, 22, 22, 6.89)
>>> tup.count(22)
The index() Function
The tuple index() method helps us to find the index or occurrence of an element in a tuple.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> print(tup.index(45))
>>> print(tup.index(890))
#prints the index of elements 45 and 890
Slicing of Lists and Tuples:
A slice object is used to specify how to slice a sequence. You can specify where to start the slicing,
and where to end. You can also specify the step, which allows you to
Syntax: slice(start:end:step)
Eg-1 :
list = [1, 2, 3, 4, 5]
slice = list[0:3]
print(slice)
output [1,2,3]
Eg-2:
list = [1, 2, 3, 4, 5]
slice = list[-3:]
print(slice)
output- [3,4,5]
Eg-3:
list = [1, 2, 3, 4, 5]
slice = list[::2]
print(slice)
output: [1,3,5]