Learn Python – NOTES 61 – 70

Program 61 – Exchanging Values

variable_1 = 1
variable_2 = 2
variable_1, variable_2 = variable_2, variable_1

OR

variable_1 = “Good”
variable_2 = “job”
variable_1, variable_2 = variable_2, variable_1
print(variable_1,variable_2)
variable_1, variable_2 = variable_2, variable_1
print(variable_1,variable_2)

FOR LONG LISTS

my_list = [10, 1, 8, 3, 5, 7, 34, 4, 76]
length = len(my_list)
for i in range(length // 2):
my_list[i], my_list[length – i – 1] = my_list[length – i – 1], my_list[i]
print(my_list)

length = len(my_list)
for i in range(length // 2):
my_list[i], my_list[length – i – 1] = my_list[length – i – 1], my_list[i]
print(my_list)

Program 62 – Finding a Value in a List

Now let's find the location of a given element inside a list:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
to_find = 5
found = False
for i in range(len(my_list)):
    found = my_list[i] == to_find
    if found:
        break
if found:
    print("Element found at index", i)
else:
    print("absent")

Program 63 -Is this number prime?

def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

for i in range(1, 61):
if is_prime(i):
print(i)

 

Program 64 –

 

Program 65 –

 

Program 66 –

 

Program 67 –

 

Program 68 –

 

Program 69 –

 

Program 70 –