Loading...
More Python Posts
prime_lists=[] # a list to store the prime numbersdef prime(n): # define prime numbersif n <= 1:return False# divide n by 2... up to n-1for i in range(2, n):if n % i == 0: # the remainder should'nt be a 0return Falseelse:prime_lists.append(n)return Truefor n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30prime(n)check=0 # a var to limit the output to 10 onlyfor n in prime_lists:for x in prime_lists:val= n *xif (val > 1000 ):check=check +1if (check <10) :print("the num is:", val , "=",n , "* ", x )break
import mathdef factorial(n):print(math.factorial(n))return (math.factorial(n))factorial(5)factorial(10)factorial(15)
# Python program for Plotting Fibonacci# spiral fractal using Turtleimport turtleimport mathdef fiboPlot(n):a = 0b = 1square_a = asquare_b = b# Setting the colour of the plotting pen to bluex.pencolor("blue")# Drawing the first squarex.forward(b * factor)x.left(90)x.forward(b * factor)x.left(90)x.forward(b * factor)x.left(90)x.forward(b * factor)# Proceeding in the Fibonacci Seriestemp = square_bsquare_b = square_b + square_asquare_a = temp# Drawing the rest of the squaresfor i in range(1, n):x.backward(square_a * factor)x.right(90)x.forward(square_b * factor)x.left(90)x.forward(square_b * factor)x.left(90)x.forward(square_b * factor)# Proceeding in the Fibonacci Seriestemp = square_bsquare_b = square_b + square_asquare_a = temp# Bringing the pen to starting point of the spiral plotx.penup()x.setposition(factor, 0)x.seth(0)x.pendown()# Setting the colour of the plotting pen to redx.pencolor("red")# Fibonacci Spiral Plotx.left(90)for i in range(n):print(b)fdwd = math.pi * b * factor / 2fdwd /= 90for j in range(90):x.forward(fdwd)x.left(1)temp = aa = bb = temp + b# Here 'factor' signifies the multiplicative# factor which expands or shrinks the scale# of the plot by a certain factor.factor = 1# Taking Input for the number of# Iterations our Algorithm will runn = int(input('Enter the number of iterations (must be > 1): '))# Plotting the Fibonacci Spiral Fractal# and printing the corresponding Fibonacci Numberif n > 0:print("Fibonacci series for", n, "elements :")x = turtle.Turtle()x.speed(100)fiboPlot(n)turtle.done()else:print("Number of iterations must be > 0")
import random# Define the ranks, suits, and create a deckranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']deck = [(rank, suit) for rank in ranks for suit in suits]# Shuffle the deckrandom.shuffle(deck)# Display the shuffled deckfor card in deck:print(card[0], "of", card[1])
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0}# How to sort a dict by value Python 3>sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))}print(sort)# How to sort a dict by key Python 3>sort = {key:mydict[key] for key in sorted(mydict.keys())}print(sort)
def generate_pascals_triangle(num_rows):triangle = []for row in range(num_rows):# Initialize the row with 1current_row = [1]# Calculate the values for the current rowif row > 0:previous_row = triangle[row - 1]for i in range(len(previous_row) - 1):current_row.append(previous_row[i] + previous_row[i + 1])# Append 1 at the end of the rowcurrent_row.append(1)# Add the current row to the triangletriangle.append(current_row)return triangledef display_pascals_triangle(triangle):for row in triangle:for number in row:print(number, end=" ")print()# Prompt the user for the number of rowsnum_rows = int(input("Enter the number of rows for Pascal's Triangle: "))# Generate Pascal's Trianglepascals_triangle = generate_pascals_triangle(num_rows)# Display Pascal's Triangledisplay_pascals_triangle(pascals_triangle)