Exception handling in Python

During program execution, unexpected situations may occur: division by zero, access to a non-existent file, incorrect user input, or network connection failures. Such situations are called exceptions. Python provides a powerful and flexible exception handling mechanism that allows developers to prevent program crashes and properly respond to errors. What Is an Exception? An exception is an object that describes an error occurring during program execution. When an exception occurs, the normal flow of the program is interrupted. print(10 / 0) This code produces a ZeroDivisionError because division by zero is mathematically impossible. The try-except Block The primary mechanism for handling exceptions in Python is the try-except block. Code that may cause an error is placed inside the try block, while the error handling logic is placed inside the except block. try: number = 10 / 0 except ZeroDivisionError: print("Division by zero is not allowed.") If a...

Python Data Types

The Python programming language is known for its simplicity and flexibility, making it popular among both beginners and experienced developers. One of the key concepts in Python is its data type system, which allows working with different kinds of information: numbers, text, collections, and logical values. Understanding data types is a fundamental step toward effective programming.

Dynamic Typing

Python uses dynamic typing, which means that the type of a variable is determined automatically at runtime. Developers do not need to explicitly declare types, as is required in many other programming languages.

x = 10        # integer (int)
x = "hello"   # now it is a string (str)

This makes the code more flexible, but it also requires careful attention, as type-related errors may occur during execution.

Numeric Types

Python supports several numeric types:

int — integers of arbitrary size:

a = 42
b = -100

float — floating-point numbers:

x = 3.14
y = -0.001

complex — complex numbers:

z = 2 + 3j

Strings (str)

Strings are used to represent text. In Python, they are immutable, meaning they cannot be changed after creation.

text = "Hello, world!"
print(text.upper())

Python supports indexing and slicing of strings:

word = "Python"
print(word[0])    # P
print(word[1:4])  # yth

Boolean Type (bool)

The bool type has only two values: True and False. It is widely used in conditional statements.

is_active = True
print(5 > 3)  # True

Lists (list)

A list is an ordered and mutable collection of elements. Lists can contain elements of different types.

numbers = [1, 2, 3, 4]
mixed = [1, "text", 3.14]
numbers.append(5)

Lists support indexing, slicing, and numerous methods for data manipulation.

Tuples (tuple)

Tuples are similar to lists but are immutable. This means they cannot be changed after creation.

point = (10, 20)

Tuples are often used to store fixed collections of data.

Sets (set)

A set is an unordered collection of unique elements. It is useful for performing set operations such as union, intersection, and difference.

unique_numbers = {1, 2, 3, 3}
print(unique_numbers)  # {1, 2, 3}

Dictionaries (dict)

A dictionary is a collection of key-value pairs. Keys must be unique and immutable.

person = {
    "name": "Ivan",
    "age": 25
}
print(person["name"])

Dictionaries are widely used to represent structured data.

NoneType

The NoneType type has a single value — None. It is used to represent the absence of a value.

result = None

Type Checking

The type() function is used to determine the type of a variable:

x = 10
print(type(x))  # <class 'int'>

You can also use isinstance() to check if a value belongs to a specific type:

print(isinstance(x, int))  # True

Data types in Python provide flexibility and expressiveness. Dynamic typing simplifies development, while a rich set of built-in types allows efficient handling of various data structures. Understanding these types is an essential step toward writing reliable and readable code.

Original: www.webjdev.com/2026/05/python.html

Comments

Popular posts from this blog

Apache Camel for Service Integration

Exception handling in Python