## Untitled

0 likes • Apr 21, 2023
Python

## More Python Posts

`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))`
```def max_n(lst, n = 1):  return sorted(lst, reverse = True)[:n]
max_n([1, 2, 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 stringstring = input("Enter a string: ")
# Compute permutationspermutations = compute_permutations(string)
# Display the permutationsprint("Permutations:")for permutation in permutations:    print(permutation)```
```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, kv))}print(sort)
# How to sort a dict by key Python 3>sort = {key:mydict[key] for key in sorted(mydict.keys())}print(sort)```
```filename = "data.txt"data = "Hello, World!"
with open(filename, "a") as file:    file.write(data)```
```import pandas as pd