Loading...
More Python Posts
""" Calculator----------------------------------------"""def addition ():print("Addition")n = float(input("Enter the number: "))t = 0 //Total number enterans = 0while n != 0:ans = ans + nt+=1n = 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 entersum = 0while n != 0:ans = ans - nt+=1n = 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 enterans = 1while n != 0:ans = ans * nt+=1n = float(input("Enter another number (0 to calculate): "))return [ans,t]def average():an = []an = addition()t = an[1]a = an[0]ans = a / treturn [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
import sys# sample TuplesTuple1 = ("A", 1, "B", 2, "C", 3)Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))# print the sizes of sample Tuplesprint("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")
list_1 = [1,2,3,4,5,6,7,8,9]cubed = map(lambda x: pow(x,3), list_1)print(list(cubed))#Results#[1, 8, 27, 64, 125, 216, 343, 512, 729]
"""Assignment 6The goal is to make a graph ofwho bit who and who was bitten.There should be 10 nodes and 15 edges.3 arrows of biting each other and3 arrows of someone biting themselves.Networkx can not do self bitingarrows, but it is in the code."""from graphviz import Digraph as DDotGraphfrom graphviz import Graph as UDotGraphimport networkx as nxfrom networkx.algorithms.dag import transitive_closureimport graphviz as gvimport matplotlib.pyplot as pltimport numpy as npfrom numpy.linalg import matrix_power"""class DGraph:def __init__(self):self.d = dict()def clear(self):self.d = dict()def add_node(self,n):if not self.d.get(n):self.d[n] = set()def add_edge(self,e):f,t=eself.add_node(f)self.add_node(t)vs=self.d.get(f)if not vs:self.d[f] = {t}else:vs.add(t)def add_edges_from(self,es):for e in es:self.add_edge(e)def edges(self):for f in self.d:for t in self.d[f]:yield (f,t)def number_of_nodes(self):return len(self.d)def __repr__(self):return self.d.__repr__()def show(self):dot = gv.Digraph()for e in self.edges():#print(e)f, t = edot.edge(str(f), str(t), label='')#print(dot.source)show(dot)# displays graph with graphvizdef show(dot, show=True, file_name='graph.gv'):dot.render(file_name, view=show)def showGraph(g,label="",directed=True):if directed:dot = gv.Digraph()else:dot = gv.Graph()for e in g.edges():print(e)f, t = edot.edge(str(f), str(t), label=label)print(dot.source)show(dot)def bit():G = DGraph()G.add_edge(("Blade","Samara"))G.add_edge(("Shadow","Wolfe"))G.add_edge(("Raven", "Austin"))G.add_edge(("Blade", "Alice"))G.add_edge(("Alice","Brandon"))G.add_edge(("Blade", "Wolfe"))G.add_edge(("Samara", "Robin"))G.add_edge(("Samara", "Raven"))G.add_edge(("Samara", "Hamed"))G.add_edge(("Wolfe", "Blade"))G.add_edge(("Hamed", "Samara"))G.add_edge(("Wolfe", "Shadow"))G.add_edge(("Brandon", "Brandon"))G.add_edge(("Hamed", "Hamed"))G.add_edge(("Austin", "Austin"))showGraph(G, label="bit")bit()def bitten():G=DGraph()G.add_edge(("Samara","Blade"))G.add_edge(("Wolfe","Shadow"))G.add_edge(("Austin", "Raven"))G.add_edge(("Alice","Blade"))G.add_edge(("Brandon", "Alice"))G.add_edge(("Wolfe", "Blade" ))G.add_edge(("Robin", "Samara"))G.add_edge(("Raven", "Samara"))G.add_edge(("Hamed", "Samara"))G.add_edge(("Blade", "Wolfe"))G.add_edge(("Samara", "Hamed"))G.add_edge(("Shadow", "Wolfe"))G.add_edge(("Brandon", "Brandon"))G.add_edge(("Hamed", "Hamed"))G.add_edge(("Austin", "Austin"))showGraph(G, label="bitten by")#bitten()family = ["Blade", "Samara", "Shadow", "Wolfe", "Raven", "Alice"]"""#Do transitive closure call out and the#matrix power operation should be the sameD = nx.DiGraph()#D.add_nodes_from("SamaraBladeWolfeShadowAliceRavenBrandonRobinHamedAustin")D.add_edge("Blade","Samara")D.add_edge("Shadow","Wolfe")D.add_edge("Raven", "Austin")D.add_edge("Blade", "Alice")D.add_edge("Alice","Brandon")D.add_edge("Blade", "Wolfe")D.add_edge("Samara", "Robin")D.add_edge("Samara", "Raven")D.add_edge("Samara", "Hamed")D.add_edge("Wolfe", "Blade")D.add_edge("Hamed", "Samara")D.add_edge("Wolfe", "Shadow")D.add_edge("Brandon", "Brandon")D.add_edge("Hamed", "Hamed")D.add_edge("Austin", "Austin")T = transitive_closure(D)for e in D.edges(): print(e)for n in D.nodes(): print(n)def show(H):nx.draw(H, with_labels=True, font_weight='bold')plt.show()#Use nx.to_numpy_matrix instead of nx.adjacency_matrix# M = nx.adjacency_matrix(D)# MT = nx.adjacency_matrix(T)M = nx.to_numpy_matrix(D)MT = nx.to_numpy_matrix(T)M2 = M@Mdef mPower(M, k): #M is numpy matrixassert k >= 1P = Mfor _ in range(k):P = P @ Mreturn Pdef tc(M):#compute transitive closurepassD1 = nx.DiGraph(M)D2 = nx.DiGraph(M2)print('Matrix for Original\n', M)N = nx.to_numpy_array(D,dtype=int)print('np_array for Original\n', N)print('\nMatrix for Transitive Closure\n', MT)N2 = nx.to_numpy_array(T,dtype=int)print('np_array for Transitive Closure\n', N2)show(D) #can use D, T, and numpy matrix power operationshow(T)show(T)
my_list = [1, 2, 3, 4, 5]removed_element = my_list.pop(2) # Remove and return element at index 2print(removed_element) # 3print(my_list) # [1, 2, 4, 5]last_element = my_list.pop() # Remove and return the last elementprint(last_element) # 5print(my_list) # [1, 2, 4]
primes=[]products=[]def prime(num):if num > 1:for i in range(2,num):if (num % i) == 0:return Falseelse:primes.append(num)return Truefor n in range(30,1000):if len(primes) >= 20:break;else:prime(n)for previous, current in zip(primes[::2], primes[1::2]):products.append(previous * current)print (products)