2023 Python Development Setup (for a newbie)#

I’ve recently started creating boilerplates for easily getting started with projects.

Python is a language is always wanted to learn and become good at, but never really had the time to sit down and learn fully. There were always something else that need to be learned first, so it just keep going down the list of my-learn-list.

I’ve scratch the surface of setting up a developement enviroment, with Podman and Docker. An example of setting up an enviroment for Flask in docker with miniconda, would require 3 files, a docker-compose.yaml, Dockerfile.db and Dockerfile.conda:

docker-compose.yml:

version: '3'
services:
  miniconda:
    build:
      context: "./"
      dockerfile: Dockerfile.conda
    container_name: flask_conda
    networks:
      pythonNET:
        ipv4_address: 172.21.0.2
    ports:
      - "5000:5000"
    volumes:
      - ../:/app:rw
    command: tail -f /dev/null

  mariadb:
    build:
      context: "./"
      dockerfile: Dockerfile.db
    container_name: flask_db
    networks:
      pythonNET:
        ipv4_address: 172.21.0.3
    ports:
      - "3306:3306"
    volumes:
      - mariadb-data:/var/lib/mysql
      - ../:/mnt:rw

networks:
  pythonNET:
    driver: bridge
    ipam:
      config:
        - subnet: 172.21.0.0/24

volumes:
  mariadb-data:

Dockerfile.conda:

FROM continuumio/miniconda3:latest
LABEL MAINTAINER="mailaddr@domain.tld"

ENV VERSION=3.6.2
ENV NAME=example-tool

RUN apt-get update && apt-get upgrade -y

RUN conda create -n $NAME python=$VERSION --yes

Dockerfile.db:

FROM mariadb/server:10.3
LABEL MAINTAINER="mailaddr@domain.tld"

Place all the files in a folder and change the values and volumes to reflect what you need. Then simply run:

docker-compose up -d

Data types#

Below is a list of data types in python.

  • Lists
fruits = ['apple', 'banana', 'orange']
fruits.append('grape')
print(fruits)  # Output: ['apple', 'banana', 'orange', 'grape']
  • Tuples
coordinates = (3, 5)
x, y = coordinates
print(x)  # Output: 3
  • Dictornaries
person = {'name': 'John', 'age': 30, 'city': 'New York'}
print(person['name'])  # Output: John
  • Sets
unique_numbers = {2, 4, 6, 8, 2}
print(unique_numbers)  # Output: {8, 2, 4, 6}
  • Arrays (from the ‘array’ module)
from array import array
numbers = array('i', [1, 2, 3])
numbers.append(4)
print(numbers)  # Output: array('i', [1, 2, 3, 4])
  • Strings
message = "Hello, Python!"
print(message[7])  # Output: Python
  • Bytes and Butearrays
binary_data = b'\x41\x42\x43'
print(binary_data)  # Output: b'ABC'
  • Ranges
for num in range(5):
    print(num)  # Output: 0, 1, 2, 3, 4
  • Enumerations (from the ’enum’ module)
from enum import Enum
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

print(Color.RED)  # Output: Color.RED
  • Complex Numbers
z = 3 + 2j
print(z.real)  # Output: 3.0
  • Classes and Objects
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def area(self):
        return self.width * self.height

rect = Rectangle(5, 10)
print(rect.area())  # Output: 50
  • None
value = None
if value is None:
    print("Value is not set.")  # Output: Value is not set.

Roadmap#

I’ve created a roadmap for learning python, so that you might have a chance to get started, maybe even before I do :) (exercises included).

Phase 1: Introduction to Programming Concepts

1.1 Introduction to Programming [ ]

  • Read beginner-friendly resources or watch videos to understand the basics of programming logic and its importance in problem-solving.

1.2 Setting Up Your Environment [ ]

  • Follow installation guides available online to install Python on your system.

  • Choose a code editor like Visual Studio Code, PyCharm, or Jupyter Notebook for writing and running your Python code.

1.3 Basic Syntax and Output [ ]

  • Write your first Python program that uses the print() function to display a simple message.

  • Practice basic arithmetic operations like addition, subtraction, multiplication, and division.

1.4 Variables and Data Types [ ]

  • Learn how to declare variables and assign values to them.

  • Experiment with different data types: integers, floating-point numbers, strings, and booleans.

  • Combine variables and strings to create more complex output.

1.4 User Input [ ]

  • Use the input() function to get input from the user.

  • Create interactive programs that prompt the user for information.

1.5 Exercises:

  • Write a program that takes a user’s name and prints a greeting like “Hello, [Name]!”

  • Create a program that calculates the area of a rectangle. Prompt the user for the length and width and display the result.


Phase 2: Control Flow and Functions

2.1 Conditional Statements [ ]

  • Learn about the if, else, and elif statements for decision-making.

  • Write programs that make decisions based on user input or conditions.

2.2 Loops [ ]

  • Understand the while and for loops for repetitive tasks.

  • Implement loops to repeatedly perform actions until a condition is met.

2.3 Functions [ ]

  • Define functions using the def keyword.

  • Practice writing functions with parameters and return values to make your code modular.

2.4 Exercises:

  • Implement a program that checks if a given number is even or odd. Print an appropriate message.

  • Create a simple guessing game where the program generates a random number, and the user has to guess it using a loop and conditionals.

  • Write a function that calculates the factorial of a positive integer using a loop.


Phase 3: Data Structures and Intermediate Concepts

3.1 Lists and Tuples [ ]

  • Create lists and tuples containing different types of data.

  • Explore indexing, slicing, appending, and modifying elements.

3.2 Dictionaries [ ]

  • Understand how dictionaries work with key-value pairs.

  • Create dictionaries and practice accessing, adding, and modifying entries.

3.3 File Handling [ ]

  • Use the open() function to read and write to text files.

  • Practice reading and processing data from files, and writing data to new files.

3.4 Exception Handling [ ]

  • Learn about handling errors using try and except blocks.
  • Make your programs more robust by catching and handling common errors gracefully.

3.5 Exercises:

  • Develop a program that maintains a shopping list as a dictionary. Allow the user to add, remove, and display items.

  • Write a program that reads lines from a text file, replaces a specific word, and saves the modified content back to the file.


Phase 4: Advanced Topics

4.1 Object-Oriented Programming (OOP) Basics [ ]

  • Understand the concept of classes and objects as blueprints for creating instances.

  • Learn about attributes (variables) and methods (functions) within classes.

4.2 Modules and Libraries [ ]

  • Explore importing Python modules and using built-in functions.

  • Familiarize yourself with common libraries like math and random to perform advanced tasks.

4.3 Basic GUI Programming (Optional) [ ]

  • If interested, experiment with libraries like tkinter to create simple graphical interfaces.

4.4 Exercises:

  • Create a simple class representing a bank account with attributes like balance. Implement methods for depositing and withdrawing money.

  • Utilize the random module to build a number guessing game where the program generates a random number for the user to guess.


As you work through these phases and exercises, remember to Google concepts you don’t understand, seek help from programming communities, and most importantly, practice consistently. Building projects that interest you can also be a fantastic way to reinforce your skills and creativity. Happy coding!