Programming is a skill
x
is y
such that y * y
is x
.g
g * g
is close enough to x
, stop and say g
is the answer.g
and x / g
.A computer only does what you tell it to do.
Alan Turing proved that one can compute anything with just 6 primitive instructions. He imagined a tape with values and 6 instructions that would act on the tape: left (move the tape left 1), right (move the tape right 1), print (print a symbol on the current section), scan (identify any symbols on the current square), erase (remove everything on the current section), and nothing (do nothing).
Real programming languages have a more convenient set of primitives and ways to combine primitives to make new primitives.
Anything computable in one language is computable in any other programming language (if it has these 6 primitives in it)
English: Words
Python: Numbers, strings, simple operators
English: "cat dog boy" is not syntactically valid
"cat hugs boy" is syntactically valid
Programming Language: "hi"5
is not syntactically valid
"hi" * 5
is syntactically valid
English: "I are hungry" syntactially valid but static semantic error Programming Language: "hi" + 5
is syntactially valid but static semantic error
Programs have only one meaning, but the meaning may not be what programmer intended.
Syntactic errors: Common and easily caught.
Static Semantic Errors: Some languages check for these before running the program, and they cause unpredictable behavior.
Even if there are no "linguistic" errors, there can be different meanings than what the programmer intended; the program could stop running before reaching its goal, it could run forever, it could give a wrong answer.
A program is a sequence of definitions and commands
- Definitions evaluated
- Commands executed by Python interpreter in a shell
- Commands (statements) instruct interpreter to do something
- Can by typed directly in a shell or stored in a file that is read into the shell and evaluated
30
"Word"
8.3
, 2
True
, False
"abc"
int
— represents integers, (5
, -100
)float
— represents floating point numbers (3.27
, 2.0
)bool
— represents Boolean values (True
and False
)NoneType
— Special and has one value, None
type()
shows type of an objcetsTrue
and False
) and only one NoneType (None
) This does not change the object, it makes a new object (for example, if you have integer 3
in memory, casting it to a float does not change the integer 3
but rather creates a new float 3.0
in memory).
float(3)
casts the int 3
into float 3.0
int(3.9)
casts (note the truncation!) the float 3.9
to int 3
Some operations perform implicit casts:
round(3.9)
returns the int 4
Combine objects and operators to form expressions.
- 3 + 2
- 5 / 3
An expression has a value, which as a type.
- 3 + 2
has value 5
and type int
- 5 / 3
has value 1.6666667
and type float
Python evaluates experssions and stores the value . It doesn't store expressions.
Syntax for a simple expression:
<object> <operator> <object>
Computations work left-to-right. Parentheses override left-to-rightness
int
and float
i + j
— the sum (if both i
and j
are ints, result is int. Otherwise, result is float)i - j
— the difference (if both i
and j
are ints, result is int. Otherwise, result is float)i * j
— the product (if both i
and j
are ints, result is int. Otherwise, result is float)i / j
— division (result is always a float)i // j
— truncates the division to a float ( result is always an integer)i % j
— the remainder when i
is divided by j
(result is always an integer)i ** j
— i
to the power of j
(if both i
and j
are ints, result is int. Otherwise, result is float)pi = 3.141592653594230985703
because we don't want to write out 3.141592653594230985703
every time we use pi.
Math Variables: Are abstract and can represent many valuables (i.e. a + 2 = b - 1)
CS Variables: Is bound to one single value at a given time and can be bound to an expression. (i.e. m = 10
, F = m * 9.98
. In this case, m = 10 and F = 99.8)
In Computer Science, an equal sign ( "=") is an assignment. One value to one variable. An equal sign is not equality, not “solve for x”.
An assignment binds a value to a name.
- Step 1: Compute the value on the right-hand side (the value), which is then stored in computer memory.
- Step 2: Store it (bind it) to the left-hand side (the variable).
Choose variable names wisely. Code should be readable in a year (and longer) by you and others.
python
pi = 355/113
radius = 2.2
area = pi * (radius**2)
circumference = pi * (radius*2)
Comments tell others (and yourself) what your code is doing. They start with a #
.
```python
message = "This is code"
```
```python
a = 335/113 * (2.2*2)
c = 335/113 * (2.2 2)
```
This is pretty bad code.
```python
p = 355/113
r = 2.2
a = p(r*2)
c = p(r2)
```
This is ok code.
```python
pi = 355/113
radius = 2.2
area = pi * (radius**2)
circumference = pi * (radius * 2)
```
This is good code.
You can rebind variable names using new assignment statements. Previous value may still stored in memory but lost the handle for it.