This lesson on Python Basic Datatypes is hands-on and example-driven. You will be able to identify, create, and manipulate the four fundamental Python data types: integers, floats, booleans, and strings. You will learn how to check a variable's type, convert between types, and correctly handle text data using various quoting and escape techniques.
What You'll Be Able To Do
- Determine the data type of any variable or literal value using the built-in type() function.
- Create variables holding numeric (integer and float) values and perform basic type conversions.
- Define Boolean variables using the required capitalization for True and False.
- Construct complex strings using single quotes, double quotes, and escape characters to handle internal punctuation.
- Combine numeric variables and strings into a single output using type conversion and concatenation.
Detailed Concept Walkthrough
1. Numeric Types: Int and Float
Numeric data is categorized into integers (whole numbers) and floats (real numbers with a decimal point). Python automatically infers the type upon assignment.
- Mechanism: Integers (
int) are positive or negative whole numbers (e.g., 5, -6). Floats (float) are real numbers containing a decimal point (e.g., 4.75). - Under the Hood: Python uses the
type()function to verify the data type of a variable or a literal value, returning 'int' or 'float'. - Type Conversion: The built-in functions
int()andfloat()can convert values;int()truncates the decimal part of a float (e.g.,int(4.75)becomes 4).
x1 = 5
x2 = 4.75
print(type(x1)) # Output: <class 'int'>
print(int(x2)) # Output: 4 (truncates)
Key Takeaway: Python automatically assigns numeric types, but you must use
int()orfloat()to explicitly change them.
2. Boolean Type: True or False
The Boolean type (bool) represents logical states, corresponding to the machine's binary logic (1s and 0s). A variable can only hold one of two values: True or False.
- Syntax Rule: Boolean values must be written with an initial capital letter (
TrueorFalse). - Mechanism: If
TrueorFalseare typed in lowercase, Python interprets them as undefined variables, resulting in an error. - Under the Hood: The
type()function confirms the variable's type as 'bool'.
x3 = True
print(type(x3)) # Output: <class 'bool'>
Key Takeaway: Always capitalize the first letter of
TrueandFalsewhen defining Boolean variables.
3. String Type and Manipulation
Strings (str) are sequences of characters used to represent text data, defined by enclosing the text in quotes.
- Syntax Rule: Strings can be defined using either single quotes (
'George') or double quotes ("George"). - Best Practice: Use differing outer and inner quotes to handle internal punctuation, such as an apostrophe (e.g.,
"I'm fine"). - Escape Characters: A backslash (
\) changes the interpretation of the character immediately following it, allowing quotes to be included (e.g.,'I\'m fine'). - Concatenation: The plus sign (
+) is used to combine (concatenate) two strings.
x4 = 'George'
statement = "I'm fine"
print('Red' + ' ' + 'car') # Concatenation with space
Key Takeaway: Use quotes to define text values, ensuring the outer quotes differ from any internal quotes or using the backslash escape character.
Topics Covered in Python Basic Datatypes
- Integers and Floats (0:30 - 1:00) — Numbers are categorized as integers (whole) or floating points (decimals).
- Checking Data Type (1:00 - 1:40) — The built-in type() function verifies the data type of a variable or value.
- Type Conversion (2:00 - 2:30) — int() and float() functions convert values between numeric types.
- Boolean Values (2:40 - 3:30) — The Boolean type holds True or False, which must be capitalized.
- Defining Strings (3:45 - 4:30) — Strings are text values enclosed in single or double quotation marks.
- Type Mismatch Error (5:00 - 5:45) — Python raises an error if you try to combine different types (like int and str) using the + operator.
- String Conversion (5:45 - 6:15) — The str() function converts numeric values into text for concatenation.
- Handling Quotes (6:45 - 7:30) — Use differing outer and inner quotes or the backslash escape character to include quotes within a string.
- String Concatenation (7:45 - 8:30) — The plus sign (+) joins two strings, while a comma in print() separates them with a space.
- Printing Multiple Values (8:30 - 9:00) — Listing values separated by commas without print() displays them as a tuple in parentheses.
Python Cheat Sheet
-
type(variable)— Checks the data type of a valuetype(4.75) -
int(value)— Converts a value into an integerint(4.75) -
str(value)— Converts a value into a stringstr(10) + " Dollars" -
True / False— Boolean values used for logicx3 = True -
'text' or "text"— Defines a sequence of charactersx4 = "George" -
string1 + string2— Joins two strings together (concatenation)'Red' + ' car' -
print(val1, val2)— Prints values separated by a spaceprint('Red', 'car') -
\'— Escape character for internal quotesprint('I\'m fine')
Comparison Table
| Data Type | Description | Example |
|---|---|---|
| Integer (int) | Whole numbers, no decimal point. | 5 or -6 |
| Float (float) | Real numbers, includes a decimal point. | 4.75 |
| Boolean (bool) | Logical state, must be True or False. | True |
| String (str) | Sequence of characters (text). | 'George' |
Common Pitfalls
- Mistake: Using lowercase true or false for Boolean values. Avoid: Always capitalize the first letter: True or False.
- Mistake: Concatenating a number and a string directly with +. Avoid: Convert the number to a string first using str().
- Mistake: Using the same quotes internally and externally for a string. Avoid: Use double quotes outside if the string contains a single quote.
- Mistake: Assuming Python knows a word is text without quotes. Avoid: Always enclose text values in single or double quotes.
FAQs
- Why does int(4.75) result in 4, not 5? The int() function truncates the decimal part of a float; it does not perform standard mathematical rounding.
- Can I use single quotes inside a string defined by single quotes? Yes, but you must precede the inner single quote with a backslash () to escape it, like 'I'm fine'.
- Does Python require me to declare variable types? No, Python automatically guesses the type of data you are entering, unlike some other programming languages.
- What is the difference between using + and using a comma in the print() function? The + operator concatenates strings without adding spaces; the comma automatically separates printed values with a blank space.