Professional Documents
Culture Documents
and Iteration
• Variables and arithmetic expressions, Conditionals
• String, Lists, Tuples, Sets, Dictionaries
• Iteration and Looping
Variables and arithmetic expressions
Variables
• untyped and made any type of data
during execution
Operator
• + , - , *, /
• %
• **
2
19
Conditionals
if … else
if a < b:
print (“Computer says Yes”)
else:
print (“Computer says No”)
3
19
Conditionals
pass
• To create an empty clause using pass
statement
if a < b:
pass # Do nothing
else
print (“Computer says No”)
4
19
Conditionals
AND
OR Boolean
NOT
6
19
Conditionals
elif
if suffix == “.htm”:
content = “text/html”
elif suffix == “.jpg”:
content = “image/jpeg”
else:
raise RuntimeError ("Unknown content type")
7
19
String
8
19
String
9
19
Lists
10
19
Lists
append()
• To append new items to the end of a list
names.append ("Paula")
insert()
• To insert an item into the middle of a list
names. insert (2, "Thomas")
11
19
Lists
12
19
Tuples
13
19
Sets
s = set([3,5,9,10])
14
19
Sets
15
19
Sets
remove()
An item can be removed to set
16
19
Dictionaries
17
19
lteration and Looping
for
• To iterate over a collection of items
18
19
lteration and Looping
19
19
02 Function,
Object and File
• Functions, Generators, Coroutines
• Objects and classes, Exceptions, Modules
• File Input and Output
Functions
3
14
Generators
next()
• produces a sequence of results through
successive calls
4
14
Coroutines
5
14
Coroutines
send()
• A coroutine is suspended until a value is
sent to it
close()
• This continues until the coroutine
function returns or close
6
14
Objects and classes
7
14
Objects and classes
dir()
• lists the methods available on an object
and is a useful tool for interactive
experimentation.
8
14
Exceptions
9
14
Modules
# file : div.py
def divide(a,b):
q = a/b # If a and b are integers, q is an integer
r = a - q*b
return (q,r)
10
14
Modules
11
14
Modules
12
14
Modules
13
14
File Input and Output
open( )
• returns a new file object
readline()
• reads a single line of input,
including the terminating newline
>>
• print the output to a file
14
14
03 Program Testing