350 likes | 369 Views
Learn fundamental Python concepts like functions, variables, boolean operators, and debugging techniques. Includes practical examples and exercises. Great for beginners and aspiring programmers.
E N D
Computational Modeling Part 1 Professor Martin Mason AAD 2006, 2008, 2010 AD 2011
AKA: RIM-2; Terrier. Gross mass: 1,392 kg (3,068 lb). Payload: 110 kg (240 lb). Height: 8.08 m (26.50 ft). Diameter: 0.34 m (1.11 ft). Span: 1.59 m (5.21 ft). Thrust: 22.50 kN (5,058 lbf). Apogee: 30 km (18 mi). First Launch: 1952.11.07. AKA: Hawk; Improved Orion Gross mass: 400 kg (880 lb). Height: 5.60 m (18.30 ft). Diameter: 0.35 m (1.14 ft). Thrust: 7.00 kN (1,574 lbf). Apogee: 85 km (52 mi). First Launch: 1974.05.29.
Who uses Python? • JPL/ NASA uses it for glue code. • ILM for animation tracking • Google • MIT uses for introductory programming • YouTube • NYSE transaction system Why Python?
>>> 4 + 13 >>> 17 / 10 >>> -17 / 10 What is 3 / 9 + 2 * 4 / 12? Arithmetic
>>> 17.0 / 10.0 >>> 17.0 / 10 >>> -17 / 10. Types: Int and Float >>> 6 * 4 ** 2 % 3
>>> degrees_celsius = 26 >>> 100 – degrees_celsius >>> difference = 100 – degrees_celsius >>> a = 20>>> b = 2 * a>>> b>>> a = 5>>> b >>> x = 1>>> x = x + 1>>> x Variables and Assignment
>>> 3 + something NameError: name 'something' is not defined >>> 2 + SyntaxError: invalid syntax Error Messages
Define a variable called degrees_F and set it equal to 212 • Write a script line that uses the variable degrees_F to calculate the temperature in degrees_C Simple Script
>>> vector1 = [1,2,3] • >>> vector2 = [2,2,2] • >>> vector1[0] • >>> vector1[1] • >>> vector1[0] + vector2[0] • >>> vector1 + vector2 List Data Types
def toCelsius(degrees_F): return (degrees_F-32) * 5.0 / 9.0 • >>>toCelsius(80)>>>toCelsius(78.8)>>>toCelsius(10.4) • def <name>(<parameters>): <body> • return <expression> • toCelsius(32) + toCelsius(41) Functions
def polynomial(a, b, c, x): first = a * x * x second = b * x third = c return first + second + thirdprint polynomial(2, 3, 4, 0.5)print polynomial(2, 3, 4, 1.5) • print first • print polynomial(2,3,4) Ax2 + Bx + C Local Variables
>>>from math import * • Many libraries are available in python. >>> import math Built in Functions
Write a function takes as input an angle in degrees and returns an angle in radians. • Extra challenge: Write a function using only what we have used so far compute the roots of a quadratic polynomial Simple Function
>>> a = True>>> b = False>>> print not a>>> print a and a>>> print a and b>>> print a or b >>> print not (b or b) and (a or b) Boolean Operators
>>>print 45 > 34>>>print 23.1 <= 23>>>print 67.3 != 87>>>print 67.0 == 67 >>> x = 3>>> print (1 < x) and (x <= 5)>>> print not (x > 2) or (x <=4) and ((x ==3) or (x>=5) Relational Operators
>>>input(“Enter your age ”) • >>>age = Input(“Enter your age “) • >>>input(“Enter your name “) • >>>raw_input(“Enter your name “) • x = input("Enter a number between 0 and 10: ") ans = input("Enter an expression ") print ans User Input
Make sure that your know what the program is supposed to do • Repeat the failure • Divide and conquer • Change one thing at a time, for a reason • Keep records • Use print statements through your code. • Comment out code until it does what you expect. • When all else fails, start fresh and copy and paste code sections one at a time. Enbugging and Debugging
fv = input("Enter the amount to be received in the future: ") r = input("Enter the rate of return (e.g. 0.05 for 5 percent): ") n = raw_input("Enter the number of years: ") # calculate the pvfactor = 1 / (1+r)^n pvfactor = 1 / (1+r) * n # calcualte the pv = fv * pvfactor pv = fv * pvfactor # output the present value print "That's worth $", pv, "today." Debugging Example
defcheck_wisdom(age): if age > 40: print "Wow! You must be very wise!“ • if <condition>: <body> If Statements
import math def quadRoots(a,b,c): discRoot = math.sqrt(b*b - 4 * a* c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) return [root1 , root2] >>>quadRoots(1,0,-4) Revisiting Polynomial Roots
>>>quadRoots(1,2,3) defquadRoots(a,b,c): discrim = b*b - 4 * a* c if discrim >=0: discRoot = math.sqrt(discrim) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) return [root1 , root2] else: return[] Polynomial Roots Flaw
Polynomial Roots Flaw quadRoots(1,2,1) Modify the code so that if there is only a single root, it returns just that root def quadRoots(a,b,c): discrim = b*b - 4 * a* c if discrim >=0: discRoot = math.sqrt(discrim) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) return [root1 , root2] else: return[]
If the score is bigger then 90 the grade is an A If the grade is not an A and the score is bigger then 80 the grade is a B. If the grade is not an A or a B and the score is bigger then 70 the grade is a C. If the grade is not an A, B or C and the score is larger then 60 the grades is D. If none of the above are true, the grade is an F. PsuedoCode and Comments
defletterGrade(score): if score >= 90: letter = 'A' else: # grade must be B, C, D or F if score >= 80: letter = 'B' else: # grade must be C, D or F if score >= 70: letter = 'C' else: # grade must D or F if score >= 60: letter = 'D' else: letter = 'F' return letter Multiway decisions
defletterGrade(score): if score >= 90: letter = 'A' elifscore >= 80: letter = 'B' elifscore >= 70: letter = 'C' elifscore >= 60: letter = 'D' else: letter = 'F' return letter Multiway decisions with elif
A person is eligible to be a US Senator who is at least 30 years old and has been a US citizen for at least 9 years. A person is eligible to be a US Representative who is at least 25 years old and has been a US citizen for at least 7 years. Write a function that takes as input age and length of citizenship and prints out just the one of the following three statements that is accurate: • You are eligible for both the House and Senate. • You eligible only for the House. • You are ineligible for Congress. Putting it together
for <var> in <sequence>: <body> • for i in [0, 1, 2, 3]: print I • for odd in [1, 3, 5, 7, 9]: print odd * odd Definite Loops
>>>range(10) • >>>range(5,10) • >>>range(5,10,2) • def compoundInterest(principal, rate, time): for i in range(time): principal = principal * (1 + rate) print principal return round(principal, 2)print compoundInterest(1000, 0.05, 10)print 1000 * (1.05)**10 • def printDivisors(x): for i in range(1, x): if x % i == 0: print i, Using the range function
Write a program that tells the user whether or not the number they have entered is a prime. Hint: think about what can be said about the smallest factor if the number is prime. Some really big primes you could try with your program are 1,299,709 and 15,485,861. Putting it all together
Save your programs as script files *.py • Write psuedocode first. • Use your psuedocode as your comments. • Split your code across multiple programs. You can import your own functions into your programs. • import math defdegToRad(degrees): return (math.pi * degrees / 180.) • from degrees import degToRad defarcLength(angle,radius): return (angle*radius) angle = input ("Enter an angle in degrees ") radius = input("Enter the radius of the circle ") print "arclength = ",(arcLength(degToRad(angle),radius)) Making your life easier
Angle Unit Conversion • Coordinate Systems and time • Visual Functions • Collaboration vs. Plagiarism Homework