Course Content
Python MySQL
Python provides several libraries for working with MySQL databases. One popular library is mysql-connector-python, which allows you to connect to a MySQL database, execute queries, and fetch results.
Python MongoDB
Python SQLite
Python Questions
Python Programs
Mastering Python Programming: A Comprehensive Guide for Beginners
About Lesson

Python provides a rich set of built-in data types that serve various purposes. Let’s delve into each category to gain a comprehensive understanding:

Python’s Built-in Data Types 

Python’s built-in data types can be broadly classified into the following categories:

  1. Numeric Types: int, float, complex

  2. Sequence Types: list, tuple, range

  3. Text Type: str

  4. Mapping Type: dict

  5. Set Types: set, frozenset

  6. Boolean Type: bool

  7. Binary Types: bytes, bytearray, memoryview

Numeric Types 

Python supports three numeric data types: integers, floating-point numbers, and complex numbers.

  • int : Integers are whole numbers, either positive, negative, or zero. In Python, integers have no size limitation, constrained only by available memory.
a = 42 
b = -7 
  • float : Floating-point numbers represent real numbers with a decimal point. They can be specified using a decimal point or scientific notation.
c = 3.14 
d = 1.23e-4 
  • complex : Complex numbers consist of a real and an imaginary part, represented as x + yi , where and are real numbers, and is the imaginary unit.
e = 2 + 3j 

Sequence Types 

Sequence types represent ordered collections of elements, which can be indexed and sliced.

  • list : Lists are mutable, ordered collections of elements enclosed in square brackets. They can store elements of different data types, including other lists.
fruits = ["apple", "banana", "cherry"] 
nested_list = [1, 2, [3, 4]] 
  • tuple : Tuples are similar to lists but are immutable, meaning their elements cannot be modified once assigned. Tuples are enclosed in parentheses.
coordinates = (32.7767, -96.7970) 
  • range : The range type represents an immutable sequence of numbers, commonly used in loops. It is generated using the range() function.
for i in range(5): 
    print(i) # Prints numbers from 0 to 4 

Text Type 

  • str : Strings are immutable sequences of Unicode characters, enclosed in either single or double quotes. Strings can be concatenated, indexed, and sliced.
greeting = "Hello, World!" 
print(greeting[0]) # Prints 'H' 

Mapping Type 

  • dict : Dictionaries are mutable, unordered collections of key-value pairs, enclosed in curly braces. Keys must be unique and hashable, while values can be of any data type.
person = { "name": "John", "age": 30, "city": "New York" }

Set Types 

Set types represent unordered collections of distinct elements.

  • set : Sets are mutable, unordered collections of unique elements, created using curly braces or the set() constructor. Sets do not support indexing or slicing.
my_set = {1, 2, 3, 4, 5} 
my_set_from_list = set([1, 2, 3, 4, 5]) 
  • frozenset : Frozensets are similar to sets but are immutable, making them suitable for use as dictionary keys. They can be created using the frozenset() constructor.
immutable_set = frozenset([1, 2, 3]) 

Boolean Type 

  • bool : Booleans represent the truth values True and False , often used in conditional expressions and comparisons.
is_happy = True 
is_raining = False 

Binary Types 

Binary types are used to store binary data, such as images, audio files, or serialized objects.

  • bytes : Bytes objects are immutable sequences of integers in the range 0-255, created using the bytes() constructor.
byte_data = bytes([72, 101, 108, 108, 111]) 
  • bytearray : Bytearrays are mutable versions of bytes objects, created using the bytearray() constructor.
mutable_byte_data = bytearray([72, 101, 108, 108, 111]) 
  • memoryview : The memoryview type allows you to work with memory buffers directly, without copying the underlying data. It can be created using the memoryview() constructor.
byte_data = b"Hello, World!" 
memory_view = memoryview(byte_data) 

Working with Python Data Types 

To effectively use Python data types, it’s important to understand type conversion, type checking, and type-specific operations.

  1. Type Conversion : You can convert between compatible data types using built-in functions, such as int() float() str() list() tuple() set() , and dict() .
integer_value = int("42") 
float_value = float("3.14") 
  1. Type Checking : Use the type() function to determine the data type of an object, or use isinstance() to check if an object is an instance of a specific class or a tuple of classes.
print(type(42)) # Prints <class 'int'> print(isinstance(42, int)) # Prints True 
  1. Type-Specific Operations : Each data type supports specific operations, such as arithmetic operations for numeric types, string formatting for strings, and set operations for sets.
result = 3 * 4 # Arithmetic operation 
formatted_str = f"Hello, {name}!" # String formatting 
union_set = set1 | set2 # Set union