Skip to main content

Untitled

0 likes • Apr 21, 2023
Python
Loading...
Download

More Python Posts

Python Fibonacci

JoeCamRoberon
0 likes • Sep 6, 2020
Python
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))

return maximum

CodeCatch
0 likes • Nov 19, 2022
Python
def max_n(lst, n = 1):
return sorted(lst, reverse = True)[:n]
max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3, 2]
import itertools
def compute_permutations(string):
# Generate all permutations of the string
permutations = itertools.permutations(string)
# Convert each permutation tuple to a string
permutations = [''.join(permutation) for permutation in permutations]
return permutations
# Prompt the user for a string
string = input("Enter a string: ")
# Compute permutations
permutations = compute_permutations(string)
# Display the permutations
print("Permutations:")
for permutation in permutations:
print(permutation)

Dictionary Sort

AustinLeath
0 likes • Nov 18, 2022
Python
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)

Append to a file

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

Read Dataset from excel file

KETRICK
0 likes • Oct 7, 2022
Python
import pandas as pd
x = pd.read_excel(FILE_NAME)
print(x)