CENG240-2024 Week3 Dive Into Python Part 1
CENG240-2024 Week3 Dive Into Python Part 1
Disclaimer: Figures without reference are from either from “Introduction to programming concepts
with case studies in Python” or “Programming with Python for Engineers”, which are both co-authored
by me.
!
0
24
What does ‘algorithm’ mean?
NG
CE
on
METU Computer Engineering
ly
us
ev
Pr
problem”
“A set of instructions to be followed to
solve a problem”
“an effective method expressed as a finite
ly
ious
ev
Step 1: Get how many numbers will be provided and store that in N
Step 2: Create a variable named Result with initial value 0
Step 3: Execute the following step N times:
Step 4: Get the next number and add it to Result
Step 5: Divide Result by N to obtain the average
!0
24
NG
CE
on algorithms
METU Computer Engineering
ly
ious
ev
Pr
ly
us
Random guessing
Sweeping from beginning
Middle guessing
!
0
24
NG
CE
on Languages
METU Computer Engineering
ly
ious
ev
Pr
ly
ious
ev
Pr
ly
ious
ev
Pr
ly
ious
ev
Pr
REPRESENTATION OF DATA IN
COMPUTERS (CH3)
2021 S. Kalkan - CEng 240 9
Binary Representation of Numeric
!0
24
NG
CE
on Information (continued)
METU Computer Engineering
ly
ious
ly
1st Way:
0.375 0x½ + 1x¼ + 1x1/8 011
2nd Way:
Multiply by 2 and get the integer part until we get ‘0’
after the dot:
0.375 x 2 = 0.750 = 0 + 0.750
0.750 x 2 = 1.500 = 1 + 0.500
0.500 x 2 = 1.000 = 1 + 0.000
0!
24
NG
CE
on Representation
METU Computer Engineering
ly
ious
ev
Pr
! 0
24
NG
CE
on Representation
METU Computer Engineering
ly
us
!
0
24
NG
CE
on Information Loss
METU Computer Engineering
ly
ious
ev
Pr
!
0
24
NG
Decima Binary Val. Hex. Unicode Charac.
l
CE
0x30 0x0030 0
on 48 0011000 0
METU Computer Engineering
0 0x31 0x0031 1
ly
us
1 0x33 0x0033 3
ev
Pr
2020
What is data?
METU Computer Engineering
Integers:
int
Unlimited size
Floating point numbers:
float
IEEE754 standard (32bit, 64bit)
Complex numbers
complex
3+4j
bool type
Can take True or False
str
list Mutable
tuple
dict
set
Immutable
frozenset
Positive indexing
Negative indexing
Slicing
len() Membership
Concatenation (+)
Repetition
Writing strings
single quote
double quote
triple quote
Special characters
Unicode support in v3
str()
len()
Lists: mutable
[10, 20, 30]
[“ali”, 20, “veli”, 30]
[10, [20, [30]], 40]
Tuples: immutable
Deletion
Insertion
Concatenation, repetition
Membership
dict: mutable
Add/delete
elements
KeyError
len()
Membership
values()
keys()
set: mutable
frozenset:
immutable
ACTION
Purposes of actions
Creating/modifying data
Interaction with the environment
Types of actions
Expressions
Statements
Implicit
3 + 4.5
3 + True
Explicit
int(), float(),
bool()
str(), ...
Basic statements
del L[3]
a = 20
pass, del, return, yield, raise
, break, continue, import, futu
re, global, nonlocal.
Compound statements
Conditional statement
Repetition statements
2021 S. Kalkan - CEng 240 44
Statements: Assignment
METU Computer Engineering
Simple assignment
a = 10
Multiple assignment id() function
a = b = c = 10
a, b = 10, 20
Compound assignment
a += 10
a *= 20
Swapping values
a, b = b, a
Arithmetic
operators
Logic operators
Container
operators
Comparison
operators
Data
Basic Data Types
Container Types
Accessing elements of a container type (indexing, negative indexing,
slicing).
Mutable vs. immutable types
Actions
Expressions, statements
Expression evaluation: Operators, precedence, associativity
Basic statements
Assignment