0% found this document useful (0 votes)
2 views46 pages

(Lecture 1) Abstraction, Hello Python, Memory Models

The document outlines the fundamentals of computing, focusing on abstraction, programming in Python, and memory models. It discusses the importance of interfaces and operating systems in programming, as well as how to write and evaluate expressions in Python. Additionally, it covers variable assignment and reassignment, emphasizing the memory model associated with these operations.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views46 pages

(Lecture 1) Abstraction, Hello Python, Memory Models

The document outlines the fundamentals of computing, focusing on abstraction, programming in Python, and memory models. It discusses the importance of interfaces and operating systems in programming, as well as how to write and evaluate expressions in Python. Additionally, it covers variable assignment and reassignment, emphasizing the memory model associated with these operations.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Review

• Data Science
• Drawing useful conclusions from data using computation

• Computer Science
• Making computers do what you want to do efficiently (for your productivity)

• Algorithms and Programs


• Logical steps (recipe)
• Written in a computer language (instruction set)

1
Computing Bootcamp SNU Graduate School of Data Science 1
Computing Bootcamp

Abstraction

Lecture 1-1

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 2
How does a computer run a Python program?

Computing Bootcamp SNU Graduate School of Data Science 3


Abstraction – Car

Interface

Implementation

Don’t worry about it J

Computing Bootcamp SNU Graduate School of Data Science 4


Abstraction
• Drivers do not need to know implementation details about a car to drive it
• Tire, wheel, engine, size, weight

• They just need to deal with user interfaces, such as handle and pedals
• Once they know how to handle interfaces, they can drive all of various cars in the
world

• Abstraction: The process of preserving information that is relevant in a


given context, and forgetting information that is irrelevant in that context
• We cannot remember and focus on many things at a time …
• People with different expertise/interest can focus on their jobs without worrying
about other part

Computing Bootcamp SNU Graduate School of Data Science 5


Abstraction – Machine Learning

https://www.youtube.com/watch?v=CS4cs9xVecg&list=
PLkDaE6sCZn6Ec-XTbcX1uRg2_u4xOEky0

Computing Bootcamp SNU Graduate School of Data Science 6


Abstraction – Mobile Phone

Application programmers do not need to know


various hardware details thanks to…

Computing Bootcamp SNU Graduate School of Data Science 7


Abstraction – Mobile Phone
Application
Software

Interface

Operating
System

Implementation

Hardware

Computing Bootcamp SNU Graduate School of Data Science 8


Abstraction – More General Computer
Application
Software

Application programmers do not need to know


Operating various hardware details thanks to…
System

Hardware

Computing Bootcamp SNU Graduate School of Data Science 9


Abstraction – More General Computer
Application
Software

Interface
Operating
System
Implementation

Hardware

Computing Bootcamp SNU Graduate School of Data Science 10


Abstraction – Thank You Operating System!
• Operating system (e.g., MS Windows, Linux, macOS) is the only program
on the computer that’s allowed direct access to the hardware

• If any other application program wants to interact with hardware (fetch


data from storage, draw on the screen, find out what key was just pressed
on the keyboard), it sends a request to the OS (indirect access)

• If you are not writing an OS, you don’t need to care about hardware but OS
• Some smartphone Apps support either Android or iOS, not both of them
• Once an app is ported on Android, it works for all different kinds of Android
phones

Computing Bootcamp SNU Graduate School of Data Science 11


Abstraction – Thank You Interpreter!
• OS is still too low and complex to directly
program an application on…
Python
• If you are not a hardcore programmer

• Another layer for you, called Interpreter Application Python


• If you write a program using Python, the Interpreter
interpreter takes your program, translate it into
a language that OS understands
Operating System

• Now you don’t have to worry about OS too!


Just write a program and it will run on
various OSes! Storage Screen

Computing Bootcamp SNU Graduate School of Data Science 12


Python Interpreter
• Multiple ways of playing with Python
• Execute a Python program that is saved in a file with a .py extension
• The interpreter will execute the whole program in the file

• Execute a program called a shell, and type Python statements one at a time
• The interpreter will execute each statement when you type it

• Execute jupyter notebook, type and execute a group of Python statements


• The interpreter will execute the group of statements

• With the Python interpreter, you only care about Python, neither OS nor
hardware

Computing Bootcamp SNU Graduate School of Data Science 13


Summary
• Abstraction – Interface and implementation
• For now, it is OK to know only interfaces Python
• To become a power user, you need to learn low-
level system programming!
Application Python
Interpreter
• Operating system and application

Operating System
• Python Interpreter

• This figure Storage Screen

Computing Bootcamp SNU Graduate School of Data Science 14


Computing Bootcamp

Hello, Python!

Lecture 1-2

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 15
Let’s start programming!

Computing Bootcamp SNU Graduate School of Data Science 16


Your First Program
• Open a Jupyter cell

• Let’s type a simple mathematical expression and execute the code (press
Ctrl+Enter or click Run)
• >>> 3 + 4
• 7

• Congratulation! You wrote a code and it works!

Computing Bootcamp SNU Graduate School of Data Science 17


Primitive Expressions
• What you just typed is an expression including
• Operators: +, -, *, /, %, //, **
• Operands: Values that an operator takes

• An expression does not have to have an operator, a single value is also an


expression

• Evaluation: When you type an expression and run it, Python evaluates
the expression, produces a value, and shows it

Computing Bootcamp SNU Graduate School of Data Science 18


Primitive Expressions
• Operators precedence
• **
• - (negation)
• *, /, //, %
• +, -
• Example
• -2 ** 4 -16
• -(2 ** 4) -16
• (-2) ** 4 16

• Type whatever mathematical expressions for fun and see how Python
evaluates them!

Computing Bootcamp SNU Graduate School of Data Science 19


Types
• Every value in Python has a particular type
• int (integer): 1, 4, 8, 10, 100 …
• float (floating point): 2.5, 19.2, 7.1 …

• An expression having two floats produces a float


• 100.0 – 25.0 75.0

• An expression having an int and a float makes Python convert the int to a
float
• 100 - 25.0 75.0

Computing Bootcamp SNU Graduate School of Data Science 20


Types
• A type in Python consists of two things
• A set of values
• A set of operations that can be applied to those values

• int: (1) integer numbers, (2) arithmetic operators can be applied


• float: (1) a subset of the real numbers, (2) arithmetic operators can be
applied

• Finite precision: Computer does not represent all the real numbers due to
its limited memory, representing the closest value it can produce
• 2/3 0.66666666666
• This is memory efficient and allows fast calculation

Computing Bootcamp SNU Graduate School of Data Science 21


Call Expressions
• Operator can be a function (there are many functions that Python provides)

• Call expression: An expression that calls a function

• Example
• max(2,3)
• Operators: max – a function name
• Operands: 2 and 3 – again, values that an operator takes

• An operand can also be a call expression


• max( min(2, 3) , min(9, 10) )

Computing Bootcamp SNU Graduate School of Data Science 22


Variables and Names
• Values do not have any meaning, so now we want to name them: Variable

• Name
• Letters, digits, and the underscore symbol
• Cannot start with a digit: 7ab
• Case sensitive: GSDS vs. gsds
• No empty space
• Naming properly is very important! value

• Reserved word (or keyword) variable


• A name that Python already uses: True, False, if, for …
• You cannot use these words as your own names

Computing Bootcamp SNU Graduate School of Data Science 23


Assignment
• You can create a new variable by naming it and assigning it a value
• temp_celsius = 31.0 (assignment statement)
name value
• “temp_celsius is assigned the value 31.0.”
• “=” is not equality!

• You can do assignment for multiple variables on a single line


• x, y = 1, 2
• y, x = x, y

Computing Bootcamp SNU Graduate School of Data Science 24


Assignment
• When Python sees a variable in an expression, it uses its assigned value
• temp_celsius + 5/10 31.5

• We can reassign another value to an existing variable (yes, it is “variable”!)


• temp_celsius = -15.2

Computing Bootcamp SNU Graduate School of Data Science 25


Summary
• Expression and evaluation
• Operator and operand
• Value and type
• Name and variable

• Assignment

Computing Bootcamp SNU Graduate School of Data Science 26


Computing Bootcamp

Memory Model and


Reassignment
Lecture 1-3

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 27
What happens in the computer
when you execute an assignment statement?

Computing Bootcamp SNU Graduate School of Data Science 28


Memory Model
• A memory object
• Address
• Value

• Example
• The object at the memory address id1 has type float and the value 31.0
• The object at the memory address id2 has type function and max

id1: float id2: function

31.0 max()

Computing Bootcamp SNU Graduate School of Data Science 29


Memory Model – Assignment
• <<variable>> = <<expression>>
• Step 1: Evaluate the expression on the right side to produce a value. This value is
stored in a memory object
• Step 2: Store the address of the memory object (containing the value above) in
the variable on the left side
• If the variable already exists, replace the memory address that it contains
• Result: the variable points the memory where the value is stored

• Example: temp_celsius = 31.0


id1: float

temp_celsius id1 31.0 temp_celsius 31.0

Computing Bootcamp SNU Graduate School of Data Science 30


Memory Model – Assignment
• <<var1>>, <<var2>>, …, <<varN>> = <<exp1>>, <<exp2>>, …, <<expN>>

Computing Bootcamp SNU Graduate School of Data Science 31


Memory Model – Reassignment (1)
• >>> advisor = “Hyung-Sin Kim”
• >>> advisor 942-416

• “Hyung-Sin Kim”

advisor 942-416

Computing Bootcamp SNU Graduate School of Data Science 32


Memory Model – Reassignment (1)
• >>> advisor = “Hyung-Sin Kim”
• >>> advisor 942-416

• “Hyung-Sin Kim”
• >>> advisor = “Minhwan Oh”

advisor 942-416

Computing Bootcamp SNU Graduate School of Data Science 33


Memory Model – Reassignment (1)
• >>> advisor = “Hyung-Sin Kim”
• >>> advisor 942-416

• “Hyung-Sin Kim”
• >>> advisor = “Minhwan Oh”

advisor 942-419
942-416
942-419

Computing Bootcamp SNU Graduate School of Data Science 34


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0
• >>> temp_celsius = temp_celsius – 2*difference
• >>> difference = 5.0
• >>> temp_celsius
• ?

Computing Bootcamp SNU Graduate School of Data Science 35


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 31.0
• >>> temp_celsius = temp_celsius – 2*difference
• >>> difference = 5.0
• >>> temp_celsius
• ?

Computing Bootcamp SNU Graduate School of Data Science 36


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
• >>> difference = 5.0
• >>> temp_celsius
• ?

Computing Bootcamp SNU Graduate School of Data Science 37


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
10.0
• >>> difference = 5.0
• >>> temp_celsius
• ?

Computing Bootcamp SNU Graduate School of Data Science 38


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
difference 10.0
• >>> difference = 5.0
• >>> temp_celsius
• ?

Computing Bootcamp SNU Graduate School of Data Science 39


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
difference 10.0
• >>> difference = 5.0
• >>> temp_celsius 11.0
• ?

Computing Bootcamp SNU Graduate School of Data Science 40


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
difference 10.0
• >>> difference = 5.0
• >>> temp_celsius 11.0
• ?

Computing Bootcamp SNU Graduate School of Data Science 41


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
difference 10.0
• >>> difference = 5.0
• >>> temp_celsius 11.0
• ?
5.0

Computing Bootcamp SNU Graduate School of Data Science 42


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
difference 10.0
• >>> difference = 5.0
• >>> temp_celsius 11.0
• ?
5.0

Computing Bootcamp SNU Graduate School of Data Science 43


Memory Model – Reassignment (2)
• >>> temp_celsius = 31.0
• >>> difference = 10.0 temp_celsius 31.0
• >>> temp_celsius = temp_celsius – 2*difference
difference 10.0
• >>> difference = 5.0
• >>> temp_celsius 11.0
• ?
• 11.0 5.0

Computing Bootcamp SNU Graduate School of Data Science 44


Summary
• Memory model

• Each value dwells in a memory box

• A variable points at the memory box where its assigned values lives

• Reassignment is pointing at a different memory box

Computing Bootcamp SNU Graduate School of Data Science 45


Thanks!

Computing Bootcamp SNU Graduate School of Data Science 46

You might also like