Τμήμα Πληροφορικής και Τηλεπικοινωνιών - Άρτα
Πανεπιστήμιο Ιωαννίνων
Γκόγκος Χρήστος
http://chgogos.github.io/
Εαρινό εξάμηνο 2020-2021
age = 17
if age < 18:
print("Δεν μπορείς να λάβεις δίπλωμα οδήγησης")
Δεν μπορείς να λάβεις δίπλωμα οδήγησης
age = 18
if age < 18:
print("Δεν μπορείς να λάβεις δίπλωμα οδήγησης")
else:
print("Μπορείς να λάβεις δίπλωμα οδήγησης")
Μπορείς να λάβεις δίπλωμα οδήγησης
age = 16
if age < 15:
print("Δεν μπορείς να λάβεις δίπλωμα οδήγησης")
elif 15 <= age < 18:
print("Μπορείς να κάνεις μαθήματα για δίπλωμα οδήγησης")
else:
print("Μπορείς να λάβεις δίπλωμα οδήγησης")
Μπορείς να κάνεις μαθήματα για δίπλωμα οδήγησης
# Εμφωλευμένα if
a, b = 1, 2 # A
# a,b = 1,3 # B
# a,b = 2,1 # C
if a == 1:
if b == 2:
print("A")
else:
print("B")
else:
print("C")
A
print(2 > 1 and 3 > 2)
print(1 == 5 % 2 or 2 != 2)
print("abc" in [5, 16, 42, "abc", 7])
print("abc" not in [5, 16, 42, "abc", 7])
True True True False
## H λέξη κλειδί None
x = None
y = []
z = ()
w = {}
y = ""
if not x:
print("None is considered False")
if not y:
print("Empty list is considered False")
if not z:
print("Empty tuple is considered False")
if not w:
print("Empty dictionary is considered False")
if not y:
print("Empty string is considered False")
print((not x) == (not y))
print(x == y)
None is considered False Empty list is considered False Empty tuple is considered False Empty dictionary is considered False Empty string is considered False True False
x = 5
"Άρτιος" if x % 2 == 0 else "Περιττός"
'Περιττός'
# all
a = 1
b = 2
c = 3
list_of_conditionals = [a<5,b%2==0, c>0]
if all(list_of_conditionals):
print("Όλες οι συνθήκες ισχύουν")
Όλες οι συνθήκες ισχύουν
# any
a = 1
b = 2
c = 3
list_of_conditionals = [a%2==0, b%2==1, c%2==1]
if any(list_of_conditionals):
print("Μια τουλάχιστον από τις συνθήκες ισχύει")
Μια τουλάχιστον από τις συνθήκες ισχύει