What kind of language is Python?
Start with concise syntax, lists, dictionaries, and functions
The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.
Python is a programming language that is great for quickly expressing ideas with less code. It is used in many fields such as automation, data analysis, artificial intelligence, and web servers.
Indentation creates code blocks
Python expresses the scope of conditional statements and functions with indentation instead of braces.
def calculate_damage(attack, defense):
damage = attack - defense
if damage < 1:
return 1
return damage
print(calculate_damage(12, 5))
Since indentation is not just a decoration but part of the grammar, spacing must be kept consistent in the same block.
Lists and dictionaries
Lists store multiple values in order, and dictionaries connect keys and values.
players = ["Rose", "Knight", "Mage"]
inventory = {
"potion": 3,
"key": 1,
}
for player in players:
print(player)
print(inventory["potion"])
Python variables do not necessarily need to have type names. Instead, as your function gets larger, type hints make it easier to convey your intent, since some type errors may not be discovered until you run them.
def add_score(current: int, amount: int) -> int:
return current + amount
Modules and virtual environments
Functions from other files and standard libraries are brought in with import. External packages are usually installed with pip.
Because each project may need different package versions, it is best to isolate its dependencies in a virtual environment with venv.
python -m venv .venv
Activate the .venv environment
pip install required packages
Python is not the fastest language for every task, but it has a fast implementation and many libraries. If you start with small file organizing tools or automating repetitive tasks, you can learn the grammar and how to use them.