0% found this document useful (0 votes)
17 views

CENG240-2024 Week3 Dive Into Python Part 1

Uploaded by

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

CENG240-2024 Week3 Dive Into Python Part 1

Uploaded by

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

METU Computer Engineering

CEng 240 – Spring 2021


Week 3
Sinan Kalkan

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

“A procedure or formula for solving a


io


ev
Pr

problem”
 “A set of instructions to be followed to

solve a problem”
 “an effective method expressed as a finite

list of well-defined instructions for


calculating a function”
 “step-by-step procedure for calculations”

2021 S. Kalkan - CEng 240 2


!
0
24
Describing algorithms
NG
CE
on
METU Computer Engineering

ly
ious
ev

Option 1: Use pseudo-code descriptions.


Pr

Algorithm. Calculate the average of numbers provided by


the user.
Input: N -- the count of numbers
Output: The average of N numbers to be provided

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

2021 S. Kalkan - CEng 240 3


Describing

!0
24
NG
CE
on algorithms
METU Computer Engineering

ly
ious
ev
Pr

Option 2: Use flow-charts.

2021 S. Kalkan - CEng 240 4


!
0
24
Comparing Algorithms
NG
CE
on
METU Computer Engineering

ly
us

 Rougly count the main number of steps in


io
ev
Pr

terms of , the ‘size’ of the problem.


 Example: Guess my number!

 Random guessing
 Sweeping from beginning
 Middle guessing

2021 S. Kalkan - CEng 240 5


The World of Programming

!
0
24
NG
CE
on Languages
METU Computer Engineering

ly
ious
ev
Pr

2021 S. Kalkan - CEng 240 6


!
0
24
Interpreter vs. Compiler
NG
CE
on
METU Computer Engineering

ly
ious
ev
Pr

2021 S. Kalkan - CEng 240 7


!
0
24
Interpreter vs. Compiler
NG
CE
on
METU Computer Engineering

ly
ious
ev
Pr

2021 S. Kalkan - CEng 240 8


!
0
24
NG
CE
on
METU Computer Engineering

ly
ious
ev
Pr

Two’s complement representation of integers, IEEE floating-point


representation, Information loss with Floating Points, representation of
characters, text and Boolean.

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

 Two’s complement instead of sign-magnitude representation


ev
Pr

 Positive numbers have a leading 0.


 5 => 0101
 The representation for negative numbers is found by subtracting
the absolute value from 2N for an N-bit system:
 -5 => 24 – 5 = 16 – 5 = (11)10 => (1011)2
 Advantages:
 0 has a single representation: +0 = 0000, -0 = 0000
 Arithmetic works fine without checking the sign bit:
 1011 (-5) + 0110 (6) = 0001 (1)
 1011 (-5) + 0011 (3) = 1110 (-2)

2021 S. Kalkan - CEng 240 10


!
0
24
Binary Representation of Real Numbers
NG
CE
on
METU Computer Engineering

ly

Conversion of the digits after the dot into binary:


ious
ev
Pr

 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

2021 S. Kalkan - CEng 240 11


IEEE 32bit Floating-Point Number

0!
24
NG
CE
on Representation
METU Computer Engineering

ly
ious
ev
Pr

• Mx2 E ( 2 − 2−23 ) ×2127


• Exponent (E): 8 bits
• Add 127 to the exponent value before storing it
• E can be 0 to 255 with 127 representing the real zero.
• Fraction (M - Mantissa): 23 bits
• 2128 = 1.70141183 × 1038

2021 S. Kalkan - CEng 240 12


IEEE 32bit Floating-Point Number

! 0
24
NG
CE
on Representation
METU Computer Engineering

ly
us

 Now consider 4.1:  So,


io
ev
Pr

 4 => (100)2  Representing a


 0.1 => fraction which is a
 x 2 = 0.2 = 0 + 0.2

multiple of 1/2n is
x 2 = 0.4 = 0 + 0.4
 x 2 = 0.8 = 0 + 0.8
lossless.
 x 2 = 1.6 = 1 + 0.6  Representing a
 x 2 = 1.2 = 1 + 0.2 fraction which is not
 x 2 = 0.4 = 0 + 0.4

a multiple of 1/2n
x 2 = 0.8 = 0 + 0.8
 …….
leads to precision
loss.
2021 S. Kalkan - CEng 240 13
Representing Real Numbers:

!
0
24
NG
CE
on Information Loss
METU Computer Engineering

ly
ious
ev
Pr

2021 S. Kalkan - CEng 240 14


Binary Representation of Textual Information (cont’d)

!
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

49 0011000 1 0x32 0x0032 2


io

1 0x33 0x0033 3
ev
Pr

50 0011001 2 0x34 0x0034 4


0
0x35 0x0035 5
51 0011001 3
1 0x36 0x0036 6

ASCII 52 0011010 4 0x37 0x0037 7 Unicode


7 bits long
0 0x38 0x0038 8 16 bits long
53 0011010 5 0x39 0x0039 9
1
0x3A 0x003A :
54 0011011 6
0 0x3B 0x003B ;
55 0011011 7 0x3C 0x003C <
1
0x3D 0x003D =
56 0011100 8
0x3E 0x003E >
0 Partial
0x3F 0x003F ?
57 0011100 9 listings
1 0x40 0x0040 @
only!
58 0011101 : 0x41 0x0041 A
0
0x42 0x0042 B
59 0011101 ;
1
2021 S. Kalkan - CEng 240 15
60 0011110 <
This Week
METU Computer Engineering

 Dive into Python [Part 1/2]


 Basic and Container Data in Python:
 int, float, complex, bool
 string, list, tuple, dict, set
 Operators and Expressions: Arithmetic operators,
Expression, Comparison operators, Logic
connectives

2021 S. Kalkan - CEng 240 16


METU Computer Engineering

int, float, complex, bool


string, list, tuple, dict, set

BASIC AND CONTAINER DATA

2021 S. Kalkan - CEng 240 17


What is data?
METU Computer Engineering

• Data: Information to be processed to solve a problem.


• Identify the data for the following example problems:

2020
What is data?
METU Computer Engineering

 CPU can only understand two types of data:


 Integers,
 Floating points.
 The following are not directly understandable by a CPU:
 Characters (‘a’, ‘A’, ‘2’, …)
 Strings (“apple”, “banana”, …)
 Complex Numbers
 Matrices
 Vectors
 But, programming languages can implement these data
types.
2020
Basic Data in Python
Numerical Types
METU Computer Engineering

 Integers:
 int
 Unlimited size
 Floating point numbers:
 float
 IEEE754 standard (32bit, 64bit)
 Complex numbers
 complex
 3+4j

2021 S. Kalkan - CEng 240 20


Basic Data in Python
Numerical Types
METU Computer Engineering

 Useful operations with numerical types


 type(<data>) function
 abs(<number>)
 pow(<number1>, <number2>)
 round(<float-number>)
 sin(), cos(), log() from math library

2021 S. Kalkan - CEng 240 21


Basic Data in Python
Boolean Type
METU Computer Engineering


bool type
 Can take True or False

 Useful operations with bool type


 and, or, not

2021 S. Kalkan - CEng 240 22


Container Data in Python
METU Computer Engineering

 str
 list Mutable
 tuple

 dict

 set
Immutable
 frozenset

 Mutability vs. immutability


2021 S. Kalkan - CEng 240 23
Container Data in Python
Accessing Elements of Sequences
METU Computer Engineering

 Positive indexing
 Negative indexing

 Slicing

2021 S. Kalkan - CEng 240 24


Container Data in Python
Useful Operations
METU Computer Engineering

 len()  Membership

 Concatenation (+)

 Repetition

2021 S. Kalkan - CEng 240 25


Container Data in Python
String
METU Computer Engineering

 Writing strings
 single quote
 double quote
 triple quote
 Special characters

 Unicode support in v3

2021 S. Kalkan - CEng 240 26


Container Data in Python
String
METU Computer Engineering

 Examples with strings

 Strings are immutable

2021 S. Kalkan - CEng 240 27


Container Data in Python
String: Useful Operations
METU Computer Engineering

 str()
 len()

 Concatenation, repetition, membership

 Evaluate a string: eval() function

 Deletion from / insertion into a string


 Not possible

2021 S. Kalkan - CEng 240 28


Container Data in Python
List and Tuple
METU Computer Engineering

 Lists: mutable
 [10, 20, 30]
 [“ali”, 20, “veli”, 30]
 [10, [20, [30]], 40]
 Tuples: immutable

 (10, 20, 30)


 (“ali”, 20, “veli”, 30)
 (10, (20, (30)), 40)

2021 S. Kalkan - CEng 240 29


Container Data in Python
List: Useful Operations
METU Computer Engineering

 Deletion

2021 S. Kalkan - CEng 240 30


Container Data in Python
List: Useful Operations
METU Computer Engineering

 Insertion

2021 S. Kalkan - CEng 240 31


Container Data in Python
List: Useful Operations
METU Computer Engineering

 list() and tuple() functions

 Concatenation, repetition

 Membership

2021 S. Kalkan - CEng 240 32


Container Data in Python
Dictionary
METU Computer Engineering

 dict: mutable

 Add/delete
elements

 KeyError

2021 S. Kalkan - CEng 240 33


Container Data in Python
Dictionary: Useful Operations
METU Computer Engineering

 len()
 Membership

 values()

 keys()

2021 S. Kalkan - CEng 240 34


Container Data in Python
Set
METU Computer Engineering

 set: mutable

2021 S. Kalkan - CEng 240 35


Container Data in Python
Frozen Set
METU Computer Engineering

 frozenset:
immutable

2021 S. Kalkan - CEng 240 36


Container Data in Python
Set: Useful Operations
METU Computer Engineering

 len(<set>)  Only with the ‘set’


 Membership
type:
 Set operations:  S.add(element)
 Subset: S1 <= S2  S.remove(element
)
 Superset: S1 >= S2  S.pop()
 Union: S1 | S2
 Intersection: S1 & S2
 Set difference: S1-S2
2021 S. Kalkan - CEng 240 37
METU Computer Engineering

Operators and Expressions

ACTION

2021 S. Kalkan - CEng 240 38


Action
METU Computer Engineering

 Purposes of actions
 Creating/modifying data
 Interaction with the environment

 Types of actions
 Expressions
 Statements

2021 S. Kalkan - CEng 240 39


Expressions
METU Computer Engineering

 Expression: A mathematical operation that has


a resulting value
 a = 10 + 20 * 3

 Operators can be unary or binary

2021 S. Kalkan - CEng 240 40


Expression Evaluation
Precedence and Associativity
METU Computer Engineering

2021 S. Kalkan - CEng 240 41


Expression Evaluation
and, or evaluation
METU Computer Engineering

 A shortcut is taken if possible

2021 S. Kalkan - CEng 240 42


Expression Evaluation
Type Conversion
METU Computer Engineering

 Implicit
 3 + 4.5
 3 + True

 Explicit
 int(), float(),
bool()
 str(), ...

2021 S. Kalkan - CEng 240 43


Statements
METU Computer Engineering

 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

2021 S. Kalkan - CEng 240 45


Operators in
Python
METU Computer Engineering

 Arithmetic
operators
 Logic operators

 Container

operators
 Comparison

operators

2021 S. Kalkan - CEng 240 46


Final Words:
Important Concepts
METU Computer Engineering

 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

2021 S. Kalkan - CEng 240 47


METU Computer Engineering

THAT’S ALL FOLKS!


STAY HEALTHY

2021 S. Kalkan - CEng 240 48

You might also like