What Is a Data Type?
A data type is simply a way to classify different types of variables. When you have a fruit, you can classify it as a particular type, such as apple or orange, for example. When you have a variable, the computer needs to classify it as a specific type as well: a data type. These are similar to the types of data you can use in an Excel spreadsheet: numbers, words, etc.
The most basic, or primitive, data types used in Python are:
Wait, what? 😵 Primitive data types? Booleans? Is that even English?!
Don't worry! You'll have a few new terms to get used to as you learn to think like a computer, but we'll go through each of these together. 😎
Let's start with the first two, which are both numeric values.
Discover the Numeric Data Types: Integer and Float
Numeric types can fall into two categories in Python: integer or float.
An integer is any whole number:
A float is a decimal number:
Here are examples of variables with numeric data. Which are floats and which are integers?
conversion_rate = 3.2
number_of_followers = 1304
number_of_likes = 234
episode_duration = 29.1
If you guessed
episode_duration as floats and the other two as integers, you're correct!
Just like numbers in Excel or with a calculator, you can do all sorts of arithmetic operations with integers and floats in Python. Here are a few examples of the arithmetic operators you can use in your code:
x + y: the sum of x and y (x plus y).
x - y: the difference between x and y (x minus y).
x * y: the product of x and y (x times y).
x / y: the quotient of x and y (x divided by y).
x % y: the remainder of x divided by y.
Playing around with arithmetic is a great way to practice some basic programming, so you'll find an exercise to help you do that at the end of this chapter.
Level-Up: Use Numeric Data & Arithmetic Operators
Variables, data, and types, oh my! They aren’t so bad once you get some practice. Tackle this exercise on Replit to get a better feel for putting arithmetic in code. 😁
Then, check your work by:
submitting your project,
navigating back to the course team page (click on the exercise title on the top left of your screen, then the team name "OCpythonbasics")
clicking on "Fork the solution".
Pin Down the String Data Type
A string is just a fancy word for saying, well, words. A string is any information (usually text) that is surrounded by quotation marks (either single or double). So
'hello' in single quotation marks and
"hello" in double quotation marks are both strings. So are
Some other examples of string variables:
greeting = "hi!"
the_teacher = "Rina"
first_name = "Sam"
inspiring_quote = "You miss 100% of the shots you don’t take"
Level-Up, Round 2: Experiment With Strings
Practice working with strings and explore some of the tricks to using them effectively in this exercise. 😁
This is a Boolean: True or False?
Don't be intimidated by the funny name! A boolean is quite simple - it has only two options for its value: either
Booleans can be very useful for saving whether something is successful or not.
For example, if you wanted to have a variable that could store the fact that it's sunny outside or not, you could say
sunny_weather = True. If it is not sunny, then
sunny_weather = False.
A data type is a category or classification of the information in your code.
There are many data types, but the most basic ones are float (decimal number), integer (whole number), boolean (True/False), and string (text in quotation marks).
Now that you know how to classify data with data types, we’ll go into more complex data objects, like lists.