Programming
Fundamentals
in Python
Georgios Papadopoulos
Laboratory Center of Katerini
Algorithmic
Structures
Selection Structure
●If we want to execute a command sequence, if
a logical condition is met, then we use the IF
statement.
Selection structure
Python supports the usual logical conditions from mathematics:
● Equals: a == b
● Not Equals: a != b
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if
statements" and loops.
Conditions
if condition:
block_of_code
code
● If the condition is True, the block of code is executed
and then the code after the if statement.
● If the condition is False the block of code is skipped
from execution.
1st
case
if statement
A block of code is
defined by identation.
(typically 4 spaces
or 1 tab)
1st
case
Example 1
#Ex11 - Absolute Value
a = input ("Give a number:")
if a < 0:
a = (-1) * a
print ("absolute number value:", a)
1st
case
Exercise 11
if the number is negative,
we convert to positive
multiplying by -1
alternative: a=abs(a)
if condition:
block_of_code_1
else:
block_of_code_2
code
2nd
case
if – else statement
• If the condition is True, the
block of code 1 is executed
and then the code after the if
statement.
• If the condition is False the
block of code 2 is executed
and then the code after the if
statement.
2nd
case
Example
Write a program that reads two integers a
and b and if a>b calculates and displays a
- b differently, calculates and displays b -
a.
2nd
case
Exercise 12
#Ex12
a = input ("Give 1st number:")
b = input ("Give 2nd number:")
if a> b:
c = a - b
else:
c = b - a
print ("result:", c)
2nd
case
Exercise 12 - Solution
●Write a program that reads an integer
and displays "even" or "odd" depending
on what this number is.
●Note: “even” numbers are exactly divided by 2
(no remainder), but “odd” numbers are not
2nd case
Exercise 13
#ex13 - even or odd
a = input ("Give an integer:")
if a % 2 == 0:
print "even"
else:
print "odd"
2nd case
Exercise 13 - Solution ...
3rd
case
if-elif-else statement
if condition1:
block_of_code_1
elif condition2:
block_of_code_2
else:
block_of_code_3
code
• If condition1 is True the
block_of_code1 is executed
Otherwise the condition2 is checked.
• If the condition2 is True, the
block_of_code2 is executed
Otherwise the block_of_code3 is
executed.
• Finally the code after if is executed ...
3rd case
Example
#Ex14 - Positive or Negative or Zero
a = input ("Give a Number:")
if a> 0: print ("Positive")
elif a <0: print ("Negative")
else: print ("Zero")
● Note: Since the code blocks consist of only one statement, then they
can be written in the same line as above!
3rd case
Example
●Create a program that reads two
numbers given by the keyboard
and calculates and displays the
greater number (max).
Exercise 15
#Ask15
a1 = input ("Give 1st number:")
a2 = input ("Give 2nd number:")
if a1 > a2:
m = a1
else:
m = a2
print ("greater number:", m)
ASK15 - Solution
●Write a program that reads two float
numbers (eg a and b) and a character
that is '+' or '-' or '*' or '/' and calculates
and displays the result of the
corresponding operation. e.g. 5, 7 and +
will give 5+7=12
Ex16 – Simple calculator
#Ex16
a = float (input ("Give 1st number:"))
b = float (input ("Give 2nd number:"))
ch = input ("Give operator (+, -, *, /):")
if ch == "+": res = a+b
elif ch == "-": res = a-b
elif ch == "*": res = a*b
else: res = a/b
Print ("result:", res)
Ex16 - Solution
We assume that one of the
character was given one of: +,-,*,/
Example:
Give 1st number: 1
Give 2nd number: 2
Give operator ( +,-,*,/): +
result: 3.0
In three different qualifying matches for the Sydney
Olympics in the long jump, an athlete achieved
performance A, B, C. Write a program that:
a) reads performance values ​
​
A, B, C
b) Calculates and displays the average value of the
above values
​
​
c) displays the message "qualified" if the above
average value is greater than 8 meters.
Ex17
#Ex17
a = float (input ("Give 1st performance:"))
b = float (input ("Give 2nd performance:"))
c = float (input ("Give 3rd performance:"))
avg = (a+b+c)/3
if avg > 8:
print "qualified"
else:
Print "not qualified”
Ex17 - Solution
●Write a program that reads 3
numbers and displays the message
"can be triangle angles" if these
numbers can be the angles of a
triangle.
Ex18
#Ask18
A1 = int( input ('Give 1st number:
'))
A2 = int( input ('Give 2nd number:
'))
A3 = int( input ('Give 3rd number:
'))
ASK18 - Solution
● Write a program that reads an integer and if its last
digit is 4 or 7 displays the half of the number,
otherwise displays the double of the number.
Ex20
#Ask20
a = int (input ("Give a
number:"))
if a % 10 == 4 or a % 10 == 7:
print(a/2)
else:
ASK20 - Solution
Logical operators
and: Returns True if both statements are true
or: Returns True if one of the statements is true
not: Reverse the result, returns False if the result is true
●Write a program that reads a double-digit
number and if its first digit is larger than the
second, calculate their difference while in any
other case the square of their summary.
Ex25
● Note: Use divmod function
#Ex25
num = int (input ("Give a two-digit
integer :"))
d1, d2 = divmod(num, 10)
if d1> d2:
res = d1-d2
else:
res = (d1+d2) ** 2
Print (res)
Ex25 - Solution
alternative:
d1 = num // 10
d2 = num % 10
●Write a program that reads three
numbers and calculates and displays
the smallest (min).
Ex26
#Ask26
a1 = input ("Give 1st number:")
a2 = input ("Give 2nd number :")
a3 = input ("Give 3rd number :")
if a1 <= a2 and a1 <= a3:
m = a1
elif a2 <= a1 and a2 <= a3:
m = a2
else:
m = a3
Print ("min number: ", m)
Ex26 - Solution
The owner of an electrical store to attract customers has
decided to offer the products with a price more than €
1,000 in interest-free installments. Write a program that:
a) reads the value of the product
b) reads the number of installments (if input is 0 will not
have any)
c) calculates and displays the amount of each installment,
if they are selected, otherwise displays the message "no
installments".
Ask27
# Ask27
price = float (input ("price:"))
if price> 1000:
inst = int ("give number of installments:"))
if inst> 0:
inst_amount = price / inst
print ("installment amount: ", inst_amount)
else:
Print ("No installments")
else:
Print ("No installments")
Ask27
Write a program that will read two numbers
corresponding to the height and weight of a man. The
messages "tall - heavy", "tall - light", "short - heavy" or
"short - light" messages should appear.
Note: Heavy is considered someone who has over 80 kg
and tall when he is over 1.8 meters high.
Ask28
#Ex28
w = input ("Give weight:")
h = input ("Give height:")
if h > 1.8 and w > 80:
print "tall - heavy"
elif h > 1.8 and w <= 80:
print "tall - light"
elif h <= 1.8 and w > 80:
print "short - heavy"
else:
print "short - light"
Ex28 - Solution

Programming Fundamentals in Python - Selection Structure

  • 1.
  • 2.
  • 3.
    ●If we wantto execute a command sequence, if a logical condition is met, then we use the IF statement. Selection structure
  • 4.
    Python supports theusual logical conditions from mathematics: ● Equals: a == b ● Not Equals: a != b ● Less than: a < b ● Less than or equal to: a <= b ● Greater than: a > b ● Greater than or equal to: a >= b These conditions can be used in several ways, most commonly in "if statements" and loops. Conditions
  • 5.
    if condition: block_of_code code ● Ifthe condition is True, the block of code is executed and then the code after the if statement. ● If the condition is False the block of code is skipped from execution. 1st case if statement A block of code is defined by identation. (typically 4 spaces or 1 tab)
  • 6.
  • 7.
    #Ex11 - AbsoluteValue a = input ("Give a number:") if a < 0: a = (-1) * a print ("absolute number value:", a) 1st case Exercise 11 if the number is negative, we convert to positive multiplying by -1 alternative: a=abs(a)
  • 8.
    if condition: block_of_code_1 else: block_of_code_2 code 2nd case if –else statement • If the condition is True, the block of code 1 is executed and then the code after the if statement. • If the condition is False the block of code 2 is executed and then the code after the if statement.
  • 9.
  • 10.
    Write a programthat reads two integers a and b and if a>b calculates and displays a - b differently, calculates and displays b - a. 2nd case Exercise 12
  • 11.
    #Ex12 a = input("Give 1st number:") b = input ("Give 2nd number:") if a> b: c = a - b else: c = b - a print ("result:", c) 2nd case Exercise 12 - Solution
  • 12.
    ●Write a programthat reads an integer and displays "even" or "odd" depending on what this number is. ●Note: “even” numbers are exactly divided by 2 (no remainder), but “odd” numbers are not 2nd case Exercise 13
  • 13.
    #ex13 - evenor odd a = input ("Give an integer:") if a % 2 == 0: print "even" else: print "odd" 2nd case Exercise 13 - Solution ...
  • 14.
    3rd case if-elif-else statement if condition1: block_of_code_1 elifcondition2: block_of_code_2 else: block_of_code_3 code • If condition1 is True the block_of_code1 is executed Otherwise the condition2 is checked. • If the condition2 is True, the block_of_code2 is executed Otherwise the block_of_code3 is executed. • Finally the code after if is executed ...
  • 15.
  • 16.
    #Ex14 - Positiveor Negative or Zero a = input ("Give a Number:") if a> 0: print ("Positive") elif a <0: print ("Negative") else: print ("Zero") ● Note: Since the code blocks consist of only one statement, then they can be written in the same line as above! 3rd case Example
  • 17.
    ●Create a programthat reads two numbers given by the keyboard and calculates and displays the greater number (max). Exercise 15
  • 18.
    #Ask15 a1 = input("Give 1st number:") a2 = input ("Give 2nd number:") if a1 > a2: m = a1 else: m = a2 print ("greater number:", m) ASK15 - Solution
  • 19.
    ●Write a programthat reads two float numbers (eg a and b) and a character that is '+' or '-' or '*' or '/' and calculates and displays the result of the corresponding operation. e.g. 5, 7 and + will give 5+7=12 Ex16 – Simple calculator
  • 20.
    #Ex16 a = float(input ("Give 1st number:")) b = float (input ("Give 2nd number:")) ch = input ("Give operator (+, -, *, /):") if ch == "+": res = a+b elif ch == "-": res = a-b elif ch == "*": res = a*b else: res = a/b Print ("result:", res) Ex16 - Solution We assume that one of the character was given one of: +,-,*,/ Example: Give 1st number: 1 Give 2nd number: 2 Give operator ( +,-,*,/): + result: 3.0
  • 21.
    In three differentqualifying matches for the Sydney Olympics in the long jump, an athlete achieved performance A, B, C. Write a program that: a) reads performance values ​ ​ A, B, C b) Calculates and displays the average value of the above values ​ ​ c) displays the message "qualified" if the above average value is greater than 8 meters. Ex17
  • 22.
    #Ex17 a = float(input ("Give 1st performance:")) b = float (input ("Give 2nd performance:")) c = float (input ("Give 3rd performance:")) avg = (a+b+c)/3 if avg > 8: print "qualified" else: Print "not qualified” Ex17 - Solution
  • 23.
    ●Write a programthat reads 3 numbers and displays the message "can be triangle angles" if these numbers can be the angles of a triangle. Ex18
  • 24.
    #Ask18 A1 = int(input ('Give 1st number: ')) A2 = int( input ('Give 2nd number: ')) A3 = int( input ('Give 3rd number: ')) ASK18 - Solution
  • 25.
    ● Write aprogram that reads an integer and if its last digit is 4 or 7 displays the half of the number, otherwise displays the double of the number. Ex20
  • 26.
    #Ask20 a = int(input ("Give a number:")) if a % 10 == 4 or a % 10 == 7: print(a/2) else: ASK20 - Solution Logical operators and: Returns True if both statements are true or: Returns True if one of the statements is true not: Reverse the result, returns False if the result is true
  • 27.
    ●Write a programthat reads a double-digit number and if its first digit is larger than the second, calculate their difference while in any other case the square of their summary. Ex25 ● Note: Use divmod function
  • 28.
    #Ex25 num = int(input ("Give a two-digit integer :")) d1, d2 = divmod(num, 10) if d1> d2: res = d1-d2 else: res = (d1+d2) ** 2 Print (res) Ex25 - Solution alternative: d1 = num // 10 d2 = num % 10
  • 29.
    ●Write a programthat reads three numbers and calculates and displays the smallest (min). Ex26
  • 30.
    #Ask26 a1 = input("Give 1st number:") a2 = input ("Give 2nd number :") a3 = input ("Give 3rd number :") if a1 <= a2 and a1 <= a3: m = a1 elif a2 <= a1 and a2 <= a3: m = a2 else: m = a3 Print ("min number: ", m) Ex26 - Solution
  • 31.
    The owner ofan electrical store to attract customers has decided to offer the products with a price more than € 1,000 in interest-free installments. Write a program that: a) reads the value of the product b) reads the number of installments (if input is 0 will not have any) c) calculates and displays the amount of each installment, if they are selected, otherwise displays the message "no installments". Ask27
  • 32.
    # Ask27 price =float (input ("price:")) if price> 1000: inst = int ("give number of installments:")) if inst> 0: inst_amount = price / inst print ("installment amount: ", inst_amount) else: Print ("No installments") else: Print ("No installments") Ask27
  • 33.
    Write a programthat will read two numbers corresponding to the height and weight of a man. The messages "tall - heavy", "tall - light", "short - heavy" or "short - light" messages should appear. Note: Heavy is considered someone who has over 80 kg and tall when he is over 1.8 meters high. Ask28
  • 34.
    #Ex28 w = input("Give weight:") h = input ("Give height:") if h > 1.8 and w > 80: print "tall - heavy" elif h > 1.8 and w <= 80: print "tall - light" elif h <= 1.8 and w > 80: print "short - heavy" else: print "short - light" Ex28 - Solution