BIS4+ +Programming+ +Python+%28no+Solutions%29
BIS4+ +Programming+ +Python+%28no+Solutions%29
1. Introduction
2. Goal - Basic concepts
3. Algorithm
4. Programming
Python in a Nutshell
5. Functionalities
6. Project management
7. Databases
Overview
Download: https://www.anaconda.com/products/individual
Python Single Predefined Types
Programming
Exercise:
Program “miles in km” from chapter 3 (algorithm): the input is a given
distance in miles. The desired result is the same distance but in
kilometers. The relationship between the two is: 1 mile = 1,609 km
Exercise:
Program “miles in km” from chapter 3 (algorithm): the input is a given
distance in miles. The desired result is the same distance but in
kilometers. The relationship between the two is: 1 mile = 1,609 km
Step4: Put the algorithm in Python
Transform the algorithm from slide 49 (from BIS3 – Algorithms) into a Python program.
Read the Marks and give an error when >100!
mark = int(input("Give the mark of the student: "))
if mark < 60:
grade = "F"
elif mark < 70:
grade = "D"
elif mark < 80:
grade = "C"
elif mark < 90:
grade = "B"
elif mark <= 100:
grade = "A"
else:
grade = "Error! Mark is maximum 100!!!"
print ("The grade =", grade)
Exercise:
Write a Python program where a counter starts with the
value 1 and stops when the counter has the value 6.
After this, the value of the counter is displayed.
Infinite loop!!!
PseudoCode
Begin
sum ← 0
write ‘Give number: ‘
read number
While number <> 0
do sum ← sum + number
write ‘Give number: ‘
read number
EndDo
write ‘The sum is ‘, sum
End
From algorithm (step 3) to programme (step 4)
PseudoCode Python
Begin
sum ← 0
write ‘Give number: ‘
sum = 0
read number number = int(input("Give number: "))
While number <> 0
while (number != 0):
do sum ← sum + number
write ‘Give number: ‘ sum = sum + number
read number number = int(input("Give number: "))
EndDo
write ‘The sum is ‘, sum print(“The sum is ", sum)
End
Or….. Solution with a counter
sum = 0
counter = 1
number = int(input("Give number n° " + str(counter) + ": "))
while (number != 0):
sum = sum + number
number = number + 1
number = int(input("Give number n° " + str(counter) + ": "))
print(“The sum is ", sum)
Python doesn't know this kind of loop!!!
The FOR-loop (in Python)
How would you
write a for-loop if
PseudoCode the for structure
did not exist?
In case this function was not predefined, how should you write it?
Example: pow(5,2) with the result 25
Python has the predefined function pow (power).
In case this function was not predefined, how should you write it?
General case:
With a function/procedure
Python has the predefined function pow (power).
In case this function was not predefined, how should you write it?
General case:
Formula: Kn = K0 * (1 + i)n
Same question as previous but the 3 parameters are read!!
Write a function/procedure that calculates the capital of an investment on x-number years.
We want to see the current balance for each year. Instead of one interest rate the
function makes the calculation for a list of several interest rates! Make two tests!!
Tip: use a function that calls another function (the one on slide 46)!
Input – Output Question
a = [1, 2, 3]
b=0
for i in a:
b=b+2*i
print(b)
Input – Output Question
a=1
b=3
for i in range(1,6):
if (a == i):
b = (i * 4) / a
else:
a=a*2
print(a,b)
Input – Output Question
How will the list “numbers” look like after executing following code?
list = [1, 7, 5, 4]
for item in list:
print(item)
Solution(s): see file on Canvas: ATM in Python