1_Introduction_programmation_Python
1_Introduction_programmation_Python
TODAY
course info
what is computation
python basics
mathematical operations
python variables and types
NOTE: slides and code files up before each lecture
o highly encourage you to download them before lecture
o take notes and run code files when I do
o bring computers to answer in-class practice exercises!
1
12/12/2024
PROBLEM
SOLVING
PRACTICE
KNOWLEDGE PROGRAMMING
OF CONCEPTS SKILL
TOPICS
represent knowledge with data structures
iteration and recursion as computational metaphors
abstraction of procedures and data types
organize and modularize systems using object classes
and methods
different classes of algorithms, searching and sorting
complexity of algorithms
2
12/12/2024
TYPES OF KNOWLEDGE
declarative knowledge is statements of fact.
◦ someone will win a Google
Cardboard before class ends
imperative knowledge is a recipe or “how-to”.
1) Students sign up for raffle
2) Ana opens her IDE
3) Ana chooses a random number between 1st and nth responder
4) Ana finds the number in the responders sheet. Winner!
3
12/12/2024
A NUMERICAL EXAMPLE
square root of a number x is y such that y*y = x
recipe for deducing square root of a number x (16)
1) Start with a guess, g
2) If g*g is close enough to x, stop and say g is the
answer
3) Otherwise make a new guess by averaging g and x/g
4) Using the new guess, repeat process until close enough
g g*g x/g (g+x/g)/2
3 9 16/3 4.17
4.17 17.36 3.837 4.0035
4.0035 16.0277 3.997 4.000002
WHAT IS A RECIPE
1+2+3 = an algorithm!
4
12/12/2024
CONTROL ARITHMETIC
UNIT LOGIC UNIT
program counter do primitive ops
INPUT OUTPUT
5
12/12/2024
STORED PROGRAM
COMPUTER
sequence of instructions stored inside computer
◦ built from predefined set of primitive instructions
1) arithmetic and logic
2) simple tests
3) moving data
BASIC PRIMITIVES
Turing showed that you can compute anything using 6
primitives
modern programming languages have more
convenient set of primitives
can abstract methods to create new primitives
6
12/12/2024
CREATING RECIPES
a programming language provides a set of primitive
operations
expressions are complex but legal combinations of
primitives in a programming language
expressions and computations have values and
meanings in a programming language
ASPECTS OF LANGUAGES
primitive constructs
◦ English: words
◦ programming language: numbers, strings, simple
operators
Word Cloud copyright Michael Twardos, All Right Reserved. This content is excluded from our Word Cloud copyright unknown, All Right Reserved.
Creative Commons license. For more information, see https://ocw.mit.edu/help/faq-fair- This content is excluded from our Creative
use/. Commons license. For more information, see
https://ocw.mit.edu/help/faq-fair-use/.
7
12/12/2024
ASPECTS OF LANGUAGES
syntax
◦ English: "cat dog boy" not syntactically valid
"cat hugs boy" syntactically valid
◦ programming language: "hi"5 not syntactically valid
3.2*5 syntactically valid
ASPECTS OF LANGUAGES
static semantics is which syntactically valid strings
have meaning
◦ English: "I are hungry" syntactically valid
but static semantic
error
◦ programming language: 3.2*5 syntactically
valid
◦ 3+"hi" static semantic error
8
12/12/2024
ASPECTS OF LANGUAGES
semantics is
the meaning associated with a
syntactically correct string of symbols with no static
semantic errors
◦ English: can have many meanings "Flying
planes can be dangerous"
◦ programming languages: have only one meaning
but may not be what programmer intended
9
12/12/2024
PYTHON PROGRAMS
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 be typed directly in a shell or stored in a file that
is read into the shell and evaluated
◦ Problem Set 0 will introduce you to these in Anaconda
OBJECTS
programs manipulate data objects
10
12/12/2024
SCALAR OBJECTS
int – represent integers, ex. 5
float – represent real numbers, ex. 3.27
bool – represent Boolean values True and False
NoneType – special and has one value, None
can use type() to see the type of an object
>>> type(5)
int
>>> type(3.0)
float
11
12/12/2024
PRINTING TO CONSOLE
to show output from code to a user, use print
command
In [11]: 3+2
Out[11]: 5
In [12]: print(3+2)
5
EXPRESSIONS
combine objects and operators to form expressions
an expression has a value, which has a type
syntax for a simple expression
<object> <operator> <object>
12
12/12/2024
SIMPLE OPERATIONS
parentheses used to tell Python to do these
operations first
operator precedence without parentheses
◦ **
◦ *
◦ /
◦ + and – executed left to right, as appear in expression
13
12/12/2024
pi = 3.14159
pi_approx = 22/7
value stored in computer memory
an assignment binds name to value
retrieve value associated with name or variable by
invoking the name, by typing pi
ABSTRACTING EXPRESSIONS
why give names to values of expressions?
to reuse names instead of values
easier to change code later
pi = 3.14159
radius = 2.2
area = pi*(radius**2)
14
12/12/2024
PROGRAMMING vs MATH
in programming, you do not “solve for x”
pi = 3.14159
radius = 2.2
# area of circle
area = pi*(radius**2)
radius = radius+1
CHANGING BINDINGS
can re-bind variable names using new assignment
statements
previous value may still stored in memory but lost the
handle for it
value for area does not change until you tell the
computer to do the calculation again
3.14
pi = 3.14 pi
2.2
radius = 2.2 radius
area = pi*(radius**2) area 3.2
15
12/12/2024
Lecture 1
type(5)
print(3.0-1)
What’s printed?
o int
o 2.0
o nothing
Lecture 1
o x+y=2
o x*x = 2
o 2=x
o xy = 2
16
12/12/2024
Lecture 1
type(5)
print(3.0-1)
What’s printed?
o int
o 2.0
o nothing
3 Bindings
You run the code below from the file editor.
usa_gold = 46
uk_gold = 27
romania_gold = 1
total_gold = usa_gold + uk_gold + romania_gold
print(total_gold)
romania_gold += 1
print(total_gold)
What’s printed?
o 74 then 74
o 74 then 75
o 74
o 75
17