Module 1 · Python crash course · Lesson 1 of 66

Numbers and variables

Python as a calculator, and names that hold values.

In this lesson · about 10 minutes

  • Use +, −, *, /, ** and %
  • Use parentheses to control order
  • Store values in well-named variables

Welcome! This course takes you from your first line of Python to analyzing real data sets and drawing charts, all inside your browser. Every gray box of code on these pages is live: change it, press Run, and see what happens. Nothing to install.

This first module is a quick tour of the Python we need. If you already code, skim it and do the exercises. If you're brand new, take your time and run every cell.

Python as a calculator

OperatorMeaningExampleResult
+ -add, subtract7 - 25
*multiply3 * 412
/divide (always gives a decimal)1 / 20.5
//divide and drop the remainder7 // 23
%remainder (modulo)7 % 21
**power2 ** 416

Python follows the usual order of operations, so (2 + 3) * (5 + 5) is 50 while 2 + 3 * 5 + 5 is 22.

Try it

Like a notebook, the value of the last line is shown under the cell. Use print() to show anything else.

Variables

A variable is a name for a value. = means "store the value on the right under the name on the left". Names can use letters, digits and underscores, but can't start with a digit or contain spaces or symbols like -. By convention Python names are lowercase with underscores: monthly_rent, not MonthlyRent.

Exercise

Seven to the fourth

What is 7 to the power of 4? Write a single expression that calculates it.

Exercise

Sharing a pizza

A pizza has slices = 14 and there are people = 4. Create a variable each with how many whole slices each person gets, and left with how many slices are left over.

Check your understanding0 of 2 correct

Question 1

1.What does 9 / 3 give in Python 3?

Question 2

2.Which is a valid variable name?

Quiz score: 0 of 2

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.