Lecture 3 : Data Types in Python | Python Full Course Free

preview_player
Показать описание
Python Data Types
In Python, a data type defines the kind of value a variable can hold. Python automatically assigns the type of a variable based on the value you provide, so you don’t need to declare it explicitly.

Here’s a detailed explanation of the different data types in Python:

1. Numeric Types
int: Whole numbers (e.g., 10, -3, 1000).
float: Decimal numbers (e.g., 3.14, -0.5, 2.0).
complex: Numbers with real and imaginary parts (e.g., 2 + 3j).
Example:

python
Copy code
x = 10 # int
y = 3.14 # float
z = 2 + 3j # complex
2. Text Type
str: A sequence of characters, such as text or words (e.g., "Hello", 'Python').
Example:

python
Copy code
name = "Alice"
greeting = 'Hello, World!'
3. Boolean Type
bool: Represents True or False.
Example:

python
Copy code
is_active = True
is_logged_in = False
4. Sequence Types
These are used to store multiple items in an ordered way.

list: A collection that is ordered and changeable (e.g., [1, 2, 3] or ["apple", "banana"]).
tuple: A collection that is ordered but unchangeable (e.g., (1, 2, 3)).
range: A sequence of numbers (e.g., range(5) generates 0, 1, 2, 3, 4).
Example:

python
Copy code
fruits = ["apple", "banana", "cherry"] # list
coordinates = (10, 20, 30) # tuple
numbers = range(5) # range
5. Set Types
These store multiple items but in an unordered way and ensure all items are unique.

set: A collection of unique, unordered items (e.g., {1, 2, 3}).
frozenset: An immutable version of a set.
Example:

python
Copy code
unique_numbers = {1, 2, 3, 3, 4} # {1, 2, 3, 4}
immutable_set = frozenset([1, 2, 3])
6. Mapping Type
dict: A collection of key-value pairs (e.g., {"name": "Alice", "age": 25}).
Example:

python
Copy code
person = {"name": "Alice", "age": 25}
print(person["name"]) # Output: Alice
7. Binary Types
bytes: Immutable sequence of bytes (e.g., b"Hello").
bytearray: Mutable sequence of bytes.
memoryview: A view object for binary data.
Example:

python
Copy code
binary_data = b"Hello"
mutable_binary = bytearray(5)
8. None Type
NoneType: Represents the absence of a value, i.e., "nothing."
Example:

python
Copy code
value = None
Type Checking
To check the type of a variable, use the type() function:

python
Copy code
x = 42
print(type(x)) # Output: class 'int'
Type Conversion
You can convert between types using functions like int(), float(), str(), etc.

Example:

python
Copy code
x = "100"
y = int(x) # Converts string to int
print(y + 10) # Output: 110
Summary Table
Data Type Description Example
int Whole numbers 10, -5
float Decimal numbers 3.14, -0.5
complex Real and imaginary numbers 2 + 3j
str Text (sequence of chars) "Hello"
bool True/False True, False
list Ordered, changeable list [1, 2, 3]
tuple Ordered, unchangeable tuple (1, 2, 3)
set Unordered, unique items {1, 2, 3}
dict Key-value pairs {"key": "value"}
NoneType Represents no value None
Let me know if you'd like to explore any of these in more detail with examples! 😊
Рекомендации по теме
visit shbcf.ru