You are on page 1of 4

Babe-Bolyai University

Cluj Napoca
Faculty of Mathematics and Computer Science
Fundamentals of Programming

SEMINAR I INTRODUCTION TO PYTHON


CONTENTS
What you shoud know after attending .................................................................................................................................................... 1
Python programs ......................................................................................................................................................................................... 2
A first example .............................................................................................................................................................................................. 2
Indentation, indentation, indentation................................................................................................................................................. 2
A first example dynamic (duck) typing ........................................................................................................................................... 2
introductory problems .............................................................................................................................................................................. 2
Compound data in Python ............................................................................................................................................................................ 3
List ..................................................................................................................................................................................................................... 3
Sets .................................................................................................................................................................................................................... 3
Dictionary ....................................................................................................................................................................................................... 3
Feature driven development ....................................................................................................................................................................... 3
Python Resources ............................................................................................................................................................................................. 4

WHAT YOU SHOUD KNOW AFTER ATTENDING

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

INDENTATION, INDENTATION, INDENTATION


a = 1
while a<5:
print a
a=a+1
print "done"

A FIRST EXAMPLE DYNAMIC (DUCK) TYPING


a = 1
print a
a = "abc"
print a
a = [1,2,4,3]
print a
a.sort()
print a

INTRODUCTORY PROBLEMS

Compute the sum of n natural numbers.

Decide if a given number is prime or not

Compute the greatest common divisor of 2 natural number

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

COMPOUND DATA IN PYTHON


LIST
s = [1, 2, 3, 4, 5]
print s[:]
#[1, 2, 3, 4, 5]
print s[1:] #[2, 3, 4, 5]
print s[:1] #[1]
print s[2:4] #[3,4]
print s[-1:] #[5]
print s[-3:] #[3,4,5]
print s[:-1] #[1,2,3,4]
print s[:-3] #[1,2]

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

FEATURE DRIVEN DEVELOPMENT


1. Given a natural number n find the smallest prime number greater than n.

Identity and outline requirements


Build a feature list from the problem statement
Plan iterations, an iteration include a single feature
o Choose some simple features
o Describe running scenarios
o Test and Implement in python the feature

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

Where (the) Python lives


https://www.python.org/
https://docs.python.org/3/
Python IDLE tutorial
https://hkn.eecs.berkeley.edu/~DYOO/python/idle_intro/index.html

You might also like