Star pattern

 # Star Display

"""

"""

# Read Number of lines 

N = int(input("Enter an odd number : "))


print("Star pattern : ")


tmp=N//2

for n in range(1,tmp+2,1):

print("   "*(tmp-n+1),end="")

print(" * "*((2*n)-1))


for n in range(tmp,0,-1):

print("   "*(tmp-n+1),end="")

print(" * "*((2*n)-1))

Star pattern

 # Star Display

"""

"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Star pattern : ")


for n in range(N,0,-1):

print("   "*(N-n),end="")

print(" * "*((2*n)-1))


Star pattern

 # Star Display

"""

"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Star pattern : ")


for n in range(1,N+1,1):

print("   "*(N-n),end="")

print(" * "*((2*n)-1))


Star pattern

 # Star Display

"""

"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Star Pattern : ")


for n in range(N,0,-1):

print("* "*n)


Star pattern

 # Star Display

"""


"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Star Pattern : ")


for n in range(1,N+1,1):

print("* "*n)


Star patterns

 # Star Display

"""

"""

# Read Number of Rows and Columns 

R = int(input("Enter number of Rows : "))

C = int(input("Enter number of Columns : "))


print("Star Display : ")


for n in range(1,R+1,1):

print("*  "*C)


Star patterns

 # Star Display

"""


"""

# Read Number of Rows and Columns 

R = int(input("Enter number of Rows : "))

C = int(input("Enter number of Columns : "))


print("Star Display : ")


print("*  "*C)


for n in range(1,R-1,1):

print("*  ",end="")

print("   "*(C-2),end="")

print("*  ")


print("*  "*C)


Binary squares

 # Binary Display

"""

"""

# Read Number of Rows and Columns 

R = int(input("Enter number of Rows : "))

C = int(input("Enter number of Columns : "))


print("Binary Display : ")


print("0  "*C)


for n in range(1,R-1,1):

print("0  ",end="")

print("1  "*(C-2),end="")

print("0  ")


print("0  "*C)


Number Triangle

 # Number Triangles

"""

"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Number Triangle : ")


for n in range(1,N+1,1):

print("    "*(N-n),end="")

for k in range(1,n+1,1):

print(str(k).center(4), end="")

for k in range(n-1,0,-1):

print(str(k).center(4), end="")

print()


for n in range(1,N+1,1):

print("    "*(n),end="")

for k in range(1,N-n+1,1):

print(str(k).center(4), end="")

for k in range(N-n-1,0,-1):

print(str(k).center(4), end="")

print()


Number triangle

 # Number Triangles

"""

"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Number Triangle : ")


for n in range(1,N+1,1):

print("    "*(N-n),end="")

for k in range(1,n+1,1):

print(str(k).center(4), end="")

for k in range(n-1,0,-1):

print(str(k).center(4), end="")

print()


Number triangle - binary

 # Binary Triangles

"""

"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Binary Triangle : ")


for n in range(1,N+1,1):

i = n%2

for val in range (1,n+1,1):

print(i, end=" ")

i=(i+1)%2

print()


Number Triangles

 # Number Triangles

"""


"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Number Triangle : ")


i = 1


for n in range(1,N+1,1):

for val in range (1,n+1,1):

print(i, end=" ")

i+=1

print()


Number triangles

 # Number Triangles

"""


"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Number Triangle : ")


for n in range(1,N+1,1):

for val in range (1,n+1,1):

print(n, end=" ")

print()


Number Triangles Display

 




Write program to generate the following NUMBER / STAR arrangements

1. Pascal Triangle


2. Simple Number Arrangements 






3. Binary Display



4. Star patterns 










Number Triangles

 # Number Triangles

"""


"""

# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Number Triangle : ")


for n in range(1,N+1,1):

for val in range (1,n+1,1):

print(val, end=" ")

print()




Pygame - Moving an object based on key pressed

# Moving an object based on keypress


import sys,pygame


pygame.init()

screen = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Moving object")

  

# Moving object starting co-ordinates and size

x = 200

y = 200


width = 20

height = 20

  

# Moving speed

step = 5

  

# infinite loop 

while 1:

pygame.time.delay(10)

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()


# Get the keys pressed 

keys = pygame.key.get_pressed()

      

    # Left arrow key - decrement in x axis

if keys[pygame.K_LEFT]and x>0:

x-=step

    # Right arrow key - increment in x axis

if keys[pygame.K_RIGHT] and x<500-width:

x += step

         

    # Up arrow key - decrement in y axis

if keys[pygame.K_UP] and y>0:

y -= step

          

    # Down arrow key - increment in y axis

if keys[pygame.K_DOWN] and y<500-height:

y += step

        

    # Set screen background and Place moving object on screen 

screen.fill((255, 255, 255))

pygame.draw.rect(screen, (255, 0, 0), (x, y, width, height))

      

    # Display movement

pygame.display.update() 

  

pygame.quit()

Pascal Triangle


# Number Triangles

# Pascals Triangle

"""

"""

# Factorial Function 


def fact(Num):

if(Num==0):

return 1

else:

return Num*fact(Num-1)



# Read Number of lines 

N = int(input("Enter number of Lines : "))


print("Pascal Triangle : ")


for n in range(0,N,1):

Nr = fact(n)

        # For Alignment

print("   "*(N-n),end="")

for k in range(0,n+1,1):

term = (int)(Nr/(fact(k)*fact(n-k)))

print(str(term).center(6), end="")

print()




# Output


> python PascalTriangle.py

Enter number of Lines : 10

Pascal Triangle :




Formula for calculating elements in Pascal Triangle




matplotlib bar chart

 # Creating a simple barchart using matplotlib


# importing the required module 

import matplotlib.pyplot as plt 


# plotting the x and y axis values for two sets of data


plt.bar([1,2,3,4,5,6,7],[7.1,9.4,10.6,12.5,11.5,10.9,9.8], label="Chennai",width=.25)


plt.bar([1.25,2.25,3.25,4.25,5.25,6.25,7.25],[8.1,10.4,9.6,8.5,10.5,8.9,7.8], label="Kancheepuram",width=.25) 


# Naming the axes and setting the legends

plt.legend()

plt.xlabel('Days')

plt.ylabel('Rainfall (mm)')


# Graph title

plt.title('Rainfall')


# Display the graph

plt.show()

"""

Output



"""


Matplotlib line graph

# Creating a line graph using matplotlib


 """

Before executing the program install matplotlib


open command prompt and type


    pip install matplotlib

   

this will install all required packages


"""


# importing the required module 

import matplotlib.pyplot as plt 

    

# x axis values 

x = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] 


# corresponding y axis values 

y1 = [24.3, 25.3, 27.4, 29.6, 30.8, 30.3, 30, 29.2, 28.7, 27.3, 25.7, 24.6] 

y2 = [23.9, 25.8, 28.1, 28.9, 27.5, 25.2, 24.5, 24.5, 25, 24.8, 23.9, 23.2]  


# Setting the width and height of graph

f = plt.figure()

f.set_figwidth(12)

f.set_figheight(4)


# plotting the points  

plt.plot(x, y1, 'g', label='Chennai', linewidth=2.5) 

plt.plot(x, y2, 'r', label='Coimbatore', linewidth=2.5)

    

# naming the x axis 

plt.xlabel('Month') 


# naming the y axis 

plt.ylabel('Temperature') 


# display legend

plt.legend()

    

# giving a title to my graph 

plt.title('Monthly Avarage Temperature') 

    

# function to show the plot 

plt.show() 


"""

Output



"""





Matplotlib example - ploting a triangle

 # Creating a simple triangle using matplotlib


"""

Before executing the program install matplotlib

open command prompt and type


    pip install matplotlib

   

this will install all required packages

"""


# importing the required module 

import matplotlib.pyplot as plt 

    

# x axis values 

x = [1,2,3,1] 


# corresponding y axis values 

y = [2,4,1,2] 

    

# plotting the points  

plt.plot(x, y) 

    

# naming the x axis 

plt.xlabel('x - axis') 


# naming the y axis 

plt.ylabel('y - axis') 

    

# giving a title to my graph 

plt.title('My first Triangle!') 

    

# function to show the plot 

plt.show() 


Output:




Number Patterns - squares and cubes of numbers

# Number Patterns

# Squares and cubes of Numbers


Num_of_terms = int(input("Enter number of terms : "))

val = 1


print("Number Patterns : ")

print("Squares of Numbers : ", end="")

for i in range(1,Num_of_terms+1,1):

print(i*i, end=" ")


print("\nCubes of Numbers : ", end="")

for i in range(1,Num_of_terms+1,1):

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

Number Patters - Odd Numbers

 # Number Patterns

# Odd Numbers


Num_of_terms = int(input("Enter number of terms : "))

val = 1


print("Number Patterns - Odd Numbers : ")

for i in range(0,Num_of_terms,1):

print(val, end=" ")

val+=2

Number series - Fibonacci

 # Number series 

# Fibonacci series 


print("Fibonacci series : ")


Num_of_terms = int(input("Enter number of terms : ")) 


f1 = -1

f2 = 1


for i in range(0,Num_of_terms,1):

term = f1+f2

print(term, end=" ")

f1 = f2

f2 = term


Exchange the values of two variables

 # Exchange the values of two variables


var1 = int(input("Enter value : "))

var2 = int(input("Enter value : "))


print("Original values : ")

print("Value 1= ",var1)

print("Value 2= ",var2)


#swapping the values

var1, var2 = var2, var1


print("Swapped values : ")

print("Value 1 = ",var1)

print("Value 2 = ",var2)