reasonable balance among
nitty-gritty practical programming instruction:
… I just sat down in front of a text editor, with nothing but thoughts, and ended up with a program that did exactly what I wanted it to a few hours later … (ankit panda)
interesting applications
Hello, world (always the first program you write in a new computer language)
print('hello, python world!')
## hello, python world!
Python as a fancy calculator (REPL, Read-Evaluate-Print-Loop)
print(62**2*27/5+3)
## 20760.6
reference: Python intro section 3.1.1
code here (bbolker.github.io/math1mp/code/primewalk.py
)
Note:
integrated project management tools
most important: maintain reproducibility; well-defined workflows
=
is the assignment operator (“gets”, not “equals”)<variable> = <value>
variables_like_this
(“snake case”)v
vs. temporary_variable_for_loop
int
), floating-point (float
), complex, Boolean (bool
: True
or False
),print(type(x))
for different possibilities (x=3
; x=3.0
; x="a"
)x=a
?Examples
x=3
y=3.0
z="a"
q=complex(1,2)
type(x+y) ## mixed arithmetic
type(int(x+y)) ## int(), float() convert explicitly
type(x+z)
type(q)
type(x+q)
type(True)
type(True+1) ## WAT
[^2](As Dive into Python says in a similar context, “Ew, ew, ew! Don’t do that. Forget I even mentioned it.”)
Check out the Python tutor for these examples
**
)-
)*
,/
,//
=integer division,%
=remainder (“modulo”))+
, -
(“binary”))Use parentheses when in doubt!
Puzzle: what is -1**2
? Why?
==
, !=
)>
, <
, >=
, <=
,and
, or
, not
)not(a and b)
equals (not a) or (not b)
a = True; b = False; c=1; d=0
a and b
not(a and not b)
a and not(b>c)
a==c ## careful!
not(d)
not(c)
operator precedence
not
has higher precedence than and
which has higher precedence than or
. When in doubt use parentheses …From CodingBat:
We have two monkeys, a and b, and the parameters
a_smile
andb_smile
indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. ReturnTrue
if we are in trouble.
monkey_trouble(True, True) → True
monkey_trouble(False, False) → True
monkey_trouble(True, False) → False
A | B | A and B | A or B | not A |
---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
The logical expression: not not a and not b or a
is equivalent to ((not (not a)) and (not b)) or a
since the operator not
takes precedence over the operators and
and or
.
a = True
and b = False
this evaluates to True
not not a
is equivalent to a
, we can simplify the expression to just (a and not b) or a
.Can we simplify this further?
What can we do with not a and not b
?
reference: Python intro section 3.1.2
+
concatenates*
replicates and concatenatesin
searches for a substringa = "xyz"
b = "abc"
a+1 ## error
a+b
b*3
(a+" ")*5
b in a
CodingBat problems:
One more useful string operation: len(s)
returns the length (number of characters)
x[:] # everything
x[a:b] # element a (zero-indexed) to b-1
x[a:] # a to end
x[:b] # beginning to b-1
x[a:b:n] # from a to b-1 in steps of n
Strings have lots of methods, for example:
x = "abcdef"
x.upper()
## 'ABCDEF'
x.capitalize()
## 'Abcdef'
x.endswith("f")
## True
x.startswith("qrs")
## False
x.islower()
## True