This document introduces Python data types, including integers, floats strings, booleans, and typecasting. It explains how Python represents and converts data types, with practical examples and key concepts for beginners.
This document explains Python's core data types—integers, floats, strings, and booleans—along with typecasting and practical usage. Readers will learn how Python represents, converts, and manipulates different types of data for programming and analysis.
Python uses data types to represent different kinds of values. Understanding these types is essential for effective programming and data analysis.
Python supports several fundamental data types:
| Expression | Data Type |
|---|---|
| 11 | int |
| 21.213 | float |
| “words” | str |
Python provides tools to check and convert data types:
Use the type() command to determine the type of a value.
1# Example usage
2print(type(11)) # int
3print(type(21.213)) # float
4print(type("words")) # str
Typecasting changes the type of a value:
float(2) yields 2.0.int(1.1) yields 1 (information may be lost).str(2) or str(2.0).Booleans represent logical values:
True (uppercase T)False (uppercase F)Use type(True) or type(False) to check the type, which returns bool.
True to int or float yields 1.False to int or float yields 0.1 to bool yields True.0 to bool yields False.Typecasting can result in loss of information, especially when converting floats to integers. Attempting to convert a string that does not contain an integer value to int will result in an error.
For more examples, refer to Python documentation or lab exercises.
Understanding Python’s fundamental concepts and building blocks helps in writing clear and effective code.
| Term | Definition | Example |
|---|---|---|
| Literals | Fixed values assigned to variables or directly used in expressions. | 5 (integer), 3.14 (float), "Hello" (string), True (boolean), [1, 2, 3] (list) |
| Axioms | Foundational, universally accepted principles or rules that form the basis of the language semantics. | - “Indentation defines blocks of code.” - “Everything in Python is an object.” |
| Keywords | Reserved words that have special meanings and cannot be used as identifiers. | if, else, for, while, def, class, import, return, with, etc. |
| Operators | Symbols or words that perform operations on variables or values. | + (addition), - (subtraction), * (multiplication), and, or, not |
| Data Types | Types of data Python can manipulate, like numbers, strings, lists, etc. | int, float, str, list, tuple, dict, set, bool |
| Identifiers | Names used to identify variables, functions, classes, etc. | x, y, function_name, MyClass |
| Expressions | Combinations of values, variables, and operators that Python evaluates to produce a result. | x + 5, len(my_list), True and False |
| Statements | Instructions that Python executes, like assignments or function calls. | x = 5, print("Hello"), if x > 0: |
| Functions | Reusable blocks of code designed to perform specific tasks. | python<br>def add(a, b):<br> return a + b |
| Classes | Blueprints for creating objects, encapsulating data and behavior. | python<br>class Dog:<br> def __init__(self, name):<br> self.name = name |
| Objects | Instances of classes that hold data and methods defined by the class. | python<br>dog = Dog("Buddy") |
Literals are the simplest building blocks of a programming language, representing constant values.
| Type of Literal | Description | Examples |
|---|---|---|
| Numeric Literals | Numbers (integers, floats, complex). | 42 (int), 3.14 (float), 1+2j (complex) |
| String Literals | Text enclosed in quotes. | "Hello", 'Python' |
| Boolean Literals | Logical values. | True, False |
| Special Literals | Denote the absence of value. | None |
| Collection Literals | Represent collections such as lists, tuples, sets, and dictionaries. | [1, 2, 3], (1, 2), {1, 2, 3}, {"key": "value"} |
Axioms are foundational principles or “rules of the game” for Python.
| Python Axiom | Explanation |
|---|---|
| Indentation defines code blocks. | Unlike braces {} in many languages, Python uses indentation to denote code hierarchy. |
| Everything is an object. | Python treats everything (e.g., numbers, strings, functions) as objects with properties. |
| Dynamic typing. | Variable types are determined at runtime, not explicitly declared by the programmer. |
| Strong typing. | Python enforces type correctness and won’t automatically convert incompatible types. |
| Readability counts (The Zen of Python). | Python emphasizes code readability and simple syntax as a guiding design philosophy. |
| Concept | Example |
|---|---|
| Expression | x + y evaluates to the sum of x and y. |
| Statement | x = 5 assigns the value 5 to the variable x. |
| Looping Construct | for i in range(3): print(i) outputs 0, 1, 2. |
| Conditional | if x > 0: print("Positive") else: print("Non-positive") |
| Data Type | Description | Example |
|---|---|---|
| int | Integer values like 42 or -10. | 42, -10 |
| float | Floating-point numbers with decimal points. | 3.14, 2.718 |
| str | Sequence of characters enclosed in quotes. | "Hello", 'Python' |
| bool | Boolean values representing True or False. | True, False |
| list | Ordered collection of items. | [1, 2, 3], ["a", "b", "c"] |
| tuple | Immutable ordered collection of items. | (1, 2, 3), ("a", "b", "c") |
| dict | Unordered collection of key-value pairs. | {"key1": "value1", "key2": "value2"} |
| set | Unordered collection of unique items. | {1, 2, 3}, {"apple", "banana", "cherry"} |
Python’s data types—int, float, str, and bool—form the foundation for programming and analysis. Understanding typecasting and type checking helps prevent errors and enables flexible data manipulation.
(3) Casting a non-integer string to int results in an error, not 0.
| Type | Description |
|---|---|
| A. int | 1. Sequence of characters |
| B. float | 2. Logical value, True or False |
| C. str | 3. Whole number, positive or negative |
| D. bool | 4. Real number, includes decimals |
A-3, B-4, C-1, D-2.
Typecasting a float to an integer in Python may result in loss of information.
True. Only the integer part is retained, and the fractional part is discarded.