Professional Documents
Culture Documents
Cluj Napoca
Faculty of Mathematics and Computer Science
Fundamentals of Programming
Python basics easy to understand, interpreted, dynamically typed, modern, fully-featured language
Using the Python IDLE
Where to search for documentation
Write and run simple programs using console input/output and basic structured data types (list, sets,
dictionaries and tuples)
A first glimpse into simple feature driven development
Babe-Bolyai University
Cluj Napoca
Faculty of Mathematics and Computer Science
Fundamentals of Programming
PYTHON PROGRAMS
A FIRST EXAMPLE
x = 5
if x < 6:
print "less"
else:
print "more"
while x < 10:
print x
x += 1
INTRODUCTORY PROBLEMS
Determine the n-th element of the sequence 1,2,3,2,5,2,3,7,2,3,2,5,... obtained from the sequence of
natural numbers by replacing composed numbers with their prime divisors, without memorizing the
elements of the sequence.
Babe-Bolyai University
Cluj Napoca
Faculty of Mathematics and Computer Science
Fundamentals of Programming
SETS
l = 'abracadabra'
ln = [1, 2, 3, 4, 3, 2, 1, 10]
print set(l)
print set(ln)
a = set([1, 2, 3, 4])
b = set([1, 2, 5, 6])
print "Reunion - "
+
str(a
print "Intersection - " +
str(a
print "Difference - "+
str(a
print "Mutual difference -" +str(a
|
&
^
b) #reunion
b) #intersection
b)
b) #mutual difference
DICTIONARY
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print tel # {'sape': 4139, 'guido': 4127, 'jack': 4098}
print tel['jack'] # 4098
del tel['sape']
tel['irv'] = 4127
print tel # {'guido': 4127, 'irv': 4127, 'jack': 4098}
print tel.keys() # ['guido', 'irv', 'jack']
print 'guido' in tel #True
a. We have a single feature and a single iteration (find the smallest prime number)
Babe-Bolyai University
Cluj Napoca
Faculty of Mathematics and Computer Science
Fundamentals of Programming
b. Running scenario
User
Program
Give a positive number (0
exits)
4
5
Give a positive number
Description
Read a number
Enter number
Print result
-3
Enter positive number
0
Exit
c. Tasks
T1 Compute the smallest prime greater than given number
T2 Verify if a number is prime
T3 Implement user interface
Task relation
T2 T1 T3
Start with T2: add testing
def isPrime(x):
if x < 2:
return False
for i in range(2, x / 2 + 1):
if x % i == 0:
return False
return True
Input
2
6
3
-2
1
7
Output
True
False
True
False
False
True
PYTHON RESOURCES