Feature

Basic Characteristics of Python#

Python is a multipurpose programming language and its characteristics are as follows.

  1. Readable Syntax
    Python’s syntax is intuitive and clear, which enhances the readability of the code. This improves maintainability and productivity.

  2. Dynamic Typing
    Python supports dynamic typing, which allows you to use variables without explicitly declaring their types. The type of a variable is determined at runtime.

  3. Interpreter Language
    Python is an interpreted language, executing code line by line. This enhances debugging and development speed, but may result in slower execution speeds compared to compiled languages.

Image editing

Letting AI Edit Images#

Is AI good at editing images?#

Editing images, such as removing specific objects or changing their colors, has become an area where AI excels.
Let’s take a look at how well it performs.
The image on the left is the original, and the one on the right has been modified by AI.

  • Sample 1
    Prompt : “Please remove the apple from the picture.”

The AI smoothly erased the apple as follows.
AI image editing sample 1 - removing an apple

Data type

Python Data Types#

Python has various data types, each with its own unique characteristics and purposes.
I will explain the main data types.

1. Numeric Types#

  • Integer (int): An integer data type. It includes both positive and negative numbers and has no size limit.
a = 10
b = -5
  • Floating Point (float): A floating-point data type that includes decimal points.
c = 3.14
d = -0.5
  • Complex Numbers (complex): A complex number data type with a real part and an imaginary part.
    The imaginary part is represented using j.
e = 1 + 2j

2. Sequence Types#

  • String (str): A collection of characters enclosed in single quotes (’) or double quotes (").
f = "Hello, World!"
  • List (list): A mutable sequence type that stores multiple values in order.
    It is defined using square brackets ([ ]) and can include different data types.
g = [1, 2, 3, 'a', 'b', 'c']
  • Tuple (tuple): Similar to a list but immutable.
    It is defined using parentheses (( )).
h = (1, 2, 3, 'a', 'b', 'c')

3. Set Types#

  • Set (set): A collection of unique elements.
    It is unordered and defined using curly braces ({ }).
i = {1, 2, 3, 4, 5}
  • frozenset: An immutable set.
    It is created using the frozenset() function.
j = frozenset([1, 2, 3, 4, 5])

4. Mapping Types#

  • Dictionary (dict): A collection of key-value pairs.
    It is defined using curly braces ({ }) and the keys must be unique.
k = {'name': 'Alice', 'age': 25}

5. Other Types#

  • Boolean (bool): A type that represents True and False.
l = True
m = False
  • None: A type that represents the absence of a value.
n = None

Casting

Casting in Python (Type Conversion)#

In Python, “casting” means converting a data type to another data type.
Since Python is a dynamically typed language, it is sometimes necessary to explicitly convert the data type of a variable.

1. int -> float#

In the following code, the variable a is of int type.
Since a is cast to float type as b = float(a), b is of float type and its value is the floating-point number 10.0.

List

Python List#

In Python, a list is an ordered and mutable collection of data.
A list can contain items of various data types, and the items can be modified, added, or deleted.

Let’s assume we need to handle 10,000 values.
Creating 10,000 variables would be highly inefficient.
Therefore, Python provides lists to efficiently handle multiple values.

1. Creating a List#

A list can be created using [ ] as follows.
You can place multiple values inside the [ ] to handle them as a single list.
In the following example, integers, floats, and strings are all included in a single list.
An important feature is that a single list can contain multiple data types.
Each value contained in a list is called an element.

Read/Write a file

How to Read and Write Files in Python#

This guide explains how to read and write files in Python.
It is organized around the most commonly used example codes.

1. Reading a Text File - Reading the Entire File#

You can create a file stream using open("abcd.txt", "r") and read the entire file at once with .read().
(Creating a file stream means establishing a link to access the file.)
Here, "abcd.txt" refers to the full path of the file to be read, and you can use either an absolute path or a relative path.
"r" stands for read mode.