Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Factorial function
- def factorial(term):
- fact = 1
- if term >= 1:
- for i in range(1, term + 1):
- fact = fact * i
- print(fact)
- ## Fibonnaci Function
- def fib(nterms):
- n1 = 0
- n2 = 1
- count = 0
- if nterms <= 0: print("Input positive integer")
- if nterms == 1:
- print("Fibonnaci sequence upto" + " 1" + ":")
- print(n1)
- else:
- print("Fibonacci sequence:")
- while count < nterms:
- print(n1)
- nth = n1 + n2
- n1 = n2
- n2 = nth
- count = count + 1
- ## Hello World in Python
- print("Hello, world!")
- ## Random
- from random import randint
- def random(list_):
- random_number = randint(0, len(list_) - 1)
- return(list_[random_number])
- ## Math Functions
- # Squares a value
- square = lambda num: num**2
- # Checks if a number is even or odd
- def odd_or_even(number):
- if number % 2 == 0:
- print("even")
- else:
- print("odd")
- ## Examples
- fib(4)
- factorial(4)
- print(square(4))
- print(random([1, 2, 3, 4]))
- odd_or_even(4)
- ## Caesar Cipher in Python
- alphabet = "abcdefghijklmnopqrstuvwxyz "
- plaintext = input("Which word / phrase would you like to encrypt: ")
- key = int(input("What would you like the key to be? "))
- ciphertext = ""
- for character in range(0, len(plaintext)):
- for letter in range(0, len(alphabet)):
- if plaintext[character] == alphabet[letter]:
- if plaintext[character] != " ":
- index = letter + key
- ciphertext = ciphertext + alphabet[index % 26]
- else:
- ciphertext = ciphertext + " "
- print("ciphertext: " + ciphertext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement