Skip to main content

Palindrome checker

0 likes • Nov 19, 2022 • 3 views
Python
Loading...

More Python Posts

Check Armstrong Number

0 likes • May 31, 2023 • 0 views
Python
# Function to check Armstrong number
def is_armstrong_number(number):
# Convert number to string to iterate over its digits
num_str = str(number)
# Calculate the sum of the cubes of each digit
digit_sum = sum(int(digit) ** len(num_str) for digit in num_str)
# Compare the sum with the original number
if digit_sum == number:
return True
else:
return False
# Prompt user for a number
number = int(input("Enter a number: "))
# Check if the number is an Armstrong number
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")

Mad libs generator

0 likes • Nov 19, 2022 • 0 views
Python
#Loop back to this point once code finishes
loop = 1
while (loop < 10):
#All the questions that the program asks the user
noun = input("Choose a noun: ")
p_noun = input("Choose a plural noun: ")
noun2 = input("Choose a noun: ")
place = input("Name a place: ")
adjective = input("Choose an adjective (Describing word): ")
noun3 = input("Choose a noun: ")
#Displays the story based on the users input
print ("------------------------------------------")
print ("Be kind to your",noun,"- footed", p_noun)
print ("For a duck may be somebody's", noun2,",")
print ("Be kind to your",p_noun,"in",place)
print ("Where the weather is always",adjective,".")
print ()
print ("You may think that is this the",noun3,",")
print ("Well it is.")
print ("------------------------------------------")
#Loop back to "loop = 1"
loop = loop + 1

when predicate lambda

0 likes • Nov 19, 2022 • 0 views
Python
def when(predicate, when_true):
return lambda x: when_true(x) if predicate(x) else x
double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)
print(double_even_numbers(2)) # 4
print(double_even_numbers(1)) # 1

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")

Append to a file

0 likes • Jun 1, 2023 • 0 views
Python
filename = "data.txt"
data = "Hello, World!"
with open(filename, "a") as file:
file.write(data)

Fibonacci Series

0 likes • Nov 18, 2022 • 5 views
Python
#Python 3: Fibonacci series up to n
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(1000)