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:
Integers
Floats
Strings
Booleans
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:
1
4
3,934
A float is a decimal number:
3.14
563.2
99.9
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 conversion_rate
and 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.
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 "goodbye"
and "whatsup"
.
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: Using variables in Python
Context:
In this exercise, we will consolidate our understanding of using variables in Python by exploring the different types of variables available. You will discover how to declare and manipulate variables of different types, such as integers, floating-point numbers, strings, and booleans. By understanding these fundamental concepts, you will be better prepared to tackle more complex tasks involving data manipulation in your future Python projects.
Instructions:
Define a variable
name
containing your name as a string.Define a variable
age
containing your age as an integer.Define a variable
height
containing your height as a floating-point number.Define a variable
is_student
containing a boolean value indicating whether you are a student or not.Use the print function to display the values of the variables defined above.
Use the type function to display the types of the variables defined above.
Let’s Recap!
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.