Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python_Crash_Course_1738153616

The document is a guide for a Python Crash Course authored by Marcos Gomes Borges, aimed at engineers, researchers, and developers with basic programming knowledge. It covers essential Python concepts, installation instructions, and various coding environments like the Python interpreter, IPython, and JupyterLab. The course emphasizes hands-on practice and offers resources for further learning through Tech Engineer's platform.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python_Crash_Course_1738153616

The document is a guide for a Python Crash Course authored by Marcos Gomes Borges, aimed at engineers, researchers, and developers with basic programming knowledge. It covers essential Python concepts, installation instructions, and various coding environments like the Python interpreter, IPython, and JupyterLab. The course emphasizes hands-on practice and offers resources for further learning through Tech Engineer's platform.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

TECH ENGINEER

Python Crash Course


Breaking Free from the Hamster Wheel

Marcos Gomes Borges


Python Crash Course

Copyright © 2024 Marcos Gomes Borges, TechEngineer.org. All rights reserved.

All material in this book is protected by copyright unless otherwise noted. If you

received this book without purchasing it, please consider supporting future books

and courses by subscribing to our premium plan at TechEngineer.org. We offer a

30-day free trial, so you can explore our courses and join a community of learners.

Every subscription directly contributes to the creation of more resources like this.

Our books and courses are available for educational, business, or promotional use,

and most titles can also be accessed online at TechEngineer.org.

Updated: 18th November 2024

i
ii
About the Author

Marcos Gomes Borges is a Research Engineer with a PhD in Applied Mathematics

from Centrale Lille, France. With 15+ years in software engineering and machine

learning, along with a decade in project management and leadership, he now helps

engineers and researchers master Python, Rust, and C/C++ for engineering and

scientific simulations. Borges has expertise in robotic perception, target tracking,

Kalman filters, and has served as an IEEE reviewer since 2017.

iii
iv
Contents

1 Welcome 1

1.1 Who is this course for? . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 How to take this course? . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.3 About Tech Engineer . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.4 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Getting Started with Python 3

2.1 Python Installation and Considerations . . . . . . . . . . . . . . . . . 4

2.2 Python Installation with uv . . . . . . . . . . . . . . . . . . . . . . . . 4

2.3 Running Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.3.1 The Python Interpreter . . . . . . . . . . . . . . . . . . . . . . 6

2.3.2 The IPython Interpreter . . . . . . . . . . . . . . . . . . . . . . 8

2.3.3 The JupyterLab . . . . . . . . . . . . . . . . . . . . . . . . . . 11

v
vi
1 Welcome

Welcome to the Python Crash Course! Whether you’re a beginner or looking to

sharpen your skills, this course is for you.

To learn a programming language like Python, first, you need to familiarise yourself

with its core concepts and syntax. Once you’re comfortable with the language,

the next step is to gain experience through real-world projects.

In this course, we’ll cover the essentials in just two hours, providing you with a

solid foundation to start coding with confidence. For more advanced topics in

engineering and scientific simulations, additional courses and books are available

at TechEngineer.org.

1.1 Who is this course for?

This course is designed for engineers, researchers, and developers who already

have a basic understanding of programming concepts.

1.2 How to take this course?

We recommend coding along as you progress through this course, as hands-on

practice is the most effective way to understand and retain new concepts. You

can learn by following the e-book or watching the videos, but the key to success

lies in consistent practice and repetition—there’s no shortcut for that!

TechEngineer.org 1
1.3 About Tech Engineer

Tech Engineer offers practical, hands-on training in Python and Rust, specialising

in engineering and scientific simulations. We provide individual courses as well as

customised corporate training solutions, available online and on-site to address

the specific needs of your team.

Visit our website: techengineer.org

Find us on LinkedIn: linkedin.com/company/techengineer

Watch us on YouTube: youtube.com/@monsieurborges

1.4 Getting help

Join the Tech Engineer Community for support—it’s easy and free!

For full access to expert support and premium resources to accelerate your learn-

ing, consider our paid subscription.

2 TechEngineer.org
2 Getting Started with Python

Python, created by Guido van Rossum in 1991, is a widely used programming lan-

guage praised for its simplicity, readability, and versatility. Its clean syntax makes

it easier to read and write code compared to languages like C, C++, or Rust.

Python is powerful for a wide range of applications. It includes extensive libraries

that make it ideal for projects in data analysis, web development, and machine

learning.

Popular platforms like YouTube, Instagram, and Dropbox have been developed

using Python, showcasing its ability to handle complex, large-scale systems. Its

versatility and ease of use also make it a preferred choice for scientific computing,

automation, and artificial intelligence projects.

Figure 2.1: Guido van Rossum, creator of Python.

Photo credit: Michael Cavotta.

TechEngineer.org 3
2.1 Python Installation and Considerations

To start your learning journey, install Python on macOS, Linux, or Windows.

There are many ways to install Python, but we recommend using the uv package

manager for the best experience. Other options like conda, pyenv, or the official

installers from Python.org are reliable and effective but may become limiting as

your Python expertise grows.

For now, don’t worry about the details! Relax and focus on learning as much as

you can about Python. Remember, everything is reversible and can be changed.

If you prefer using conda or pyenv, feel free to experiment—that’s perfectly fine.

Just know that uv will help you with tasks like:

• Installing specific Python versions

• Creating and managing virtual environments

• Installing and managing Python packages and tools

• Replacing several tools like conda, pyenv, pip, poetry, virtualenv, and more

2.2 Python Installation with uv

The steps for installing uv and running Python code can seem intimidating if you’re

not familiar with using the terminal. Don’t worry! To navigate this process without

stress, we recommend that you always read the messages that appear after you

run any commands.

There’s often useful information or instructions provided—simply follow them or

do a quick online search, and you may be surprised at how quickly you can find

solutions to common problems.

4 TechEngineer.org
1 Installing uv

To install uv, open the Terminal or PowerShell and paste the following code:

macOS and Linux

curl -LsSf https://astral.sh/uv/install.sh | sh

Windows

powershell -ExecutionPolicy ByPass -c `


"irm https://astral.sh/uv/install.ps1 | iex"

� Important
Follow the PowerShell instructions to validate the installation.

You may need to restart the system.

� Note

For more details and assistance with installing uv, please refer to the

documentation available at docs.astral.sh/uv.

2 Installing Python

Once we’ve installed uv, we’re ready to install Python to begin our journey!

Enter the following code in the Terminal or PowerShell:

uv python install 3.12

Installation complete! The adventure begins now!

TechEngineer.org 5
2.3 Running Python Code

Python provides several tools and environments for writing and executing code,

each suited to different needs.

We’ll explore three methods for running Python code: the Python interpreter for

immediate feedback, IPython for interactive coding experience, and JupyterLab

for developing comprehensive, interactive projects.

Future courses will include advanced configurations using tools such as VS Code

and virtual environments with uv . For more details, visit TechEngineer.org.

2.3.1 The Python Interpreter

The Python interpreter is the simplest way to execute Python code directly. It

allows you to run commands line by line and is ideal for quick testing and simple

scripts. The standard Python interpreter is included with Python installations and

can be started from your command line.

1 Launching Python

To start Python, enter the following command in the Terminal or PowerShell:

uv run python

You’ll see something like this:

uv run python

Python 3.12.7 (main, Oct 16 2024, 07:12:08)


Type "help", "credits" or "license" for more information.
>>>

6 TechEngineer.org
2 Hello, world!

Tradition suggests that the first program in a new language should print the

words ”Hello, world!” on the screen.

In Python, this can be done in a single line:

print("Hello, world!")

Type it into the Python interpreter, and you should see the following output:

uv run python

Python 3.12.7 (main, Oct 16 2024, 07:12:08)


Type "help", "credits" or "license" for more information.
>>> print("Hello, world!")
Hello, world!
>>>

Try calculating the sum of the first 5 natural numbers:

1 + 2 + 3 + 4 + 5

You should see the following output:

uv run python

Python 3.12.7 (main, Oct 16 2024, 07:12:08)


Type "help", "credits" or "license" for more information.
>>> print("Hello, world!")
Hello, world!
>>> 1 + 2 + 3 + 4 + 5
15

TechEngineer.org 7
2.3.2 The IPython Interpreter

IPython is a powerful interactive Python shell that provides a rich environment for

data exploration, experimentation, and visualisation. It offers features like code

completion, syntax highlighting, and the ability to execute code line by line or cell

by cell.

When learning Python, IPython can be invaluable for testing code snippets, visu-

alising data, and understanding the immediate impact of changes. It’s a great tool

for interactive learning and experimentation.

1 Installing IPython

To install IPython using uv (see the previous section), enter the following

command in the Terminal or PowerShell:

uv tool install ipython --with pip

To complete the setup, update your shell PATH with this command:

uv tool update-shell

� Important
You might need to restart your Terminal for the changes to work.

Check the uv message for exact steps.

2 Launching IPython

To start IPython, use the following command:

ipython

8 TechEngineer.org
You’ll see something like this:

ipython

Python 3.12.7 (main, Oct 16 2024, 07:12:08)


IPython 8.29.0 -- An enhanced Interactive Python.
In [1]:

3 Exploring Tab Completion

Remember the classic ”Hello, world!”? It’s a great start, but let’s dive into

something more practical: IPython’s tab completion.

IPython’s tab completion helps you gain efficiency by letting you complete

code with just a tap of the Tab key. This simple action reduces typos and

saves time.

For instance, if you want to type print, start by entering pr in the IPython

interpreter and press Tab . Instantly, a list of options starting with pr will

appear:

In [1]: pr
print() property %precision %prun

Select print() and complete the ”Hello, world!” example:

In [1]: print("Hello, world!")


Hello, world!

In [2]:

TechEngineer.org 9
4 Discovering Object Attribute Completion

Imagine you’re building a Lego castle. Each Lego brick is an object, and come

in different shapes and colors—these are like the attributes of an object.

Attributes describe what an object is, such as its color, size, or shape.

To build your castle, you also need to know what each Lego brick can do.

For example, some bricks can snap together, while others might have special

hinges. In Python, these actions are called methods. Methods tell you what

an object can do, such as adding, removing, or changing things.

IPython’s object attribute completion is a handy tool that helps you easily

discover the attributes and methods an object has.

For example, if you have a list of Lego pieces:

In [2]: legos = ['brick', 'hinge', 'plate']

In [3]:

When you type legos. and press the Tab , IPython will show a list of attrib-

utes and methods available for the legos list:

In [3]: legos.
append() count() insert() reverse()

This way, you can quickly see what you can do with the legos list without

having to remember everything.

Why is it useful?

• Saves time: No more Googling for specific methods or attributes

• Reduces errors: Autocompletion minimizes typos and mistakes

• Enhances learning: Explore the capabilities of objects in a fast manner

10 TechEngineer.org
2.3.3 The JupyterLab

JupyterLab is a web-based application for creating interactive documents that

combine code, text, equations, and rich visuals. It provides Python learners with

an intuitive environment for coding and experimentation.

1 Installing JupyterLab

To install JupyterLab, enter the following command in the terminal:

uv tool install jupyterlab --with altair

This will install JupyterLab with the Altair extension that allows us to create

interactive visualisations.

� Note

Installing JupyterLab this way is great for learning and experimenting.

For serious projects, consider setting up a virtual environment.

2 Launching JupyterLab

Launch JupyterLab with:

jupyter-lab

This will launch the JupyterLab server and automatically open it in your web

browser, as shown in the image below.

TechEngineer.org 11
Menu bar Main work area

Access to commands and settings Workspace for notebooks

Left sidebar

Quick access to tools and file management

3 Exploring JupyterLab

In our previous tutorial, we covered the fundamentals of IPython including

tab completion and object attribute completion. Now, let’s explore similar

features with JupyterLab.

In the menu bar, go to File , click New , and select Notebook . Choose

Python 3 as the kernel. A window similar to this will appear:

Notebook cell

A container for code or text

12 TechEngineer.org
JupyterLab tab completion works like IPython. Start typing pr in a notebook

cell and press Tab to see options:

Select print and complete the ”Hello, world!” example.

Press Shift + Enter to run the code:

4 Ploting a sine wave

Here’s how to create a sine wave plot using Altair, a library for interactive

visualisations. Our main focus is to present JupyterLab’s functionality, so

don’t worry about fully understanding the code just yet.

This code snippet will:

1. Generate a sequence: Numbers from 0 to 10 with a step size of 0.1 for

the x-axis

2. Calculate sine values: Compute the sine of each x-value for the y-axis

3. Create the plot: Display the data as a line chart to visualize the sine

wave

TechEngineer.org 13
Paste this code into a JupyterLab cell and press Shift + Enter to run it:

import altair as alt

data = alt.sequence(0, 10, 0.1, as_='x')

alt.Chart(data).transform_calculate(
y='sin(datum.x)'
).mark_line().encode(
x='x:Q',
y='y:Q',
)

Well done! You have taken your first steps in exploring how to run Python code. In

the next module, we’ll explore the features and syntax that make Python a versatile

and powerful programming language.

14 TechEngineer.org

You might also like