#! /usr/bin/python #==================================================================== #REGULAR #==================================================================== N = 5 numbers = [1,5,2,3,2] suma = 0 for i in xrange(0,N,1): suma += numbers[i] print "The result is %d"%(suma) #==================================================================== #PYTHON 1 #==================================================================== numbers = [1,5,2,3,2] suma = 0 for i in numbers: suma += i print "The result is %d"%(suma) #==================================================================== #PYTHON 2 #==================================================================== numbers = [1,5,2,3,2] print "The result is %d"%(sum(numbers)) #! /usr/bin/python #==================================================================== #REGULAR #==================================================================== dx = 0.5 xmin = 1 xmax = 5 N = int((xmax-xmin)/dx) #Function def f(x): return x*(1+x**2)**0.5 #Array x and y=f(x) X = [] Y = [] for i in xrange(0,N,1): X.append(xmin + i*dx) Y.append(f(X[i])) print "Plain: f(%1.1f)=%1.1f"%( X[i], Y[i] ) #==================================================================== #PYTHON #==================================================================== import numpy as np dx = 0.5 xmin = 1 xmax = 5 X = np.arange(xmin,xmax,dx) f = lambda x: x*(1+x**2)**0.5 Y = f(X) for x,y in zip(X,Y): print "Python: f(%1.1f)=%1.1f"%( x, y ) #! /usr/bin/python import numpy as np N = 5 #The set is generated randomly X = np.random.random(N) print "Original set:", X print "Sorted set:", np.sort(X) #! /usr/bin/python import numpy as np N = 10 #The set is generated randomly X = 100*np.random.random(N) Y = X[ X>=X.mean() ] print "Original set", X print "Numbers greater than %1.2f:"%(X.mean()), Y #! /usr/bin/python import numpy as np #All students grades = {"Juanito":2.5, "Pepito":4.8, "Carlitos":3.0, "Maria":3.9, "Pepita":4.6, "Anita":1.9, "Bertulfo":0.9} #Students that approved winners = np.array(grades.keys())[ np.array(grades.values())>=3.0 ] print "The next students approved:", winners #! /usr/bin/python import numpy, math #Prime Checker def primes(upto): primes = numpy.arange(3,upto+1,2) isprime = numpy.ones((upto-1)/2,dtype=bool) for factor in primes[:int(math.sqrt(upto))]: if isprime[(factor-2)/2]: isprime[(factor*3-2)/2::factor]=0 return numpy.insert(primes[isprime],0,2) print "The prime numbers between 1 and 100 are:", primes(100) #! /usr/bin/python N = 5 #Factorial function def factorial(N): if N==1: return 1 else: return N*factorial(N-1) print "The factorial of %d is %d"%(N, factorial(N)) #! /usr/bin/python from __future__ import division import math #Number of terms N = [2,5,10] x = 1.5 #Taylor expansion of cos(x) def taylor_cos( x, N ): term = lambda x,n: (-1)**n/(math.factorial(2*n))*x**(2*n) return np.sum( [term(x,n) for n in xrange(0,N)] ) for n in N: print "Taylor expansion of cos(%1.2f) for %d terms: %1.6f"%( x, n, taylor_cos(x,n) ) print "Exact value of cos(%1.2f): %1.6f"%( x, math.cos(x) )