• Nov 18, 2022 •AustinLeath
0 likes • 9 views
#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)
• Nov 19, 2022 •CodeCatch
0 likes • 2 views
""" Calculator ---------------------------------------- """ def addition (): print("Addition") n = float(input("Enter the number: ")) t = 0 //Total number enter ans = 0 while n != 0: ans = ans + n t+=1 n = float(input("Enter another number (0 to calculate): ")) return [ans,t] def subtraction (): print("Subtraction"); n = float(input("Enter the number: ")) t = 0 //Total number enter sum = 0 while n != 0: ans = ans - n t+=1 n = float(input("Enter another number (0 to calculate): ")) return [ans,t] def multiplication (): print("Multiplication") n = float(input("Enter the number: ")) t = 0 //Total number enter ans = 1 while n != 0: ans = ans * n t+=1 n = float(input("Enter another number (0 to calculate): ")) return [ans,t] def average(): an = [] an = addition() t = an[1] a = an[0] ans = a / t return [ans,t] // main... while True: list = [] print(" My first python program!") print(" Simple Calculator in python by Malik Umer Farooq") print(" Enter 'a' for addition") print(" Enter 's' for substraction") print(" Enter 'm' for multiplication") print(" Enter 'v' for average") print(" Enter 'q' for quit") c = input(" ") if c != 'q': if c == 'a': list = addition() print("Ans = ", list[0], " total inputs ",list[1]) elif c == 's': list = subtraction() print("Ans = ", list[0], " total inputs ",list[1]) elif c == 'm': list = multiplication() print("Ans = ", list[0], " total inputs ",list[1]) elif c == 'v': list = average() print("Ans = ", list[0], " total inputs ",list[1]) else: print ("Sorry, invilid character") else: break
• May 31, 2023 •CodeCatch
0 likes • 0 views
class Rectangle: pass class Square(Rectangle): pass rectangle = Rectangle() square = Square() print(isinstance(rectangle, Rectangle)) # True print(isinstance(square, Rectangle)) # True print(isinstance(square, Square)) # True print(isinstance(rectangle, Square)) # False
0 likes • 1 view
import math def factorial(n): print(math.factorial(n)) return (math.factorial(n)) factorial(5) factorial(10) factorial(15)
• Mar 10, 2021 •Skrome
import string def caesar(text, shift, alphabets): def shift_alphabet(alphabet): return alphabet[shift:] + alphabet[:shift] shifted_alphabets = tuple(map(shift_alphabet, alphabets)) final_alphabet = "".join(alphabets) final_shifted_alphabet = "".join(shifted_alphabets) table = str.maketrans(final_alphabet, final_shifted_alphabet) return text.translate(table) plain_text = "Hey Skrome, welcome to CodeCatch" print(caesar(plain_text, 8, [string.ascii_lowercase, string.ascii_uppercase, string.punctuation]))
# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. # For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”. # Python program to print all primes smaller than or equal to # n using Sieve of Eratosthenes def SieveOfEratosthenes(n): # Create a boolean array "prime[0..n]" and initialize # all entries it as true. A value in prime[i] will # finally be false if i is Not a prime, else true. prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): # If prime[p] is not changed, then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0]= False prime[1]= False # Print all prime numbers for p in range(n + 1): if prime[p]: print (p) # driver program if __name__=='__main__': n = 30 print("Following are the prime numbers smaller") print("than or equal to ", n) print("than or equal to ", n) SieveOfEratosthenes(n)