Download Python Programming: Resources and Basics and more Slides Introduction to Computing in PDF only on Docsity!
Helping You Succeed: Other Resources
• Consultants. ACCEL Lab Green Room
§ Daily office hours (see website) with consultants § Very useful when working on assignments
• AEW Workshops. Additional discussion course
§ Runs parallel to this class – completely optional § See website; talk to advisors in Olin 167.
• Piazza. Online forum to ask and answer questions
§ Go here first before sending question in e-mail
• Office Hours. Talk to the professors!
§ Available in Thurston 102 between lectures
Warm-Up: Using Python
• How do you plan to use Python?
A. I want to work mainly in the ACCEL lab
B. I want to use my own Windows computer
C. I want to use my own Macintosh computer
D. I want to use my own Linux computer
E. I will use whatever I can get my hands on
Operator Precedence
• What is the difference between the following?
• Operations are performed in a set order
§ Parentheses make the order explicit
§ What happens when there are no parentheses?
• Operator Precedence : The fixed order Python
processes operators in absence of parentheses
add, then multiply multiply, then add
Precedence of Python Operators
- Exponentiation : **
- Unary operators : + –
- Binary arithmetic : * / %
- Binary arithmetic : + –
- Comparisons : < > <= >=
- Equality relations : == !=
- Logical not
- Logical and
- Logical or
- Precedence goes downwards § Parentheses highest § Logical ops lowest
- Same line = same precedence § Read “ties” left to right (for all but ) § Example: 1/23 is (1/2)
- Section 2.7 in your text
- See website for more info
- Major portion of Lab 1
Variables and Assignment Statements
• Variables are created by assignment statements
§ Create a new variable name and give it a value x = 5
• This is a statement , not an expression
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
• Assignment statements can have expressions in them
§ These expressions can even have variables in them x = x + 2 the value the variable the expression the variable x 5 Two steps to execute an assignment:
- evaluate the expression on the right
- store the result in the variable on the left “gets”
Execute the statement: x = x + 2
- Draw variable x on piece of paper:
- Step 1: evaluate the expression x + 2 § For x, use the value in variable x § Write the expression somewhere on your paper
- Step 2: Store the value of the expression in x § Cross off the old value in the box § Write the new value in the box for x
- Check to see whether you did the same thing as your
neighbor, discuss it if you did something different.
x 5 7 A: I did it correctly! B: I drew another box named x C: I did something else D: I did nothing—just watched
Execute the statement: x = 3. * x + 1.
- You now have this:
- The command: § Step 1: Evaluate the expression 3. * x + 1. § Step 2: Store its value in x
- This is how you execute an assignment statement § Performing it is called executing the command § Command requires both evaluate AND store to be correct § Important mental model for understanding Python
x ✗ 5 ✗ 7 22.
Exercise: Understanding Assignment
- Add another variable, interestRate, to get this:
- Execute this assignment:
interestRate = x / interestRate
- Check to see whether you did the same thing as your
neighbor, discuss it if you did something different.
x ✗ 5 ✗ 7 22. interestRate 4 5.
A: I did it correctly! B: I drew another box called “interestRate” C: I stored the value in the box for x D: I thought it would use int division E: I did something else (or nothing)
Dynamic Typing
• Python is a dynamically typed language
§ Variables can hold values of any type § Variables can hold different types at different times § Use type(x) to find out the type of the value in x § Use names of types for conversion, comparison
• The following is acceptable in Python:
x = 1 x = x / 2.
• Alternative is a statically typed language (e.g. Java)
§ Each variable restricted to values of just one type ç x contains an int value ç x now contains a float value type(x) == int x = float(x) type(x) == float
String: Text as a Value
• String are quoted characters
§ 'abc d' (Python prefers) § "abc d" (most languages)
• How to write quotes in quotes?
§ Delineate with “other quote” § Example : " ' " or ' " ' § What if need both " and '?
• Solution : escape characters
§ Format: \ + letter § Special or invisible chars Char Meaning ' single quote " double quote \n new line \t tab \ backslash Type : str
String are Indexed
• s = 'abc d'
• Access characters with []
§ s[0] is 'a' § s[4] is 'd' § s[5] causes an error § s[0:2] is 'ab' (excludes c) § s[2:] is 'c d'
• Called “string slicing”
• s = 'Hello all'
• What is s[3:6]?
a b c d
H e l l o
a
l
l
A: 'lo a' B: 'lo' C: 'lo ' D: 'o ' E: I do not know
CORRECT
String are Indexed
• s = 'abc d'
• Access characters with []
§ s[0] is 'a' § s[4] is 'd' § s[5] causes an error § s[0:2] is 'ab' (excludes c) § s[2:] is 'c d'
• Called “string slicing”
• s = 'Hello all'
• What is s[:4]?
a b c d
H e l l o
a
l
l
A: 'o all' B: 'Hello' C: 'Hell' D: Error! E: I do not know
Strings have many other powers
s = 'abracadabra' 'a' in s == True 'cad' in s == True 'foo' in s == False s.index('a') == 0 s.index('rac') == 2 s.count('a') == 5 len(s) == 11 s.strip('a') == ‘bracadabr’ ' cs1110 '.strip() == 'cs1110' s 1 in s 2 asks whether s 1 is a substring of s 2. Result is type bool. s 1 .index( s 2 ) returns the index of the first occurrence of s 2 in s 1. s 1 .count( s 2 ) returns the number of occurrences of s 2 in s 1. len( s ) returns the number of characters in s. s 1 .strip( s 2 ) returns a copy of s 1 with characters in s 2 removed from the ends. Just s 1 .strip() defaults to removing white space from the ends. More (too much!) information in Python documentation on www.python.org (see Library Reference, built-in types)