Overview
This lecture covers the four basic data types in Python—integers, floats, booleans, and strings—their properties, and how to work with them.
Python Data Types
- Python has four primary data types: integer, float, boolean, and string.
- An integer is a whole number (positive, negative, or zero) without a decimal point.
- A float is a number (positive or negative) that contains a decimal point.
- A boolean data type can only be True or False.
- A string is a sequence of characters surrounded by single or double quotes.
Working with Data Types
- The data type is set automatically when a value is assigned to a variable.
- Use the
type() method to find out the data type of a variable.
- Data types can be explicitly converted (cast) using built-in functions.
Strings in Detail
- Strings are arrays of characters, meaning they are iterable (can loop over each character).
- Strings are immutable; their contents cannot be changed after creation.
- You can declare multi-line strings using special syntax to store them in a variable.
- Access individual characters in a string using indexing (e.g.,
string[0]).
- Iterate over each character in a string using a for loop.
- Use the
len() function to get the length of a string.
- Use
in or not in keywords to check if a substring exists in a string, returning a boolean result.
Booleans and Expressions
- Booleans evaluate expressions to either True or False.
- Boolean results enable programs to make decisions based on conditions.
Key Terms & Definitions
- Integer — Whole number, positive or negative, without a decimal.
- Float — Number with a decimal point.
- Boolean — Data type with only two values: True or False.
- String — Sequence of characters enclosed in quotes.
- Immutable — Cannot be changed after creation.
- Iterable — Can be looped over, one element at a time.
Action Items / Next Steps
- Practice using
type(), casting, and string operations in Python.
- Review string slicing and modifying in the upcoming video.