Python - Peter Büttiker - E-Book

Python E-Book

Peter Büttiker

0,0

Beschreibung

The goal of this Python book is to quickly and easily teach you the most important fundamentals of the Python programming language so that you can quickly implement your own small project ideas. The individual chapters in this book are deliberately kept short to focus on the respective topic and address it directly. This approach should help you internalize the material taught but also understand the basic concepts of each chapter. The book begins with an introduction to the Python programming language, then moves on to the topic of variables, object-oriented programming, and all other essential topics of the Python programming language. This will introduce you to the secrets of Python step by step.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern
Kindle™-E-Readern
(für ausgewählte Pakete)

Seitenzahl: 142

Veröffentlichungsjahr: 2025

Das E-Book (TTS) können Sie hören im Abo „Legimi Premium” in Legimi-Apps auf:

Android
iOS
Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Python

Peter Büttiker

Python

LEARN PYTHON

STEP BY STEP

Programming

Bibliographic information from the German National Library:

The German National Library lists this publication in the German National Bibliography; detailed bibliographic data are available online at http://dnb.dnb.de.

Texts: © Copyright by Peter Büttiker

Cover design: © Copyright by Peter Büttiker

Publisher:

Peter Büttiker

Baumgartenstrasse 6

4900 Langenthal / Switzerland

[email protected]

1st Edition 2025

Production:

epubli - a service of neopubli GmbH, Köpenicker Strasse 154a, 10997 BerlinContact address according to EU Product Safety Regulation:

[email protected]

All rights reserved. No part of this work may be reproduced in any form (print, photocopy, microfilm, or any other process) or processed, duplicated, or distributed using electronic systems without the written permission of the author.

This publication has been prepared and reviewed with great care. Nevertheless, errors cannot be completely ruled out. The author assumes no legal responsibility or liability for incorrect information or its consequences.

Unless explicitly stated elsewhere in the publication, the copyrights to all screenshots and any files offered for download belong to the author.

FOR MY PARENTS

Table of Contents

1 Introduction

1.1 Foreword

1.2 Why Learn Python?

1.3 Book Description

2 Introduction

2.1 What is Python?

2.2 Install Python (macOS)

2.3 Install PyCharm (macOS)

2.4 Create a new project in PyCharm

3 Variables

3.1 What are variables?

3.2 Variables and what you need to consider!

4 Comments

5 Data Types

5.1 What data types are there?

5.2 Integers

5.3 Float

5.4 Strings

5.5 Boolean

5.6 type() Function

5.7 print() Function

5.8 f-String

6 Data structures

6.1 What are data structures?

6.2 Lists

6.3 Sets

6.4 Tuple

6.5 Dictionary

7 Operators

7.1 What are Operators?

7.2 The Four Basic Arithmetic Operations

7.3 Comparison Operators

7.4 Logical Operators

7.5 Modulo Operator

7.6 Exponentiation

8 Functions

8.1 What is a Function?

8.2 Structure of a function

8.3 Function without Parameters

8.4 Function with Parameters

8.5 Function with Return Value

8.6 Function with Default Value

8.7 Function with Unknown Number of Parameters

9 User input

9.1 input() Function

10 Control Structures

10.1 What are Control Structures?

10.2 if Statement

10.3 if-else Statement

10.4 if-elif-else Statement

10.5 for Loop

10.6 while Loop

11 Object-Oriented Programming

11.1 What is Object-Oriented Programming?

11.2 Explanation of the Term ‘Class’ & Co.

11.3 General Structure of a Class

11.4 Creating a Class

11.5 Creating an Instance

11.6 Inheritance

11.7 Class and Instance Variables

11.8 Class Variable

11.9 Instance Variable

11.10 Example Class and Instance Variables

12 Errors and Exceptions

12.1 Dealing with Errors in Python

12.2 ‘try-except’ Block

12.3 ‘try-except-else’ Block

12.4 ‘try-except-finally’ Block

12.5 Define your own exceptions

13 GUI with Tkinter

13.1 Introduction

13.2 What does GUI mean?

13.3 Tkinter - GUI Toolkit for Python

13.4 Creating Windows in Tkinter

13.5 Dimensions

13.6 Colors

13.7 Fonts

13.7.1 Font Objects

13.7.2 Show all ‘font-families’

13.7.3 Use Tuples (Fonts)

13.8 Anchors

13.9 Relief Styles

13.10 Bitmaps

13.11 Cursors

13.12 Geometry Management

13.12.1 pack() Method

13.12.2 grid() Method

13.12.3 place() Method

13.12.4 geometry() Method

13.13 Inbuilt Variable Classes

13.13.1 Variable Classes

13.13.2 StringVar()

13.13.3 IntVar()

13.13.4 DoubleVar()

13.13.5 BooleanVar()

13.14 Button Widgets

13.14.1 Button

13.14.2 Check Button

13.14.3 Radio Button

13.14.4 OptionMenu

13.15 Input Widgets

13.15.1 Entry Widget

13.15.2 Scrollbar Widget

13.15.3 Spinbox Widget

13.15.4 Scale Widget

13.15.5 Text Widget

13.15.6 Combobox Widget

13.16 Display Widgets

13.16.1 Label Widget

13.16.2 Message Widget

13.16.3 MessageBox Widget

13.17 Container Widgets

13.17.1 Frame Widget

13.17.2 LabelFrame Widget

13.17.3 Notebook Widget (Tabs)

13.17.4 PanedWindow Widget

13.18 Listbox Widget

13.19 Menu Widget

About the Author

1 Introduction

1.1 Foreword

First, I would like to thank you, dear readers, for choosing my book.

In a world that is constantly changing and evolving, especially in software development, knowledge and understanding of this subject have become more essential than ever. With this book, I aim to make getting started with Python programming as simple and understandable as possible so that readers can quickly implement their own small projects independently.

The last important point to mention is that if you have Python programming skills, you have very good prospects of finding a job in the IT industry.

I hope you enjoy reading, and happy coding!

1.2 Why Learn Python?

Because Python is a simple and flexible programming language, it makes it much easier for beginners to get started in the world of programming. It also offers a wide range of applications for advanced programmers and programming experts.

It's not without reason that Python is one of the most popular programming languages. Furthermore, it is characterized by its sensible and simple structure and its easy-to-read code. For example, Python dispenses with parentheses and semicolons, which unnecessarily complicate code and can often lead to errors.

Because of Python's wide range of applications, it is used by many large and well-known companies, such as Google, Netflix, Amazon, Spotify, etc. Python is also used in research and science for demanding tasks such as complex calculations, data analysis, and much more. The American space agency NASA also uses Python for its calculations. Furthermore, Python is also used in the field of AI (artificial intelligence).

1.3 Book Description

The goal of this Python book is to teach you the most important basics of the Python programming language quickly and easily so that you can implement your own small project ideas in a short time.

In this book, I use the free Python development tool "PyCharm Community Edition" from JetBrains (www.jetbrains.com). Of course, you can also use a different development tool.

The individual chapters in this book are deliberately kept short to address the respective topic in a focused and straightforward manner. This approach is intended to help readers internalize the material thoroughly, as well as understand the basic concepts of each chapter.

The book begins with an introduction to the Python programming language, then moves on to variables, object-oriented programming, and all other essential topics related to the Python programming language. This will introduce you to the secrets of Python step by step.

The following topics are covered:

Introduction to Python

Variables

Comments

Data types

Data structures

Operators

Functions

User input via the console

Control structures

Object-oriented programming (OOP)

Errors and exceptions

GUI (Graphical User Interface) with Tkinter

2 Introduction

2.1 What is Python?

The Python programming language was developed in the early 1990s by Dutch programmer Guido van Rossum. Although the Python logo features two snakes, the name was derived from the British comedy group 'Monty Python’s Flying Circus,' as Guido van Rossum was an avid fan.

Python is platform-independent and runs on various operating systems such as macOS, Windows, or Linux.

Furthermore, Python can be used for a variety of tasks, such as data analysis, complex calculations, web development, game development, and much more.

Python also requires fewer lines of code than other comparable programming languages.

Well-known companies such as Netflix, Spotify, Google, YouTube, and others use Python for their app development.

2.2 Install Python (macOS)

First, we check whether Python is already installed and which version is installed. To do this, open the Terminal on your Mac and enter the command "python3 - -version" (Figure 1 – Point 1), then press the ENTER key.

If a version of Python is already installed, the output "Python 3.13.1" (Figure 1 – Point 2) will appear in the terminal window. Depending on when Python was installed, the version number may vary.

Figure 1

However, if Python is not yet installed, you will need to install Python.

To do this, we go to the official Python website at https://www.python.org. There, under the "Downloads" menu, we'll find the latest version of Python for download (Figure 2). Now we click the download button, and Python will download immediately.

Figure 2

After downloading Python, we can now install it on our computer. The downloaded Python file can now be opened and installed (Figure 3).

Figure 3

The Python installation program will now start. Now you just need to follow the instructions in the installation program (Figure 4). Once the installation is complete, use the terminal to check whether Python has been installed with the appropriate version number. This completes the Python installation.

Figure 4

2.3 Install PyCharm (macOS)

This book uses the IDE (Integrated Development Environment) called PyCharm from JetBrains. This software can be downloaded from https://www.jetbrains.com. Of course, you can also use any other Python IDE.

After opening the JetBrains website, we see various menus in the navigation bar at the top of the website. We click on the 'Developer Tools' menu, and a pop-up window opens (Figure 5) that lists all available IDEs from JetBrains. Among other things, the PyCharm development environment is located there. We now click on PyCharm or its link, and the PyCharm download page appears.

Figure 5

To get to the PyCharm download page, we now have to click on the download button (Figure 6) again, and another window will open where we will see two versions of PyCharm.

Figure 6

In this window, we see two versions of PyCharm. The first is the paid version, 'PyCharm Professional.' This version is not necessary for learning Python and can therefore be ignored.

Below the paid version of PyCharm, we see the second version of PyCharm called "PyCharm Community Edition" (Figure 7). This version is free and completely sufficient for learning the basics of Python. We download this version and install it on our computer.

Figure 7

2.4 Create a new project in PyCharm

Once you launch PyCharm, a welcome window will appear (Figure 8). There, you have the option to create a new project. At the top of the welcome window, there's a "New Project" button. Clicking this button will take you to the next window.

Figure 8

There you'll find the "Location" input field. In the "Location" input field, you define the storage location (Python_Projects) and the project name (MyProject) (Figure 9). After making these entries, you can click the "Create" button, and PyCharm will create the new project.

Figure 9

The newly created project now appears, and we see the still empty project window (Figure 10). The next step is to create a Python file.

Figure 10

To create a new Python file, right-click on the "MyProject" project folder. A context menu will appear (Figure 10).

There, click the "New" menu and then select "Python File." A new window will open where you can name the new file accordingly.

Figure 11

In this window you can then enter the name for the new Python file (test_1) to be created (Figure 12).

Figure 12

Now we've created the new Python file, and it appears in the project window. This is where the Python code will be entered (Figure 13).

Figure 13

Of course, you can also use another Python development environment to recreate or try out the examples in this book.

3 Variables

3.1 What are variables?

Like every other programming language, Python has variables. Without variables, nothing works in a programming language. Let's imagine that we want to add two numbers and then output the result after the calculation. To perform this addition, we need three variables: one variable for each number to be added and one variable to output the result. If we didn't have variables, we wouldn't be able to perform this task at all. You can think of a variable as a kind of container in which you can store a specific value and then retrieve or change it later. These variables are valid or contain a value until the program has terminated. After that, the values of the variables are lost. To store data permanently, it must be saved, for example, in a database.

3.2 Variables and what you need to consider!

In computer science, the equal sign (=) is called an assignment operator because it assigns a value to the variable (Figure 14).

Figure 14

Another special feature in Python is that you can create variables without a specific data type and change this type when you assign them a new one.

In Python, unlike other programming languages, data types are not explicitly specified, meaning that if, for example, you have created a variable with a value of the data type String, this same variable can be converted or overwritten to another data type without warning (Figure 15).

Figure 15

Although Python is a very flexible programming language, you have to follow some rules when assigning variable names; otherwise, this will lead to corresponding error messages, and you will not be able to work with these variables.

Rules for variable names:

Furthermore, the case sensitivity of variable names must also be taken into account, i.e., the same variable name is written differently and is therefore an independent variable each time and is therefore not overwritten (Figure 16).

Figure 16

4 Comments

Comments are a very useful tool in programming and should definitely be used.

Take, for example, a software project that multiple developers are working on and that grows larger and more complex over time. At some point, everything becomes confusing, and especially when new developers have to familiarize themselves with the existing code, it can be very tedious and time-consuming until they understand what the code actually does.

This is where comments come into play. Well-documented code helps developers quickly read and understand the code. This is also very useful when programming a software project all by themselves, since after six months or more, you may no longer know exactly what the code means. Comments are a very helpful reminder and save unnecessary time wasted trying to figure out what the code actually does.

Comments can generally make code more readable and should be included in programming from the beginning.

If a piece of code or a program component doesn't work correctly, you have the option of commenting out the relevant section or parts of it. This way, the commented-out code is no longer active, allowing you to check step by step for an error, for example, and then remove the comment once you've fixed the error(s).

The hash mark (#) introduces a one-line comment.

In Figure 17 we see an example of a one-line comment.

Figure 17

Figure 18 shows an example where the comment is placed directly after the code statement.

Figure 18

In Figure 19 the code is commented out.

Figure 19

There are situations where you might want to add a longer comment or documentation to a piece of code. To avoid having to type a hash mark (#) before each new comment line, Python offers the option of enclosing a longer comment between three quotation marks (" " ") at the beginning and end of the comment or section of code.

In Figure 20 we see a comment that extends over several lines.

Figure 20

5 Data Types

5.1 What data types are there?

Python has different data types that can be used to work with or store different types of data.

We now want to look at the following data types:

Integers

Float

String

Boolean

5.2 Integers

Integers are so-called “whole numbers,” meaning they have no decimal places. Integers can represent both positive and negative numbers (Figure 21).

Figure 21

5.3 Float

Float is a floating-point number, meaning it can have both integer and decimal places using a decimal point. It can also represent negative and positive numerical values (Figure 22).

It's important to note that in programming, decimal places are always separated by a period, not a comma. If you do use a comma, the compiler will either display an error message or warning, or the calculation will be executed anyway, producing some strange result.

Figure 22

5.4 Strings

Strings are sequences of characters such as individual letters, words, entire sentences, or text. These strings are enclosed in quotation marks. In Python, you can use double quotation marks (“) or single quotation marks (‘). It's important to note, however, that you cannot mix the two types of quotation marks. You can use either double or single quotation marks (Figure 23).

Figure 23

5.5 Boolean

Boolean values can indicate two states: true or false. They are also called truth values (Figure 24).

Figure 24

5.6 type() Function

If you want to find out what type of data it is, you can do so using the type() function. The print() function can then output the desired data type to the console.

Figure 25 shows us some examples of the type() function and the print() function.

Figure 25

Figure 26 shows us the corresponding output in the console.

Figure 26

5.7 print() Function

For example, if we want to output the value of a variable, text, numbers, or other values to the console, we need to use the print()