What Is Python? A Beginner-Friendly Programming Language
Python is a high-level, general-purpose, dynamically typed programming language that lets you build almost any kind of project, from scripts to machine learning models, with concise and readable syntax. Python is relatively easy to learn and beginner-friendly. It is open-source and free for anyone to use, ships with well-tested machine learning libraries such as scikit-learn and TensorFlow, and has a very supportive community. This post overviews the basic syntax of the Python programming language, which is useful for beginners or people who move quickly from another programming language to Python.
Why Use Python for Machine Learning and General-Purpose Programming
Python is a general-purpose, object-oriented programming language. It was created by Guido Van Rossum initially thought of as a hobby project in 1989 during Xmas vacation. Python is relatively easy to learn and beginner-friendly. I like Python because you can program any kind of project with it. It is open-source and free for anyone to use. Python has well-tested machine learning libraries and a very supportive community. I will overview herein a basic syntax of the Python programming language. This will be useful for beginners or people who move quickly from another programming language to Python.
Python Basic Syntax: Variables and Dynamic Typing
In Python, we do not need to declare types of variables as we do in Java programs. We can declare variables in any place in the code. The variables and the data types they store can also be changed dynamically during code execution. This is why Python is called a “dynamically typed programming language”: a variable’s type is bound at runtime, not at declaration.
# This is a comment, starting with #
# Below we declare a variable named "birds", assigned an integer value
birds = 5
# We can print here a number of birds, and use %d to place the birds value into the string
print("We have %d birds in stock!"%birds)
We have 5 birds in stock!
# Type of the variable "birds" type(birds)
int
# Next, we can change the "birds" variable into a list with bird names birds = ["Eagle", "Chicken", "Stork", "Swan"]
# Type of the variable "birds" changed "on the fly"! type(birds)
list
# The Python len() method is a built-in function calculating the length of any iterable object,
# such as our list of birds.
# We also use here "if-elif-else" statement to test how many birds are in list
if len(birds)<5:
print("We have too few birds at the moment.")
elif len(birds)==5:
print("It is all right, 5 birds in stock!")
else:
print("Too many birds need food!")
We have too few birds at the moment.
Dictionary data structure contains keys and variables. For instance, we can keep wing spans of birds in the dictionary “wing_spans.”
# an empty dictionary
wing_spans={}
# create a list of wing spans in sentimeters for each bird in the "birds" list: # and store it in a temporary variable temp temp=[200, 50, 110, 240]
Alternatively, we could just define the dictionary by hand, for each bird:
wing_spans=dict(zip(birds, temp)) wing_spans
{'Eagle': 200, 'Chicken': 50, 'Stork': 110, 'Swan': 240}
wing_spans = {'Eagle': 200, 'Chicken': 500, 'Stork': 110, 'Swan': 240}
for key, value in wing_spans.items():
print("%ss' wing span is about %d santimeters."%(key, value))
Eagles' wing span is about 200 santimeters.
Chickens' wing span is about 500 santimeters.
Storks' wing span is about 110 santimeters.
Swans' wing span is about 240 santimeters.
It seems that chicken’s wing span is the largest among our birds! This cannot be. Let’s fix it and correct 500 centimeters of wing span for chickens:
wing_spans['Chicken'] = 50 wing_spans
{'Eagle': 200, 'Chicken': 50, 'Stork': 110, 'Swan': 240}
Build-in max and min functions to get minimum and maximum of their argument values are as follows:
max(wing_spans.values()) # values() gets all the integer values from the dictionary wing_spans!
240
min(wing_spans.values())
50
Python List Comprehensions: Concise Syntax and Examples
A list comprehension is a Python construct that builds a new list from an existing iterable (list, tuple, range, or string) in a single, concise expression. List comprehensions have a concise syntax:
# squares of numbers from range squares = [ x**2 for x in range(11)] print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# string operations [x.upper() for x in "hello!"]
['H', 'E', 'L', 'L', 'O', '!']
Here we open a log text file and find lines with the string “FAIL” in it.
# for lines in file
f=open("log.txt","r")
result=[x for x in f if "FAIL" in x]
result
['3/12/2019\tMaintanance\tFAIL\n']
# using functions from math import sin [sin(x) for x in [1,2,4]]
[0.8414709848078965, 0.9092974268256817, -0.7568024953079282]
Python IDE and Tools: Writing Code in PyCharm
Where can we write Python programs? We can use any text editor that is suitable for writing programming code. However, when the code has a complex structure and grows a lot, it is better to use an Integrated Development Environment (IDE), a software application providing functionality for more comfortable coding. At the moment, I use PyCharm, which is integrated with Git for version control. For language reference and standard-library details, the official Python documentation is the authoritative source. This way, the code is stored locally and online and accessible from anywhere on the Internet. Another advantage of GIT is that we keep track of all code changes and can always roll back when something goes wrong.
Did you like this post? Please let me know if you have any comments or suggestions.
Python posts that might be interesting for youPython FAQ
When should I use a list comprehension instead of a for loop?
Use a list comprehension when you are building a new list by transforming or filtering an existing iterable in a single expression, because it is more concise and usually faster than an equivalent for loop with .append(). Use a regular for loop when the body has side effects, multiple statements, or complex branching, where a comprehension would hurt readability.
What does “dynamically typed” mean in Python?
Dynamically typed means a variable’s type is determined at runtime and can change during execution. In Python you can assign birds = 5 (an int) and later birds = ["Eagle", "Stork"] (a list) without declaring a type, unlike statically typed languages such as Java.
What is the difference between a list, a tuple, and a dictionary in Python?
A list is an ordered, mutable sequence ([1, 2, 3]); a tuple is an ordered, immutable sequence ((1, 2, 3)); a dictionary stores mutable key-value pairs ({"Eagle": 200}). Use a list for ordered collections you will modify, a tuple for fixed records, and a dictionary for fast lookups by key.
Which Python libraries are best for machine learning?
For machine learning, start with scikit-learn for classical algorithms (regression, classification, clustering) and TensorFlow or PyTorch for deep learning. NumPy and pandas handle the numerical arrays and tabular data these libraries depend on.
Python Data Structures and Comprehensions: Summary
Python is a dynamically typed, general-purpose language whose built-in data structures (lists, dictionaries, and tuples) and list comprehensions make it a productive choice for beginners and machine learning practitioners alike. In this post, I described basic data structures in Python such as lists, dictionaries, and tuples, shown some examples of code flow statements and the usage of list comprehensions. I am going to continue on Python coding in the following days. You can get the code in progress in the Colab file. Thanks for reading!
Related Reading
Enjoyed this? Get more like it.
Weekly notes on AI tools, Python, and what I'm actually building — plus a free copy of Fantastic AI: The 2026 Toolkit.