AdvancePython_20052024_084621
AdvancePython_20052024_084621
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
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:
Ex.
color = ['red', ‘green’, ‘blue’]
for i in color:
print(i)
Output: red
green
blue
output
*
* *
* * *
* * * *
* * * * *
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
1. Operations on Lists
Example
List_1 = [10,11,12,13]
List_2 = [14,15,16,17]
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
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)
Example
List_1= [10,11,12,13]
List_1.append(14)
Example
List_1 = [10,11,12,13]
List_1.extend([14,15,16])
c) Insert(): This function is used to insert an element in the existing list at a specified
location.
Example
List_1 = [10,11,12,13]
List_1.insert(2,15)
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)
a) count(): Used to count the number of duplicated items available in the defined list.
Example:
List_1 =[10,11,12,13,13,13]
List_2= List_1.count (13)
print (“The result is: “, List_2)
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
>>> len(tup)
>>> tup.count(22)
>>> print(tup.index(45))
>>> print(tup.index(890))
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]