Advertisement
Guest User

Examples of Dip Code (in Python)

a guest
Jul 7th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. ## Factorial function
  2. def factorial(term):
  3.     fact = 1
  4.     if term >= 1:
  5.         for i in range(1, term + 1):
  6.             fact = fact * i
  7.     print(fact)
  8.  
  9. ## Fibonnaci Function
  10. def fib(nterms):
  11.     n1 = 0
  12.     n2 = 1
  13.     count = 0
  14.  
  15.     if nterms <= 0: print("Input positive integer")
  16.     if nterms == 1:
  17.         print("Fibonnaci sequence upto" + " 1" + ":")
  18.         print(n1)
  19.     else:
  20.         print("Fibonacci sequence:")
  21.         while count < nterms:
  22.             print(n1)
  23.             nth = n1 + n2
  24.             n1 = n2
  25.             n2 = nth
  26.             count = count + 1
  27.  
  28. ## Hello World in Python
  29. print("Hello, world!")
  30.  
  31. ## Random
  32. from random import randint
  33. def random(list_):
  34.     random_number = randint(0, len(list_) - 1)
  35.     return(list_[random_number])
  36.  
  37. ## Math Functions
  38. # Squares a value
  39. square = lambda num: num**2
  40.  
  41. # Checks if a number is even or odd
  42. def odd_or_even(number):
  43.     if number % 2 == 0:
  44.         print("even")
  45.     else:
  46.         print("odd")
  47.  
  48. ## Examples
  49. fib(4)
  50. factorial(4)
  51. print(square(4))
  52. print(random([1, 2, 3, 4]))
  53. odd_or_even(4)
  54.  
  55. ## Caesar Cipher in Python
  56. alphabet = "abcdefghijklmnopqrstuvwxyz "
  57. plaintext = input("Which word / phrase would you like to encrypt: ")
  58. key = int(input("What would you like the key to be? "))
  59. ciphertext = ""
  60.  
  61. for character in range(0, len(plaintext)):
  62.     for letter in range(0, len(alphabet)):
  63.         if plaintext[character] == alphabet[letter]:
  64.             if plaintext[character] != " ":
  65.                 index = letter + key
  66.                 ciphertext = ciphertext + alphabet[index % 26]
  67.             else:
  68.                 ciphertext = ciphertext + " "
  69.  
  70. print("ciphertext: " + ciphertext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement