0% found this document useful (0 votes)
4 views3 pages

Python Basics: Loops and Patterns

Python programming topic Loop for ug students

Uploaded by

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

Python Basics: Loops and Patterns

Python programming topic Loop for ug students

Uploaded by

mr.rehankmd123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print first 10 natural numbers


i=1
while i <= 10:
print(i)
i += 1

2. Number Pattern
print("Number Pattern ")
row = 5
for i in range(1, row + 1, 1):
for j in range(1, i + 1):
print(j, end=' ')
print("")

3. Sum of all numbers


Using loop
s=0
n = int(input("Enter number "))
for i in range(1, n + 1, 1):
s += i
print("\n")
print("Sum is: ", s)

Using the built-in function sum()


n = int(input("Enter number "))
# pass range of numbers to sum() function
x = sum(range(1, n + 1))
print('Sum is:', x)

4. Multiplication Table
n = int(input(“Enter a no : ”))
for i in range(1, 11, 1):
product = n * i
print(product)

5. Print nos from list (use of break and continue)


numbers = [12, 75, 150, 180, 145, 525, 50]
for item in numbers:
if item > 500:
break
elif item > 150:
continue
elif item % 5 == 0:
print(item)

6. Count digits
num = int(input(“Enter a no : ”))
count = 0
while num != 0:
num = num // 10
count = count + 1
print("Total digits are:", count)
7. Number Pattern
n=5
k=5
for i in range(0,n+1):
for j in range(k-i,0,-1):
print(j,end=' ')
print()

8. Reverse a list
Using a reversed() function and for loop
list1 = [10, 20, 30, 40, 50]
# reverse list
new_list = reversed(list1)
# iterate reversed list
for item in new_list:
print(item)
Using for loop and the len() function
list1 = [10, 20, 30, 40, 50]
size = len(list1) - 1
for i in range(size, -1, -1):
print(list1[i])

9. Prime nos in a range


start = int(input(“Enter first no : ”))
end = int(input(“Enter last no : ”))
print("Prime numbers between", start, "and", end, "are:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

10. Fibonacci Series


num1, num2 = 0, 1
print("Fibonacci sequence:")
for i in range(10):
print(num1, end=" ")
res = num1 + num2
num1 = num2
num2 = res

11. Reverse a no
num = int(input(“Enter a no : ”))
rn = 0
print("Given Number ", num)
while num > 0:
reminder = num % 10
rnumber = (rn * 10) + reminder
num = num // 10
print("Reverse Number ", rn)
12. Factorial of a given no
num = int(input(“Enter a no : ”))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
# multiply factorial by current number
factorial = factorial * i
print("The factorial of", num, "is", factorial)

13. Star Pattern


rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print("\r")
for i in range(rows, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print("\r")

You might also like