Weeks 1 and 2 Notes
Weeks 1 and 2 Notes CS 1110-002
Popular in Intro to Computing using Python
verified elite notetaker
Popular in ComputerScienence
This 5 page Class Notes was uploaded by Eunice on Tuesday January 19, 2016. The Class Notes belongs to CS 1110-002 at Cornell University taught by Lee, Van Loan in Spring 2016. Since its upload, it has received 171 views. For similar materials see Intro to Computing using Python in ComputerScienence at Cornell University.
Reviews for Weeks 1 and 2 Notes
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 01/19/16
CS 1110 Introduction to Python Lee and Van Loan Spring 2016 January 28, 2016 Assignment Statement and Types o Python’s Interactive Mode: ex. >>> r = 10 >>> A = 3.14*r**2 >>> print A o Variables, Expression, Assignments r and A are variables define variable: a named memory location, like a box, holds a value define = : assignment statement variables are used in an expression o expression: evaluated and then stored under assigned variable o ex. 3.14*r**2 order is important variable must be assigned a value before it is used and evaluated in an expressed o >>> A = 3.14*r**2 o >>> r = 10 o NameError: name ‘r’ is not defined Assigning is different from “equal to” o >>>3.14*r**2 = A o SyntaxError: can’t assign to an operator Difference between math and python Overwriting works >>> r = 10 >>> A = 3.14*r**2 >>> A = A/2 <variable name> = <expression> Evaluate right item and store it in left item Rules of naming variables Name must be digits, uppercase letters, low care letters and _ (underscore) Must begin with a letter or _ Expressions: use parentheses if there is ambiguity o Types Python views numbers as a type int: integer, integer arithmetic is exact float: float arithmetic is usually inexact literals Int: ex. 12 Float: ex. 12.0 Str: ex. abc Operations Int: + - * / unary- ** % Float: + - * / unary- ** Str: + o Ints and floats Integers v. decimals and python arithmetic Integer division: A/B = Q and R (a remainder) o >>> r = x%y o r is printed as a remainder >>> x = 30 >>> y = 8 >>> q = int(x)/int(y) >>> print q 3 >>> q = x/y >>> print q 3 Decimal division: A/B = Q (a decimal) >>> x = 30 >>> y = 8 >>> q = float (x)/ float (y) >>> print q 3.75 Clock arithmetic Float can represent an integer even though it is not an integer type o Strings Strings: quoted characters >>> s1 = ‘abc’ >>> s2 = ‘ABC’ >>> s3 = ‘ A B C ‘ s1, s2, s3 are variables a single character is a string define subscripting: characters in a string can be referenced through their indices (USE BRACKETS) o >>> s = ‘The Beatles’ o >>> t = s o >>> print t o B “t is a slice of s” o >>> s = ‘The Beatles’ o >>> t = s[4:8] o >>> print t o Beat o >>> s = ‘The Beatles’ o >>> t = s[4:] o >>> print t o Beatles o >>> s = ‘The Beatles’ o >>> t = s[:4] o >>> print t o The Concatenation: string analog of addition (unlimited) o >>> s1 = ‘The’ o >>> s2 = ‘Beatles’ o >>> s = s1+s2 o >>> print s o TheBeatles o >>> s1 = ‘The’ o >>> s2 = ‘Beatles’ o >>> s = s1+’ ‘+s2 o >>> print s o The Beatles Data is provided often in strings and must be converted >>> s= ‘123.45’ >>> x = 2*float(s) >>> print x 246.90 February 2, 2016 two modes in python o script vs interactive o code to be run (script) is entered into a file (module) o .py file suffix Example: windchill calculation o Compute windchill temperature given temperature, wind speed o Define variables and the model, print the result “ “ “computes wind chill as a function of wind (mph) and temp (Fahrenheit).” “ “ Temp = input(‘Enter temp (Fahrenheit):’) # temperature in Fahrenheit Wind = inpute(‘Enter wind speed (mph):’) # wind speed in miles-per-hour A= 35.32; B=.6215; C=-35.74; D.42275; e=.16 # model parameters wc=(A+B*temp) + (C+D*Temp)*Wind**e # compute and display the wind chill Print wc o Comments begin with a “#”, guidelines o Docstrings: three quotation marks “”” o To print with formatting Print ‘ windchild:%4.Of’ [CHECK SLIDES] o Import functions from modules Ex. sqrt function from math module From math import sqrt, pi Practice with demo files ShowInput.py ShowRawInput.py Begin all modules o Name of module (#something.py) o Name and net id (#name netid) o Date (#December 25, 2016) Conditional execution o If-Else a=input(‘Enter a pos float:’) b=input(‘Enter a pos float:’) aTob=a**b bToa=b**a if aTob>bToa: #aTob>bToa is a boolean expression which is either True or False print aTob Else: Print bToa o The punctuation and indentation are essential o You do not need variables, you can have expressions If boolean expression: Statements to execute if the expression is True Else: Statements to execute if the expression is False February 4, 2016 Conditional execution o Remember, indentation is important If something isn’t indented, it’s a new statement/task that will be executed o When comparing two expressions to see if they have exactly the same value, use == = is an assignment == is a comparison o < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (is equal to), != (not equal to) No spaces Alphabetical/string comparison: Comes first in alphabet: < capital letter is < a lower case letter 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij klmnopqrstuvwxyz o If-Elif-Else construction Elif: “else if” How it works o An input is evaluated by each condition, until it reaches a comparison that verifies as True o Once a condition is evaluated as True, it stops and executes the statement If boolean expression: Statements to execute if this expression is True Elif another Boolean expression: Statements to execute if this expression is True Else: Statements to execute if all above expressions are False “Elif”s are unlimited len function o N = len(string) N now has been assigned the value of the length of the string Finding a solution: deriving by stepwise refinement o Reformat the task (use pencil and paper and write out a potential solution in stylized English) Ex. hyphenator Read in the string Compute its length If the length is even Hyphenate in the middle Else Hyphenate around the middle character o Refine (USE COMMENTS) h = input (‘Enter a string: ‘) n = len(s) If n%2==0: # s has even length m = n/2 h = s[:m] + ‘-‘ + s[m:] Else: # s has odd length m = n/2 h = s[:m] + ‘-‘ + s[m] + ‘-‘ + s[m+1:] Logical operators
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'