Python Data Types
Python supports several data types, each designed for specific purposes. Here are some of the most commonly used data types in Python with examples:
- Integers (
int): Integers are whole numbers without a decimal point. Example. x = 5 y = -10 - Floats (
float): Floats represent real numbers with a decimal point.Example: 3.14159 temperature = 98.5 - Strings (
str): Strings are sequences of characters enclosed in single or double quote. Example: "Alice" , 'Hello, World!' - Lists (
list):Lists are ordered collections of items, which can be of different data types. Example: "apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5] - Tuples (
tuple):Tuples are similar to lists but are immutable (cannot be changed after creation). - Example (3, 4) colors = ("red", "green", "blue"
- Dictionaries (
dict):Dictionaries store key-value pairs, allowing you to access values by their keys. - Example: {"name": "John", "age": 30, "city": "New York"}
- Sets (
set):Sets are unordered collections of unique elements. - Example: {1, 2, 3, 4, 5} unique_letters = set("hello")
- Booleans (
bool): Booleans represent eitherTrueorFalse.Example: TRUE. FALSE - None (
NoneType): None represents the absence of a value. It is often used to signify that a variable has no value. - Example:
- None
- Custom Data Types (Classes): You can create your own custom data types using classes in Python.
- Example: class Person: def __init__(self, name, age): self.name = name self.age = age person1 = Person("Alice", 25)These are the fundamental data types in Python. Python is a dynamically-typed language, meaning you don't need to explicitly specify the data type when declaring a variable. Python determines the data type dynamically based on the value assigned to the variable.
pi =
- =
person =
- unique_numbers =
- ble =
Comments
Post a Comment