Chapter 3
Chapter 3
statements and
operators
INTRODUCTION TO PYTHON FOR DEVELOPERS
George Boorman
Curriculum Manager, DataCamp
Booleans
# Boolean variable
the_truth = True
print(the_truth)
True
False
True
True False
True False
True
# Compare sales
if units_sold >= sales_target
# Compare sales
if units_sold >= sales_target:
# Compare sales
if units_sold >= sales_target:
print("Target achieved")
'Target achieved'
# Compare sales
if units_sold >= sales_target:
print("Target achieved") # This line is not indented
print("Target achieved")
^
IndentationError: expected an indented block
George Boorman
Curriculum Manager, DataCamp
Individual comparisons
# Prices list
prices = [9.99, 8.99, 35.25, 1.50, 5.75]
prices[0] > 10
False
prices[0] < 5
False
True
9.99
8.99
35.25
1.5
5.75
g
e
o
r
g
e
_
d
c
AG32 87.99
HT91 21.5
PL65 43.75
OS31 19.99
KB07 62.95
TR48 98.0
AG32 87.99
HT91 21.5
PL65 43.75
OS31 19.99
KB07 62.95
TR48 98.0
range(start, end + 1)
start = inclusive
1
2
3
4
5
print(visits)
10
George Boorman
Curriculum Manager, DataCamp
If statement
If statement
1 https://unsplash.com/@joaoscferrao
# Number of purchases
num_purchases = 0
# Stock limit
stock = 10
# Number of purchases
num_purchases = 0
George Boorman
Curriculum Manager, DataCamp
Complex workflows
Loops through data structures
for , while
Update variables
+=
Return outputs
print()
True
True
# Check if "HT91" is a key and the minimum price of all products is > 5
if "HT91" in products_dict.keys() and min(products_dict.values()) > 5:
print(True)
else:
print(False)
True
# Check if "HT91" is a key or that the minimum price of all products is < 5
if "HT91" in products_dict.keys() or min(products_dict.values()) < 5:
print(True)
else:
print(False)
True
sales_count = 0
for sale in range(1, 10):
# sales_count = sales_count + 1
sales_count += 1
stock = 10
for sale in range(1, 10):
# sales_count = sales_count - 1
stock -= 1
George Boorman
Curriculum Manager, DataCamp
Chapter 1 recap
Syntax Action Example Output # Define total_spend
* Multiply 4 * 10 40 total_spend = 3150.96
+ Addition 7 + 9 16
# Single quotes
- Subtract 23 - 4 19 customer_name = 'George Boorman'
/ Division 27 / 3 9
# Double quotes also work
** Power 3 ** 2 9 customer_name = "George Boorman"
% Modulo 7 % 4 3
9.99
8.99
35.25
1.5
5.75
list.append()
enumerate()
time
venv
pandas
requests
numpy