[go: up one dir, main page]

0% found this document useful (0 votes)
3 views14 pages

Advance machine learning video lecture pdf

The document contains various code snippets demonstrating different programming concepts, including nested loops, conditionals, and user input handling. Each snippet is accompanied by its output, showcasing tasks such as multiplying elements of lists, printing patterns, and calculating sums. The examples cover a range of topics from basic loops to more complex structures like perfect number identification.

Uploaded by

iqra saleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Advance machine learning video lecture pdf

The document contains various code snippets demonstrating different programming concepts, including nested loops, conditionals, and user input handling. Each snippet is accompanied by its output, showcasing tasks such as multiplying elements of lists, printing patterns, and calculating sums. The examples cover a range of topics from basic loops to more complex structures like perfect number identification.

Uploaded by

iqra saleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Advance machine learning

1.This code compares every element of list1 with every


element of list2, and multiplies them only if they're not equal.
list1 = [2,5,6]
list2 = [2,6,5]
result = []
for i in list1:
for j in list2:
if i == j:
continue

print(i, "*", j, "=", i*j)


Output:

2 * 6 = 12
2 * 5 = 10
5 * 2 = 10
5 * 6 = 30
6 * 2 = 12
6 * 5 = 30

2. This code prints each fruit 6 times in a row, then moves to


the next line for the next fruit.
fruits = ["mango","apple","banana"]
for fruit in fruits:
count = 0
while count < 6:
print(fruit, end=" ")
count = count+1
print( )
Output:

mango mango mango mango mango mango


apple apple apple apple apple apple
banana banana banana banana banana banana

3.This code finds and prints all perfect numbers between 1 and
100.
a=1
while a<=100:
y_sum = 0
for i in range (1,a):
if a%i == 0:
y_sum = y_sum + i
if y_sum == a:
print( "perfect number:" ,a)
a = a+1

Output:

perfect number: 6
perfect number: 28
4. This code compares each element i from list1 with each
element j from list2. If i != j, it prints their product.
st1 = [2,5,6]
list2 = [2,6,5]
result = []
for i in list1:
for j in list2:
if i == j:
continue
Output:

2 * 6 = 12
2 * 5 = 10
5 * 2 = 10
5 * 6 = 30
6 * 2 = 12
6 * 5 = 30

4. This code adds each element from list1 to each element in


list2 and stores all the sums in result
list1 = [10,25,30]
list2 = [60,15,50]
result = []
for i in list1:
for j in list2:
result.append(i+j)
print(result)
Output:
[70, 25, 60, 85, 40, 75, 90, 45, 80]

5. This code prints a right-angled triangle made of * symbols,


but in *reverse order* (starting from the bottom).

# print right triangle


i = 11
while(i>0):
j = 11
while(j>i):
print("*", end = " " )
j = j-1
i = i-1
print( )
Output:
*
**
***
****
*****
******
*******
********
*********
**********

6. This code prints every combination of colour and items with


a space after each pair.
colour = ["red","black","pink"]
items = ["apple","vegeies","shirt"]
for x in colour:
for y in items:
print(x,y)
print( " ")
Output:
red apple

red vegeies

red shirt

black apple

black vegeies

black shirt

pink apple

pink vegeies

pink shir
7. This code prints each character of each fruit name separated
by *.
list_fruits = ["mango","apple","banana"]
for fruit in list_fruits:
for i in fruit:
print(i, end="*")
print( )
Output:

m*a*n*g*o*
a*p*p*l*e*
b*a*n*a*n*a*

8. - Outer loop controls the rows (1 to 7).


- Inner loop prints * equal to the row number.
- print() moves to the next line after each row.
row=7
# outer loop
for i in range (1,row+1):
# inner loop
for j in range(1,i+1):
print("*",end=" ")
print(")
Output:

*
**
***
****
*****
******
*******

9. - The loop prints count values from 0 to 4.


- When count becomes 5, the break statement exits the loop.
- Since the loop exits via break, the else block does not execute.
- After the loop ends, "out from loop" is printed.
count=0
while True:
print(count)
count+=1
if count==5:
break
else:
print("in else block")
print("out from loop")
Output:

0
1
2
3
4
out from loop

10. - The user enters a number.


- The program keeps adding the numbers to total.
- When the user inputs 0, the loop ends, and the final sum
(total) is printed.
total=0
number=int(input("enter a number(0 to quit):"))
while number!=0:
total = total+number
number=int(input("enter a number(0 to quit):"))
print("total is" ,total )
Example Output:

enter a number(0 to quit): 5


enter a number(0 to quit): 10
enter a number(0 to quit): 3
enter a number(0 to quit): 0
total is 18

11.This code prints a right-angled triangle of asterisks (*)


aligned to the left.
print right triangle
for i in range(11):
for j in range (i):
print("*", end = " ")
print(" ")
Output:

*
**
***
****
*****
******
*******
********
*********
**********

12. This code prints a right-angled triangle made of * symbols,


but in *reverse order* (starting from the bottom).
# print right triangle
i = 11
while(i>0):
j = 11
while(j>i):
print("*", end = " " )
j = j-1
i = i-1
print( )
Output:

*
**
***
****
*****
******
*******
********
*********
**********

You might also like