Introduction to Python for Developers
George Boorman
Curriculum Manager, DataCamp
# Boolean variable
the_truth = True
print(the_truth)
True
Comparison operators
*
, +
, -
etc.Check if two things are equal
==
# Compare if 2 is equal to 3
2 == 3
False
# Check that 2 is not equal to 3
2 != 3
True
# Is 5 less than 7?
5 < 7
True
# Is 5 less than or equal to 7?
5 <= 7
True
# Is 5 greater than 7?
5 > 7
False
# Is 5 greater or equal to 7?
5 >= 7
False
# Is James greater than Brian
"James" > "Brian"
True
If True
perform a task
if
> condition is met > perform action
# Target units sold and actual units sold sales_target = 350 units_sold = 355
# Compare sales if units_sold >= sales_target
# Target units sold and actual units sold sales_target = 350 units_sold = 355
# Compare sales if units_sold >= sales_target:
# Target units sold and actual units sold sales_target = 350 units_sold = 355
# Compare sales if units_sold >= sales_target:
print("Target achieved")
'Target achieved'
# Target units sold and actual units sold sales_target = 350 units_sold = 355
# Compare sales if units_sold >= sales_target:
print("Target achieved") # This line is not indented
print("Target achieved")
^
IndentationError: expected an indented block
# Target units sold and actual units sold sales_target = 350 units_sold = 325
# Compare sales if units_sold >= sales_target:
print("Target achieved")
# Check if we were close to the target elif units_sold >= 320: print("Target almost achieved")
elif
keywords as we like!# Compare sales if units_sold >= sales_target: print("Target achieved")
# Check if we were close to the target elif units_sold >= 320: print("Target almost achieved")
# Otherwise... else: print("Target not achieved")
Operator | Function |
---|---|
== |
Equal to |
!= |
Not equal to |
> |
More than |
>= |
More than or equal to |
< |
Less than |
<= |
Less than or equal to |
Keyword | Function | Use |
---|---|---|
if |
If condition is met | First in the workflow |
elif |
Else check if condition is met | After if |
else |
Else perform this action | After elif |
Introduction to Python for Developers