Types are classifications that let the computer know how a programmer intends to use a piece of data. You can just think of them as, well, types of data. We’ve already seen one type in the last section: the integer. In this section, we’ll learn four more: the floating point number, the string, the boolean, and the list. Enter these lines as you see them below:
>>> type(1) <class 'int'> >>> type(1.0) <class 'float'> >>> type("Hello there!") <class 'str'> >>> type(True) <class 'bool'> >>> type([1, 2, 3]) <class 'list'>
Each of the responses show how the different types of data registers as different “types” for Python:
1 above) are whole numbers.
1.0 above) are numbers with decimals, and are treated a little differently than integers.
"Hello there!" above) are arbitrary sets of characters, such as letters and numbers. You can think of them as a way to store text.
True above) is a fancy term for values representing “true” and “false,” or “truthiness” and “falsiness.” In Python they are always capitalized:
[1, 2, 3] above) are ordered collections of values. You can put any of the other types in a list:
["hello", "goodbye", "see ya later"] is also a valid list.
Don’t worry about trying to actively remember these types. We’ll be working with each in turn in the following sections.
What’s the deal with type()?
type() is a function. You can think of functions in Python in a few different ways:
- A way of doing something in Python.
- A way of saving some code for reuse.
- A way of taking an input, transforming that input, and returning an output. The input goes in the parentheses
These are all valid ways of thinking about functions. We’ll be learning more about functions in later sections.
Challenges for lesson 2
Open your web browser, and google the phrase “python function.” Skim through the first few results. What words do you recognize, and which ones look unfamiliar? What do you think the unfamiliar ones mean? Try to rephrase some of this new language, and guess what they mean in your own words.
Assignment: ChallengeOpen your web browser, and google the phrase “python function.” Skim through the first few results. What words do you recognize, and which ones look unfamiliar? What do you think the unfamiliar ones mean? Try to rephrase some of this new language, and guess what they mean in your own words.
When you google “python function,” you may see some phrases that look unfamiliar, like “return value” or “pass parameters.” These are advanced terms for inputting and outputting data from a function. It’s important to become familiar with the Python’s terminology about functions, as it will be helpful later on when you start working with these components.
Select all the following that accurately describe the data type categories.
Select all the following that accurately describe the data type categories.(Select all that apply)
Terms Used in Lesson
Can you define the terms below? Hover over each of them to read a preview of the definitions.
A data type in Python that is either “True” or “False” and is often used as a basic building block for logical operations. They are implied in conditional statements (which check if expressions are true …
A data type in Python that represents numbers with decimal points.
Functions are ways of doing things in Python. They take some input and do something with it, and can return a value. For example, a function can return the results of a calculation. If your …
An integer is a data type, like string, list, boolean, and float. It signifies any value that is a whole number. You can check if a value is an integer with the type() function, passing …
A list is a data type, like string, integer, float, and Boolean. A list holds items in a sequence, separated by commas and contained within square brackets. They can hold different types of data, like …
A data type in Python that represents a sequence of alphanumeric and/or special characters enclosed within single or double quotes. For example, the following are all strings: "hello world!" "The answer is 3" "!!!"
Python type() is a function that evaluates the data type of the object within the parenthesis. For a string "hello world", it will return: <str>; for an integer 5, it will return <int> and so …