Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Patterns in Python

The document contains various Python code snippets demonstrating different patterns and programming concepts, including loops, conditional statements, and functions. It showcases how to create patterns using numbers and stars, perform calculations like simple interest, check for prime numbers, and validate passwords. Additionally, it includes user input examples for various scenarios such as guessing a number and reversing a three-digit integer.

Uploaded by

KASHISH MADAN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Patterns in Python

The document contains various Python code snippets demonstrating different patterns and programming concepts, including loops, conditional statements, and functions. It showcases how to create patterns using numbers and stars, perform calculations like simple interest, check for prime numbers, and validate passwords. Additionally, it includes user input examples for various scenarios such as guessing a number and reversing a three-digit integer.

Uploaded by

KASHISH MADAN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

PATTERNS IN PYTHON

# PATTERN 1-
print("Second Number Pattern ")
lastNumber = 6
for row in range(1, lastNumber):
for column in range(1, row + 1):
print(column, end=' ')
print("")
# PATTERN 2-
rows = 5
b=0
for i in range(rows, 0, -1):
b += 1
for j in range(1, i + 1):
print(b, end=' ')
print('\r')
# PATTERN 3-
rows = 9
for i in range(1, rows):
for j in range(-1+i, -1, -1):
print(format(2**j, "4d"), end=' ')
print("")
# PATTERN 4-
rows = 7
for i in range(0, rows):
for j in range(0, i + 1):
print(i * j, end=' ')
print()
# PATTERN 5-
rows = 6
for i in range(0, rows):
for j in range(rows - 1, i, -1):
print(j, '', end='')
for l in range(i):
print(' ', end='')
for k in range(i + 1, rows):
print(k, '', end='')
print('\n')
# PATTERN 6-
rows = 5
i=1
while i <= rows:
j=1
while j <= i:
print((i * 2 - 1), end=" ")
j=j+1
i=i+1
print()
# PATTERN 7-
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows + 1):
if j <= i:
print(i, end=' ')
else:
print(j, end=' ')
print()
# PATTERN 8-
def print_pascal_triangle(size):
for i in range(0, size):
for j in range(0, i + 1):
print(decide_number(i, j), end=" ")
print()
def decide_number(n, k):
num = 1
if k > n - k:
k=n-k
for i in range(0, k):
num = num * (n - i)
num = num // (i + 1)
return num
rows = 7
print_pascal_triangle(rows)
# PATTERN 9-
print("Print equilateral triangle Pyramid using stars ")
size = 7
m = (2 * size) - 2
for i in range(0, size):
for j in range(0, m):
print(end=" ")
m = m - 1 # decrementing m after each loop
for j in range(0, i + 1):
# printing full Triangle pyramid using stars
print("* ", end=' ')
print(" ")
# PATTERN 10-
rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')

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

print(" ")

for i in range(rows + 1, 0, -1):


for j in range(0, i - 1):
print("*", end=' ')
print(" ")
# PATTERN 13-
rows = 14
print("*" * rows, end="\n")
i = (rows // 2) - 1
j=2
while i != 0:
while j <= (rows - 2):
print("*" * i, end="")
print("_" * j, end="")
print("*" * i, end="\n")
i=i-1
j=j+2
# PATTERN 14-
print("Print Alphabets and Letters pattern in Python ")
asciiNumber = 65
for i in range(0, 7):
for j in range(0, i + 1):
character = chr(asciiNumber)
print(character, end=' ')
asciiNumber += 1
print(" ")
# PATTERN 15-
num = 4
counter=0
for x in range(0, num):
for y in range(0, x+1):
print(counter, end=" ")
counter = 2**(x+1)
print()
LOOPING IN PYTHON
#Find numbers which are divisible by 7 and multiple
of 5 between a range-
nl=[]
for x in range(1500, 2701):
if (x%7==0) and (x%5==0):
nl.append(str(x))
print (','.join(nl))#JOIN FUNCTION
#Guess a number between 1 to 9-
import random
target_num, guess_num = random.randint(1, 10), 0
while target_num != guess_num:
guess_num = int(input('Guess a number between 1 and 10 until
you get it right : '))
print('Well guessed!')
#Count the number of even and odd numbers from a
series of numbers-
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
count_odd = 0
count_even = 0
for x in numbers:
if not x % 2:
count_even+=1
else:
count_odd+=1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
#Check the validity of a password-
import re
p= input("Input your password")
x = True
while x:
if (len(p)<6 or len(p)>12):
break
elif not re.search("[a-z]",p):
break
elif not re.search("[0-9]",p):
break
elif not re.search("[A-Z]",p):
break
elif not re.search("[$#@]",p):
break
elif re.search("\s",p):
break
else:
print("Valid Password")
x=False
break
if x:
print("Not a Valid Password")
#Print alphabet pattern A-
result_str="";
for row in range(0,7):
for column in range(0,7):
if (((column == 1 or column == 5) and row != 0) or ((row == 0 or
row == 3) and (column > 1 and column < 5))):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str);
#Convert month name to a number of days-
print("List of months: January, February, March, April, May, June,
July, August, September, October, November, December")
month_name = input("Input the name of Month: ")
if month_name == "February":
print("No. of days: 28/29 days")
elif month_name in ("April", "June", "September", "November"):
print("No. of days: 30 days")
elif month_name in ("January", "March", "May", "July", "August",
"October", "December"):
print("No. of days: 31 day")
else:
print("Wrong month name")
#Pattern-
for i in range(10):
print(str(i) * i)
# WAP to input single digits and print 3 digit numbers
created as n(n+1)(n+2). For example if input is 7 then
print should be 789-
n = input("Enter a single digit number :")
num = n + str(int(n)+1) + str(int(n)+2)
print("The three digit number is :", num)
# Q5 WAP to obtain principal, ROI, time from the user
and compute simple interest-
P=float(input("enter principal amount"))
R=float(input("enter rate of interest"))
T=float(input("enter time period "))
SI=P*R*T/100
print("simple interest is:",SI)
# WAP to accept three digit integer number then print
reverse. For example if input is 125 and output should
be 521-
n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
# WAP to program to input length of three sides a
triangle. Then check if these sides will form a triangle
or not-
#QUESTION NUMBER 8
a=int(input("enter length of side 1:"))
b=int(input("enter length of side 2:"))
c=int(input("enter length of side 1:"))
if a+b<c or b+c<a or a+c<b:
print("not a triangle")
else:
print("its a triangle")
# WAP to take an integer a as an input and check
whether it ends with 4 or 8. If it ends with 4 print
“ends with 4” , if it ends with 8 print “ends with 8”
other wise print ‘ends with neither’-
a = input("enter a number:")
if a[-1]=="4":
print("ends with 4")
elif a[-1]=="8":
print("ends with 8")
else:
print("ends with neither")
# Write a program to swap two variable-
x=int(input("enter a number 1:"))
y=int(input("enter a number 2:"))
print(x,y)
x=x+y
y=x-y
x=x-y
print(x,y)
# WAP to find prime numbers between 1 to 100-
for Number in range (1, 101):
count = 0
for i in range(2,(Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break

if (count == 0 and Number!= 1):


print(" %d" %Number, end = ' ')
# WAP to programme to check Armstrong-
num = int(input("enter a number"))
a = len(str(num))
sum = 0
k=num
while k > 0:
digit = k % 10
sum += digit ** a
k//= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

You might also like