Section 1 · Lesson 1

Values, variables and arithmetic

Store values in variables, do arithmetic, and print results.

20 min 2 practice tasks Free preview
Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data by Corey Schafer on YouTube

Python programs work with values (numbers, text, true/false) and store them in variables so you can reuse them.

Variables

campaign = "Summit Search"   # a string (text)
clicks = 742                 # an int (whole number)
spend = 1288.40              # a float (decimal number)
is_active = True             # a bool

A variable is a name pointing at a value. Use lowercase_with_underscores names that say what the value means; daily_spend beats x.

Arithmetic

Operator Meaning Example Result
+ - * add, subtract, multiply 3 * 4 12
/ divide (always a float) 7 / 2 3.5
// floor division 7 // 2 3
% remainder 7 % 2 1
** power 2 ** 10 1024

Underscores in numbers are just for readability: 51_200 is the same as 51200.

Printing and rounding

cpc = 1288.40 / 742
print(cpc)            # 1.7363881401617251
print(round(cpc, 2))  # 1.74

print() shows output in the console panel. round(x, n) rounds to n decimals.

Tip: In ad-tech, many key metrics are ratios: CTR = clicks ÷ impressions, CPC = spend ÷ clicks, CPA = spend ÷ conversions. You’ll compute all of them in this course.

Practice

Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.

1

Click-through rate

A campaign got clicks = 742 from impressions = 51_200. Create a variable ctr holding the click-through rate as a percentage (clicks ÷ impressions × 100), rounded to 2 decimals with round().

Need a hint?
  1. / always gives a float in Python 3.
  2. round(value, 2) keeps two decimals.
Python 3
clicks = 742
impressions = 51_200
ctr = 0
print(ctr)
Files in the working directory
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers.csv (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
advertisers.csv (12 rows)
id, name, industry, country
2

How long will the budget last?

An advertiser has budget = 10_000 dollars and spends daily_spend = 650 per day. Set full_days to the number of whole days the budget covers and left_over to the dollars remaining after those days.

Need a hint?
  1. // is floor division; % is the remainder.
Python 3
budget = 10_000
daily_spend = 650
full_days = ...
left_over = ...
Files in the working directory
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers.csv (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
advertisers.csv (12 rows)
id, name, industry, country