Skip to main content

Differentiate Between del, remove, and pop on a List

0 likes • May 31, 2023 • 0 views
Python
Loading...

More Python Posts

Factorial of N

0 likes • Nov 19, 2022 • 0 views
Python
import math
def factorial(n):
print(math.factorial(n))
return (math.factorial(n))
factorial(5)
factorial(10)
factorial(15)

Connect to MYSQL and create a database

0 likes • Nov 19, 2022 • 0 views
Python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")

Nodes and Trees

0 likes • Nov 18, 2022 • 0 views
Python
import random
class Node:
def __init__(self, c):
self.left = None
self.right = None
self.color = c
def SetColor(self,c) :
self.color = c
def PrintNode(self) :
print(self.color)
def insert(s, root, i, n):
if i < n:
temp = Node(s[i])
root = temp
root.left = insert(s, root.left,2 * i + 1, n)
root.right = insert(s, root.right,2 * i + 2, n)
return root
def MakeTree(s) :
list = insert(s,None,0,len(s))
return list
def MakeSet() :
s = []
count = random.randint(7,12)
for _ in range(count) :
color = random.randint(0,1) == 0 and "Red" or "White"
s.append(color)
return s
def ChangeColor(root) :
if (root != None) :
if (root.color == "White") :
root.SetColor("Red")
ChangeColor(root.left)
ChangeColor(root.right)
def PrintList(root) :
if root.left != None :
PrintList(root.left)
else :
root.PrintNode()
if root.right != None :
PrintList(root.right)
else :
root.PrintNode()
t1 = MakeTree(MakeSet())
print("Original Colors For Tree 1:\n")
PrintList(t1)
ChangeColor(t1)
print("New Colors For Tree 1:\n")
PrintList(t1)
t2 = MakeTree(MakeSet())
print("Original Colors For Tree 2:\n")
PrintList(t2)
ChangeColor(t2)
print("New Colors For Tree 2:\n")
PrintList(t2)
t3 = MakeTree(MakeSet())
print("Original Colors For Tree 3:\n")
PrintList(t3)
ChangeColor(t3)
print("New Colors For Tree 3:\n")
PrintList(t3)

Create a Floyd’s Triangle

0 likes • May 31, 2023 • 0 views
Python
def generate_floyds_triangle(num_rows):
triangle = []
number = 1
for row in range(num_rows):
current_row = []
for _ in range(row + 1):
current_row.append(number)
number += 1
triangle.append(current_row)
return triangle
def display_floyds_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Floyd's Triangle: "))
# Generate Floyd's Triangle
floyds_triangle = generate_floyds_triangle(num_rows)
# Display Floyd's Triangle
display_floyds_triangle(floyds_triangle)

Remove numbers from files

0 likes • Mar 26, 2023 • 0 views
Python
import os
# Get the current directory
current_dir = os.getcwd()
# Loop through each file in the current directory
for filename in os.listdir(current_dir):
# Check if the file name starts with a number followed by a period and a space
if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ':
# Remove the number, period, and space from the file name
new_filename = filename[3:]
# Rename the file
os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))

Calculate the Area of a Triangle

0 likes • May 31, 2023 • 0 views
Python
# Prompt user for base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculate the area
area = (base * height) / 2
# Display the result
print("The area of the triangle is:", area)