Loading...
More Python Posts
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)
# importing the modulesimport osimport shutil# getting the current working directorysrc_dir = os.getcwd()# printing current directoryprint(src_dir)# copying the filesshutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst# printing the list of new filesprint(os.listdir())
# Function to check Armstrong numberdef is_armstrong_number(number):# Convert number to string to iterate over its digitsnum_str = str(number)# Calculate the sum of the cubes of each digitdigit_sum = sum(int(digit) ** len(num_str) for digit in num_str)# Compare the sum with the original numberif digit_sum == number:return Trueelse:return False# Prompt user for a numbernumber = int(input("Enter a number: "))# Check if the number is an Armstrong numberif is_armstrong_number(number):print(number, "is an Armstrong number.")else:print(number, "is not an Armstrong number.")
from functools import partialdef curry(fn, *args):return partial(fn, *args)add = lambda x, y: x + yadd10 = curry(add, 10)add10(20) # 30
primes=[]products=[]def prime(num):if num > 1:for i in range(2,num):if (num % i) == 0:return Falseelse:primes.append(num)return Truefor n in range(30,1000):if len(primes) >= 20:break;else:prime(n)for previous, current in zip(primes[::2], primes[1::2]):products.append(previous * current)print (products)
def print_pyramid_pattern(n):# outer loop to handle number of rows# n in this casefor i in range(0, n):# inner loop to handle number of columns# values changing acc. to outer loopfor j in range(0, i+1):# printing starsprint("* ",end="")# ending line after each rowprint("\r")print_pyramid_pattern(10)