Written by PIRATE KING
Python has several built-in data types. Data types classify values and determine what operations can be performed on them. When you assign a value to a variable, Python automatically assigns its data type. In this section, we'll explore some common ones.
In computer science, text is called a "string" because it represents a sequence—or "string"—of characters arranged in order. This term comes from the idea of characters being linked together like beads on a string. Unlike other data types such as numbers, which represent quantities, strings specifically represent ordered text data. The concept has been part of programming terminology since the 1960s and is now universally used to denote text in code.
For example, assigning full_name = "Pirate King" stores the text (string) "Pirate King" in the variable full_name. When working with text in your programs, remember to enclose it in quotation marks "" (or '').
In Python, there are various ways to represent a number. Let's take a look at the most common two.
In programming, an integer is a whole number that can be positive, negative, or zero. For example, age = 20, net_gain = -1, and zero = 0.
Execute, and you get 20, -1, and 0.
We can also work with numbers that have decimals. For example, let's create a variable called pi and set it to 3.14. This type of number is called a floating-point number, or float for short. It's a common term you'll hear in almost any programming language. Print it out, run the code, and you'll get 3.14.
A Boolean is a simple data type that can only be one of two values: True or False. The term comes from George Boole, a 19th-century mathematician who created Boolean algebra. It's used to represent yes/no, on/off, or any condition that can have just two possible answers. For example, you can use booleans to check if something is true or false and control how your program runs based on that. Here, we have is_logged_in set to True, and is_asia set to False.
Run this and you get True and False. These are Python's Boolean values. Remember, Python is case sensitive — so Booleans must always start with a capital letter. Writing false or FALSE won't work; only True and False are valid Boolean values in Python.
Run this and you get a NameError.
In Python, None is a special value that means "no value" or "nothing." It's its own data type called NoneType. You'll often see None used when a function doesn't return anything, or as a placeholder when you want to indicate the absence of a value. It's different from 0, False, or an empty string ""—None simply means no value at all.
That wraps up the common data types in Python. Before moving on, let me quickly introduce a handy function. Remember how Python automatically sets a variable's data type based on its value? Let's check that with this function.
The type() function in Python tells you the data type of any object or value. Just pass the value or variable to type(), and it will return its class, like 'int' for numbers or 'str' for text. Let's check it out.
We'll learn about classes later. The key takeaway is that type() is a handy way to check what kind of data you're working with.
list for ordered, mutable collections of itemstuple for ordered, immutable collections of itemsrange for sequence of numbers, often used for loopingdict for dictionaries or collections of key-value pairsset for collections of unique itemscomplex for complex numbersbytes for sequence of bytes1. Which of the following correctly defines a string in Python?
2. Select ALL valid Python data types from the list:
3. Python Boolean values must start with uppercase letters (True, False). The lowercase forms true and false are invalid.
4. What does the keyword None represent in Python?
5. Write a Python program that:
boolean_expression that stores the result of the expression 10 > 5boolean_expressionExpected output:
True <class 'bool'>