Introduction To Computin G: Fall 2015
Introduction To Computin G: Fall 2015
Fall 2015
Department of Computing
The School of EE & Computing
Adama Science & Technology University
OUTLINE ASTU
Reading assignment
Chapter 3 of the textbook
The lecture note for cs1media
Characteristics of Python
Instruction set
Arithmetic and logical operations
+, -, *, /, and ** for defining
and, or, not expressions
Assignment
Conditionals
Iterations
Input/output
No pointers
No declarations
ASTU
Python Zoo
Imagine there is a zoo inside your Python interpreter.
Every time an object is created, an animal is born.
What an animal can do depends on the kind of
animal: birds can fly, fish can swim, elephants can
lift weights, etc. When an animal is no longer used, it
dies(disappears).
Numbers
integer: 13, -5
float: 3.14159265
complex number: 3 + 6j
Bo oleans(truth values)
True or False
ASTU
Complex objects
Tuples
(1, 3, 5, 7, 9)
(“red”, “green”, “blue”)
(777, “a lucky number”)
Lists
Dictionary to be discussed later
ASTU
Tuples
position = (3.0, 7.2, 5.7)
Instructors = (“Joseph S. Shin”, “Chang B. Choi”)
Type inquires
>>>type(3) >>>type(3 + 5j)
<Type ‘int’> <Type ‘complex’>
>>>type(3.145) >>>type(True)
<Type ‘float’> <Type ‘bool’>
>>>type(“Welcome”)
<Type ‘str’>
ASTU
message = “Welcome“
n = 17
from cs1robots import *
create_world()
hubo = Robot()
pi = 3.1415926535897931
finished = True
In the Python zoo, the name is a sign board on the animal’s cage.
ASTU
Good:
msg = “cce20003 is fantastic“
ba13 = 13.0
Bad:
more@ = "illegal character“
13a = 13.0
def = “Definition”
ASTU
In the Python zoo, this means that the sign board is mov
ed from one animal to a different animal.
ASTU
>>> b = "banana"
>>> print b.upper()
BANANA
>>>from cs1robots import *
>>> hubo = Robot()
>>> hubo.move()
>>> hubo.turn_left()
>>>from cs1media import *
>>>img = load_picture(“photos/yuna1”)
>>> print img.size()
(58, 50)
>>> img.show()
ASTU
hubo = Robot("yellow”)
hubo.move() The same object may have
more than one name!
ami = hubo
hubo = Robot("blue")
hubo.move() hub yellow
ami.turn_left() o robot
ami
ami.move()
blue
robot
OPERATORS AND OPERANDS ASTU
>>> 2 ** 16 a ** b = ab
65536
>>>15.3 + 3.0
18.3
>>>7 % 5
2
>>>7 / 5
1
>>>7.0 / 5
1.4
EXPRESSIONS AND STATEMENTS ASTU
•
Expressions
Repeating 8 times!
ASTU
Relational operators ==, !=, >, <, <=, and >= are
used to compare objects. The results are Boolean values, True
or False. A Boolean expression is an expression whose
value is of type bool. They are used in if and while
statements.
>>>27 == 14
False
>>> 3.14 != 3.14
False
x=9
if x == 3 ** 2 :
print “x is a perfect square”
if x % 2 != 0:
print “x is odd”
ASTU
x = 5.0
y = 6.0
z = 7.0
•
conditionals: if, if, and if
iterations
for-loops
while-loops
assignments a = b
input/output
(functions)
ASTU
Review: for-loops
block of statements
……
0,0 1,0 2,0 …… w-1,0
…… ……
0,1 1,1 2,1 w-1,1
…… …… …… …… …… ……
…… …… …… …… …… ……
Color conversion
w, h = img.size()
white = 255
black=0
print "imae size: w,h = ", w,h
for y in range(h):
for x in range(w):
r, g, b = img.get(x,y)
v = (r+g+b) / 3.0 threshold
if v > 100: (0 <= v <= 255)
img.set(x,y, white)
else:
img.set(x,y, black)
img.show()