diff --git a/_data/navigation.yml b/_data/navigation.yml index 69872ebc..c718b01b 100644 --- a/_data/navigation.yml +++ b/_data/navigation.yml @@ -36,3 +36,5 @@ getting_started_0: url: /categories/getting_started/programming_tutorial_II - title: "Python: Hello World" url: /categories/getting_started/python_hello_world + - title: "Python Packages & Venvs" + url: /categories/getting_started/python_packages_and_venvs diff --git a/_posts/2025-05-12-gs_00010_python-hello-world.md b/_posts/2025-05-12-gs_00010_python-hello-world.md new file mode 100644 index 00000000..10923a0b --- /dev/null +++ b/_posts/2025-05-12-gs_00010_python-hello-world.md @@ -0,0 +1,272 @@ +--- +layout: single +title: "Python: Hello World" +category: Getting Started +permalink: /categories/getting_started/python_hello_world +excerpt: "Your first Python Program" +description: + In this blog post you will learn how to write your first Python program. +author: Bjoern Hartmann +toc: true +toc_label: "Content" +toc_sticky: true +toc_icon: "align-justify" +sidebar: + nav: "getting_started_0" +header: + overlay_color: "#000" + overlay_filter: "0.5" + overlay_image: /assets/images/getting_started.jpeg +--- + +Welcome back to our next post where you'll finally learn how to write your first +Python program. In our latest [post](/categories/getting_started/visual_code), +we introduced VS Code, a modern and flexible IDE. The prepared profile, which we +used to set up the editor, already includes extensions for the Python language. +So, everything should be ready to get started. In another previous post about +[programming concepts](/categories/getting_started/programming_tutorial_II), we +learned about concepts, such as functions or loops, that all programming +languages have in common. We will see how these simple structures are used in a +working program. + +Python is one of the most beginner-friendly programming languages, known for its +readability and simplicity. Therefore, it's a perfect starting point for us. +Once you know how to use one language it's just a small step to learn another +language. The core principles are always the same. In our first program, we'll +implement the traditional "Hello, World!". + +## Hello World! + +A "Hello, World!" program is usually the first piece of code written when +learning a new programming language. It does something very simple: It displays +the message **Hello, World!**. While it may seem trivial, this program serves a +very important purpose. First, it helps to verify that your development +environment is set up correctly. If the program runs and outputs "Hello, World!" +to your screen, you know that your interpreter, editor, and everything else are +working as expected. Secondly, it introduces the basic syntax of the language. +It shows how to output data to the console, which is a fundamental capability in +programming. Lastly, it serves as a confidence booster. By successfully running +your first program, you're building momentum for more complex projects. + +Inside VS Code, open a folder of your choice and create a new file +_hello_world.py_. By the _.py_ extension, VS Code knows immediately that it is a +Python file. In the lower-right corner, VS Code shows that it is in Python mode +and it shows the current Python version (see the picture below). Just a quick +note on Python versions: Each version introduces new features of the programming +language. Usually, this is backward compatible. So, a program written for Python +3.6 also works for our 3.13 version. This is not true for major releases. It's +not possible to run a Python 2 program with Python 3. + +Now, write the following code and save it: + +```python + print("Hello World!") +``` + +The code is probably very easy to understand. _print_ is a Python function that +can be used to print a message to the console. The message itself has to be +within single or double quotes. To run the code you can hit the run button in +the upper-right. This will open a terminal where your Python script is executed. +If everything goes well you should see the _Hello World!_ output. Alternatively, +you can directly run the program by typing _python hello_world.py_ into the +console. + +Congrats! This was your very first Python program. + +
+ Hello World +
+ +Note that the terminal probably looks different for you. We will cover this in a +later post. + +## Python as Calculator + +In most cases, you will write all Python commands into a file and execute the +file, as we did in our _Hello World!_ program. However, sometimes it is useful +to execute the commands interactively. Inside the terminal, just type the +command + + $ python + +This will lead you to an input prompt where you can type your Python commands, +for example, the _print("Hello World!")_ command from the previous section. You +can also do calculations such as _3\*3_. This can be very handy. Just play +around with it! + +
+ Python as calculator +
+ +## Let's Build a Counter + +Now that you’ve successfully written your first Python program, let’s build +something a little more useful: a counter. This simple project will introduce us +to more key concepts in Python, such as variables, loops, functions, and +conditional statements. + +### Starting with a simple counter + +We can begin by writing a basic counter that prints numbers. Create a new file +_counter.py_ and add the following code: + +```python +count = 1 +print("Count is:", count) +``` + +In this case, we define a **variable** called count and set it to 1. The print() +function is used to output the value of count to the screen. Unlike printing a +fixed message like "Hello World", notice that we don't put the variable name in +quotes. Doing so would print the word "count" instead of its value. + +> Count is: 1 + +### Adding a while Loop + +Next, let's use a **while loop** to make the counter print numbers from 1 to 5. +A loop helps us automate repetitive tasks, so instead of manually printing each +count, we can write a single block of code that runs until a condition is met. + +Here’s how we modify the counter to use a loop: + +```python +count = 1 +while count <= 5: + print("Count is:", count) + count += 1 +``` + +In this version, the variable count starts at 1. The while loop checks whether +count is less than or equal to 5. As long as that condition is true, the loop +keeps running. Inside the loop, the program prints the current value of count, +then increases it by 1 using count += 1. Once the count reaches 6, the condition +becomes false, and the loop ends. + +An important detail here is **indentation**. Python uses indentation to show +which lines of code belong to the loop. Any indented lines after the while +statement are considered part of the loop and will be repeated each time it +runs. Without the correct indentation, Python won’t know what to include in the +loop and will raise an error. + +> Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5 + +### Encapsulating the Code in a Function + +Another useful concept in programming is the **function**. Functions allow you +to group code to be reused in different parts of your program. Let’s put our +counter logic into a function: + +```python +def simple_counter(target): + count = 1 + while count <= target: + print("Count is:", count) + count += 1 + +simple_counter(5) +``` + +Functions are **defined** by the keyword _def_ followed by a function name. +Here, we additionally add a **parameter** with the name _target_ to set the +upper limit for our counter. Observe that the previous limit _5_ is replaced by +the parameter. Also note that again indentation defines which commands belong to +the function. + +At the end, you just have to **call** the function passing a value for the +parameter. If you pass _5_, the output is exactly the same as with the previous +code without function. Using functions has three major benefits: + +1. **Code organization**: It is easy to understand your code if it is structured + in small logical blocks. +2. **Reusability**: If you need a second counter, you just need to call the + function again +3. **Abstraction**: Functions can be very complex, but often it's not required + to know how they work. It is sufficient to know what they do and how you can + use them. Take the print() function as an example. We didn't explain how it + works, but you still can use it. + +### Conditional statements + +Next, we want to improve the text output using the following logic: + +- If the count has reached the target, print "finally done". +- If the count is greater than or equal to the target minus 2, print "almost + there". +- Otherwise, print "stay patient". + +We can use **if-else** statements to directly translate this logic into +programming code: + +```python +def simple_counter(target): + count = 1 + while count <= target: + if count == target: + print("Finally done! Count is:", count) + elif count >= target - 2: + print("Almost there! Count is:", count) + else: + print("Stay patient! Count is:", count) + count += 1 + +simple_counter(5) +``` + +**if** is used to check for a condition. Note that if you want to check for +equality, you have to use "==" and **not** "=". This is a common beginner +mistake. **elif** (short for 'else if') is used to check a second condition if +the if condition is false. You can include as many elif clauses as needed. +**else** handles all remaining cases and acts as the "otherwise" in your +program. Again, indentation shows which command belongs to which structure. + +> Stay patient! Count is: 1
Stay patient! Count is: 2
Almost there! +> Count is: 3
Almost there! Count is: 4
Finally done! Count is: 5 + +### Side Note: Indentation & Style Guides + +You've learned already that indentation is crucial for structuring your program. +In Python, it plays an even more important role because it defines which +commands belong to which blocks of code. However, we haven't yet defined what +indentation actually is. The reason is that there’s no single standard and +programmers often debate the best way to handle it. On the one hand, you can use +**tabs**, which take up less space in a file but may look different depending on +the editor or environment. On the other hand, you can use **spaces**. Typically, +2 or 4 spaces are used. Spaces offer a consistent appearance across all tools. +In this blog, we’ve chosen to use four spaces, as they provide a clear and +easy-to-read structure for your code. No matter what you use, use it +consistently! + +Beyond indentation, there are many other aspects of coding style. For example, +notice that we use one empty line between the function definition and the +function call. This raises more questions: + +- Where should I use empty lines? +- What’s the ideal maximum line length? +- How should variables be named? + +There are many practical aspects that people try to standardize in so-called +**style guides**. For Python, the most popular style guide is +[PEP 8](https://peps.python.org/pep-0008/). While following such guides isn’t +mandatory, doing so helps you write clean, readable, and maintainable code. + +## Summary + +We hope you enjoyed writing your first Python program! Let's quickly recap what +we covered: + +- _Hello World_ programs are used to test your environment +- Python can be used with scripts or interactively, for example, as a handy + calculator +- You implemented your first program using basic programming concepts: + - Printing to the console + - Variables + - Loops + - Functions + - Conditional statements (if-else) +- [PEP 8](https://peps.python.org/pep-0008/) is a popular style guide for + Python. + +In our next post, we’ll build on this foundation by exploring more core Python +concepts. Until then, happy coding, and don't forget to follow us on +[X](https://x.com/bitsandvolts). diff --git a/_posts/2026-01-01-gs_00011_python-venvs.md b/_posts/2026-01-01-gs_00011_python-venvs.md new file mode 100644 index 00000000..9fca6ac0 --- /dev/null +++ b/_posts/2026-01-01-gs_00011_python-venvs.md @@ -0,0 +1,208 @@ +--- +layout: single +title: "Python: Python Packages & Virtual Environments" +category: Getting Started +permalink: /categories/getting_started/python_packages_and_venvs +excerpt: "Organize Python Packages in Virtual Environments" +description: + In this blog post, you will learn how to use a Python virtual environment to + organize Python packages for your project. +author: Bjoern Hartmann +toc: true +toc_label: "Content" +toc_sticky: true +toc_icon: "align-justify" +sidebar: + nav: "getting_started_0" +header: + overlay_color: "#000" + overlay_filter: "0.5" + overlay_image: /assets/images/getting_started.jpeg +--- + +Hello and welcome to our next post. We hope you take the great momentum of our +[last post](/categories/getting_started/programming_hello_world) where you were +able to run your first Python program. In this post, you'll learn about Python +Packages and Virtual Environments. Let's get started! + +## Python Packages + +The beauty of Python and one of the reasons why it is so popular and +beginner-friendly is the huge community: There are a lot of people in forums +that can help you if you get stuck, and there is a lot of code created by other +people that you can reuse for your own projects. + +While implementing your program, you'll face multiple challenges. Imagine you +want to plot some data or you want to solve some mathematical equation. No +matter what, chances are high that someone else has already done that exercise +before and most likely there is a Python **package** that you can download and +use to solve your problem. + +Isn't this cool? We will show a quick example in this post. + +## Virtual Environments + +One challenge is to keep all packages, that are relevant to your project, +organized. This is what **virtual environments** are for. Let's skip the theory +and directly jump into a concrete example. Typically, you create one virtual +environment (short: _venv_) per project. For the counter, that we've implemented +in our [previous post](/categories/getting_started/programming_hello_world) we +haven't created a venv yet. So, let's do it now. Here is again the code we +developed in our previous post: + +```python +def simple_counter(target): + count = 1 + while count <= target: + if count == target: + print("Finally done! Count is:", count) + elif count >= target - 2: + print("Almost there! Count is:", count) + else: + print("Stay patient! Count is:", count) + count += 1 + +simple_counter(5) +``` + +You create the venv using: + + $ python -m venv counter_venv + +This creates a new venv called counter_venv. Of course, you can choose whatever +name you like. If we want to work with a venv we have to activate it: + + $ source counter_venv/bin/activate + +You can check that it works with the following command: + + $ which python + counter_venv/bin/python + +This command returns the path to the used Python binary. In our case, it should +point to a path inside our created environment. + +## Pip - Python's Package Manager + +Let's take our counter and put some colors on the output. And yes! There is a +package called _rich_ that makes our lives very easy. For installation, we use +Python's package manager **Pip**: + + $ pip install rich + +Note that this will install the package only for the activated venv. + +## Our Counter with beautiful colors + +```python +from rich import print + +def simple_counter(target): + count = 1 + while count <= target: + if count == target: + print("[green]Finally done![/green] Count is:", count) + elif count >= target - 2: + print("[yellow]Almost there[/yellow]! Count is:", count) + else: + print("[red]Stay patient[/red]! Count is:", count) + count += 1 + +simple_counter(5) +``` + +
+ Colored counter +
+ +At the beginning of our code, you see a new **import statement**. The _rich_ +package defines a _print_ function that we want to use. Inside the _print_, we +simply annotate the desired color in square brackets. Isn't that easy? + +Well ... You might wonder how we know about this package and how to use it. The +answer is a mix of Google, ChatGPT, and documentation. For example, you can find +the documentation of the _rich_ package +[here](https://rich.readthedocs.io/en/stable/introduction.html). After some time +you will know the most popular packages. + +## Finalize your project + +When your project is ready and you want to distribute it to others, you need to +make sure that they install the same packages as you did. This can easily be +done using: + + $ pip freeze > requirements.txt + +If you open the file, you'll find the following content: + + markdown-it-py==3.0.0 + mdurl==0.1.2 + Pygments==2.19.1 + rich==14.0.0 + +At the end, you can spot the _rich_ package including a version tag. This is +important and guarantees that someone else uses the exact same version as you +used for development. But what about the other listed packages? They are +required by the _rich_ package itself. Pip keeps track of all dependencies +automatically and installs required packages for you. You can check a package's +information using: + + $ pip show markdown-it-py + Name: markdown-it-py + Version: 3.0.0 + Summary: Python port of markdown-it. Markdown parsing, done right! + Home-page: https://github.com/executablebooks/markdown-it-py + Author: + Author-email: Chris Sewell + License: + Location: /home/bjoern/projects/hello_world/counter_venv/lib/python3.13/site-packages + Requires: mdurl + Required-by: rich + +You can see that it is required by the _rich_ package and itself requires +_mdurl_. + +Once you have exported your requirements, someone else just needs to install +them using: + + $ pip install -r requirements.txt + +When you want to leave your venv you can deactivate it using: + + $ deactivate + +## Useful Packages + +While the most useful Python packages will depend on your specific project, here +are some widely used and popular packages that are definitely worth exploring: + +- NumPy – Core library for numerical computing and working with arrays. +- Pandas – Powerful data analysis and manipulation toolkit. +- Requests – Elegant and simple HTTP library for making API calls. +- Matplotlib – 2D plotting library for creating charts and graphs. +- Scikit-learn – Machine learning library for classification, regression, + clustering, and more. +- BeautifulSoup – HTML and XML parser used for web scraping. +- Pytest – Popular framework for writing and running tests. +- FastAPI – Modern, fast (high-performance) web framework for building APIs. + +## Summary + +We hope this post helped you understand how to organize Python packages using +virtual environments! Let’s quickly recap what we covered: + +- Python has a huge ecosystem of reusable packages you can install and use in + your projects. +- Virtual environments (venv) help to organize packages for your project +- You learned how to: + - Create and activate a virtual environment + - Use pip to install packages like _rich_ + - Use _pip freeze_ to save dependencies to a requirements.txt file + - Use _pip show_ to inspect package metadata and dependencies +- We also introduced some popular Python packages worth checking out. + +At this point, you are ready to start your own project! You can start by picking +one of the listed packages and playing around with it. The internet is full of +awesome projects, so we are sure you will find an idea for your first own +project! Happy coding, and don't forget to follow us on +[X](https://x.com/bitsandvolts). diff --git a/assets/images/gs_00011_color_output.png b/assets/images/gs_00011_color_output.png new file mode 100644 index 00000000..4cfe4b21 Binary files /dev/null and b/assets/images/gs_00011_color_output.png differ