/* jquery */ /* jquery accordion style*/ /* jquery init */
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Python and Artificial Intelligence

Python is one of the best programming languages for AI development.

Here are some of the reasons why:

• Python has a clear and straightforward syntax. This ensures the code is both easy to write and easy to read. Consequently beginners, researchers, data scientists and engineers can quickly create AI programs.

• The Python language runs on just about any platform including MacOS, Windows, Linux and the complete Raspberry Pi range.

• There are a huge number of Python libraries and resources tailored specifically for AI and machine learning, making it ideal for both prototyping and full-scale development. These libraries include the mathematical NumPy and SciPy, Pandas for data manipulation and analysis, scikit-learn for traditional machine learning, TensorFlow (originally from Google) and PyTorch (originally from Meta AI) for deep learning, Keras for high-level neural networks, plus a vast collection tools and frameworks.

• Widespread adoption of Python for AI and machine learning in startups, tech industry companies and academia drives an ever growing stream of research papers, blue sky projects, state-of-the-art techniques, tools, open-source code and models.

Try My Free Python Coding Tutorials

Future of Python

Python Coding

Follow the links below to hear Guido van Rossum - the creator of Python programming language - talk to Lex Fridman about the future of the language.

• Lex Fridman Podcast
• #341 on Apple Podcasts
• #341 on Spotify

Topics discussed include...

• CPython
• Code readability
• Indentation
• Bugs
• Python 3.11 Speed
• Type hinting with mypy (and other similar tools)
• Best IDE for Python
• Python 4.0
• Machine learning
• Beginner advice
• GitHub Copilot
• The future of Python

Learn Coding with my Free Python Coding Tutorials

Wireframe Python Game Coding Series

Wireframe Magazine Issues

Published by Raspberry Pi Press the Wireframe magazine is aimed at the gaming enthusiast with news, reviews, insights and coding tutorials.

There's a free to download digital edition and a paper version available from high street outlets such as WH Smiths and a number of supermarkets.

Below I provide an issue-by-issue list of the Toolbox section's Source Code articles on game simulation coding with their downloadable Python source code - typically using the Pygame or Pygame Zero modules.

• Issue 47 - Recreate the classic Tron Light Cycle arcade game in Python.
• Issue 46 - Code a replica of the Pipe Mania Puzzzle Game and begin connecting pipes.
• Issue 45 - Recreate a Tiger-Heli Game simulation with its bomb dropping helicopter.
• Issue 44 - Code an Artillary-style Tank Game similar to Worms Game Series.
• Issue 43 - Race around a Namco Rally-X game road maze simuation.
• Issue 42 - Skip around a 3D cube pyramid with classic Q*bert game simuation.
• Issue 41 - Fly through the clouds in Konami’s Time Pilot free-srolling game simuation.
• Issue 40 - Recreate JetPac gameplay to build and fuel a rocket under hostile alien fire.
• Issue 39 - Atari's Gauntlet Game four character dungeon exploration situation.
• Issue 38 - Robotron: 2084 Game twin-joystick simultaneous move and shoot simulation.
• Issue 37 - Atari's Lunar Lander Game gravity, rocket thrust and orientation simulation.
• Issue 36 - Side Pocket Pool Game with ball aiming, movement and collision simulation.
• Issue 35 - Konami’s Hyper Sports Clay Pigeon Shooting single player game simulation.
• Issue 34 - Atari Marble Madness game course topology and rolling-physics simulation.
• Issue 33 - Sega Zaxxon arcade game simulation of its classic axonometric projection.
• Issue 32 - Kung-Fu Master arcade game character action movement.
• Issue 31 - Spy Hunter arcade game vertical scrolling road Car Driving simulation.
• Issue 30 - Recreate the Boulder Dash game with Caves of Gems and Falling Boulders.
• Issue 29 - Recreate Dong Nguyen's Flappy Bird game flight and Moving Pipes.
• Issue 28 - Turn-based combat coding a la Pokemon's Sword and Shield game.
• Issue 27 - Konami Frogger arcade game animation via Pygame Zero Actors
• Issue 26 - Phoenix arcade game Mothership recreation with armoured shields
• Issue 25 - Tile-matching game code inspired by Tetris and Candy Crush
• Issue 24 - Nintendo's Donkey Kong game tribute with rolling barrels
• Issue 23 - Daley Thompson Decathalon game inspired sprinting simulation
• Issue 22 - Konami's Scramble arcade game randomly generated scrolling landscape
• Issue 21 - Atari Super Sprint racing game overhead-view track navigation
• Issue 20 - Duck Hunt 2D shooting gallery game recreation using the mouse pointer
• Issue 19 - How to code game states and rules for any game
• Issue 18 - Sinclair ZX81 3D Monster Maze game navigable labyrinth recreation
• Issue 17 - Lemmings game automatic path-following behaviour coding
• Issue 16 - Gradius game ship-following drone-like Option sprite code
• Issue 15 - Ant Attack game map drawn using block images
• Issue 14 - Nintendo Donkey Kong game animated walk simulation
• Issue 13 - Gyruss game star field zooming effect
• Issue 12 - Bomberman game exploding bomb effects
• Issue 11 - Breakout playable game
• Issue 10 - Pang game ballon spawning simulation
• Issue  9 - Space Invaders playable game
• Issue  8 - BBC Micro and machine code
• Issue  7 - Super Mario game recreation of jumping physics
• Issue  6 - How to create as simple text adventure game
• Issue  5 - How to code a retro game type high-score tables
• Issue  4 - Atari Asteroids game thruster motion simulation
• Issue  3 - Moon Patrol game platform game 3-layer parallax scrolling simulation
• Issue  2 - Galaxian arcade game AI dive bombing aliens using 'ship' sprites
• Issue  1 - Defender arcade game particle explosion simulation

Try My Free Python Coding Tutorials

LEGO® and micro:bit with Brown Dog Projects

I recently came across the Brown Dog Projects website and its selection of simple-to-build, easy-to-code LEGO® themed projects using low-price microprocessor boards like the popular micro:bit.

You can also buy a number of kits, including a micro:bit board kit.
This kit has a Crazy Circuits Bit Board to simplify LEGO® brick and micro:bit board connectivity, plus many other items including a USB Cable, Battery Holder, Small Pushbuttons, LEDs, Potentiometer, Piezo Speaker, LEGO® Compatible Servo and a few LEGO® pieces.

The video below captures some of the potential of this kit.

The Brown Dog Projects website also has a collection of step-by-step project guides

Read More LEGO® Posts

Anton's LEGO® Construction Projects

The web contains many sources of information on LEGO® programmable hub/brick technologies and the associated construction kits. One of these is from a guy called Anton.

Anton has a blog containing How-To tutorials which explain the steps required construct and program LEGO® robot projects, like the ones show in his videos.

For example, Anton has a Bluetooth wireless remote control of the EV3 hub/brick post which involves LEGO® SPIKE Prime, the MicroPython language and the PyBricks module.

Anton also has a large collection of inspirational LEGO® project videos going back over 10 years and covering various products including LEGO® NXT, LEGO® Technic, LEGO® Mindstorms and LEGO® SPIKE.

LEGO® and Python and PyBricks

Over the 20 years LEGO® have created a number of intelligent programmable hub/brick technologies for LEGO® MINDSTORMS, LEGO® TECHNIC, LEGO® CITY, LEGO® BOOST and most recently LEGO® SPIKE.

However, the ability to use the Python programming language for coding LEGO® hub/bricks is a more recent innovation. One of the Python coding options is a module called PyBricks.

PyBricks has a number of advantages:
• It is Open Source with source code on GitHub
• It is based on the popular MicroPython language
• You can run your code directly on the hub/brick
• There's an app to write MicroPython scripts
• You can send scipts to the hub/brick via Bluetooth
• It offers precise motor control
• It is compatible with all official sensors and motors

PyBricks version 2.0 was a little limited in hub/brick support. But now, with PyBricks 3.0 Beta, there's support many more LEGO® platforms, including LEGO® BOOST, LEGO® TECHNIC Control+ and LEGO® City Trains.

LEGO® SPIKE™ and MicroPython

LEGO® SPIKE™ is an education-level product from the LEGO® intelligent programmable hub/brick family.

LEGO® SPIKE™ simplifies the connection of motors and sensors - such as touch, distance-ultrasonic, and color/light - and provides a programming interface via the popular Scratch coding language.

LEGO® SPIKE™ brick/hub has six ports, a speaker, a 5x5 light matrix, a 6-axis gyroscope, Bluetooth wireless communication and a rechargeable battery.
The LEGO® SPIKE™ Prime kit adds motors, sensors and 525 technic-style bricks to create a rapid robotics development environment.

Interestingly, internally this intelligent brick/hub runs MicroPython. And, you can open a connection to a REPL session and enter commands to control the SPIKE™ brick/hub - as shown on the instructables website.

And now, with version the Education SPIKE™ 1.3 app update, LEGO® officially provide full language support for the Python Knowledge Base coding, as shown by the message below taken from a LEGO® SPIKE™ webpage.

LEGO® Education SPIKE™ Prime lets your students dip their toes into text-based coding with a beginner-friendly Python experience. Python is a coding language widely used in robotics and it lays a strong foundation for tackling more advanced topics like algorithms, data structures, machine learning, and artificial intelligence.

Python Math - SageMath

Python Coding

SageMath is a free open-source mathematics software system licensed under the GPL. Released in January 2020, SageMath 9.0 now runs on top of Python 3.0 programming langauge.

SageMath builds on top of many existing open-source packages, such as NumPy, SciPy, matplotlib, Sympy and many others. This powerful functionality is accessed through either a Python-based language or more directly using interfaces and wrappers.

By combining a math-like syntax with Python's coding capabilities SageMath can handle a large and diverse range of mathematical problems, including:

• Functions
• Basic Algebra
• Linear Algebra
• Calculus
• Polynomials
• Finite Groups
• Number Theory
• Plotting

More Python Math Coding

Python Math - Function Plotting

Python Coding

Python benefits from an impressive set of graphing packages. However, many of the most popular ones tend to be focussed on the data visualisation side of things rather than mathematics.

When it comes to plotting math functions consider an exploraion of the functionality provided by the matplotlib package. This mature package works runs on a wide range of operating systems and has some math-centric advantages.

For instance, matplotlib includes the pyplot interface, namely matplotlib.pyplot, which is a collection of functions that make matplotlib work like MATLAB.

It also plays nicely with other math packages, including NumPy. The code below uses both matplotlib.pyplot and numpy to plot sine and cos curves:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

Adding graph axis labels and a title is also sraightforward with pyplot, as shown below:

plt.xlabel('x values')
plt.ylabel('sin(x) and cos(x)')
plt.title('Plot of sin and cos')
plt.legend(['sin(x)', 'cos(x)'])
plt.show()

Importantly matplotlib has extensive collection of tutorial documentation pages covering the pyplot interface in addition to many other math-aligned applications, all with fully explained code examples.

In addition to the official documention a quick search of the web will unveil a plethora of matplotlib plotting code examples and links to diverse selection of real-world applications.

More Python Math Coding

Python Math - Complex Numbers

Python Coding

Imaginary Numbers and Complex Numbers are vital to solving many mathematical problems - including the study of waves, electric motor efficency and fractals.

Python uses the j symbol to represent the imaginary part of a Complex Numbers.

An example of how this is done is shown in the Python Interactive Mode session below...

>>> a = 4 + 3j
>>> print(a)
(4+3j)

Once created a Complex Number can be treated much like any other number, as below...

>>> a = 1 + 2j
>>> b = 2 + 4j
>>> print('Addition =', a + b)
Addition = (3+6j)
>>> print('Subtraction =', a - b)
Subtraction = (-1-2j)
>>> print('Multiplication =', a * b)
Multiplication = (-6+8j)
>>> print('Division =', a / b)
Division = (2+0j)

And you can extract a Complex Number's real and imaginary components as below...

>>> a = 1 + 2j
>>> b = 3 + 5j
>>> c = a + b
>>> c.real
4.0
>>> c.imag
7.0

More Python Math Coding

Python Math - math module

Python Coding

The Python language math module has a good collection of functionallity. Here are a few highlights to help with solving common math problems.

Constants

There are a number of useful constants in the math module:

#Import math module
import math

math.e
math.pi
math.tau

Factorials

There is a useful factorial() function:

#Import math module
import math

num = input("Enter a number: ")
fac = math.factorial(int(num));
print("Factorial of ", num, " is ", fac)

GCD (Greatest Common Divisor)

This module also has a gcd() function:

#Import math module
import math

print(math.gcd(54, 24))

And there's far more to explore including Logarithmic Functions, Trigonometric Functions, Angular Conversion and Hyperbolic Functions.

For a full list of functionality see the Python3 math module documenation page.

More Python Math Coding

Game of Life in Python Turtle Graphics

In the September 2020 issue of the Linux Format magazine (LXF) you'll find a Calvin Robinson article on how to code the classic Game of Life simulation, as invented by mathematician John Horton Conway.

The source code examples for this article are in the Python language and use its Turtle Graphics capabilties.

Creating a Turtle Graphics program is fun. But if you'd prefer a more efficient Game of Life then consider recoding the simulation to use the PyGame or PyGame Zero modules - as described in the YouTube coding tutorial video below.

Back issues of the Linux Format magazine are available from the LXF Archive webpage.
And you can download article source code listings from the LXF website.

Try My Free Python Coding Tutorials

Self-Replicating Python Code - Quines and Introns

Python Coding

Lex Fridman is well known for his in-depth AI-focussed interviews with some of the key people in artificial intelligence machine learning, software development and technology in general.

In a recent YouTube video post Lex explains how to write self-replicating Python code in the form of Quines and Introns.

Try My Free Python Coding Tutorials

Linux Format Python Game Coding

Interested in how to code Classic Games in the Python language?

Then check out Calvin Robinson's series of articles in the Linux Format magazine (LXF).

The series so far includes:
• Issue 262: a Lunar Lander game
• Issue 263: a Platform-style game
• Issue 264: a 2D Top Down Perspective Shooter game
• Issue 265: a Space Invader game

Back issues of the Linux Format magazine are available from the LXF Archive webpage and you can also download source code listings for all coding articles.

Try My Free Python Coding Tutorials

8GB Raspberry Pi Model 4B and RPI OS

Raspberry Pi

After almost 3 million sales the Raspberry Pi 4B now comes in an 8GB version, which is available to purchase for $75.

It was already known that the RPi 4B's BCM2711 chip could address up to 16GB of LPDDR4 SDRAM. But only now does a new Micron-designed component enable the 8GB version to be manufactured.

To complement the 8GB RPI 4B there's a new beta-level 64-bit ARM version of the Raspberry Pi operating system to utilise all those extra gigabytes.

New OS features include:

• Bookshelf app to access Raspberry Pi Press magazines, including The MagPi, HackSpace and Wireframe (found in the Help menu). Any magazine PDF downloads are saved in the “Bookshelf” sub-directory of your home directory.

• Magnifier app to improve is accessibility to the Desktop for people with visual impairments. Once installed a magnifying glass icon appears at the right-hand end of the taskbar. To use click this icon (or use the Ctrl-Alt-M shortcut) and then play with the zoom and static-window/follow-cursor options.

• User research allows anyone to send back feedback how you use your Raspberry Pi. It is in the form of a questionnaire, which appears the first time the Chromium browser is launched. There are just four questions and the associated unique id string doesn't compromise your animininity.

Try My Free Python Coding Tutorials

More LEGO® Boost Python Coding

As my original LEGO® Boost kit and Python coding post has received quite a number of page views I thought I'd post some additional information on this topic.

Andrey Pokhilko

I recently came across Andrey Pokhilko and his YouTube video (below). In this video he talks about his own LEGO® Boost Robotics construction and coding project, along with a demonstration of Vernie's capabilities plus a custom power pack hack.

To view Andrey's Python code examples navigate over to his GitHub resources page.

Mikhail Zakharov

I also noticed Mikhail Zakharov has added Python coded Voice Command Control to his own LEGO® Boost Vernie project.

Mikhail's Python source code can be found on his GitLib resources page.

Try My Free Python Coding Tutorials

PyCon 2020 - Online Content

With the current global lockdown events like PyCon US 2020 have been cancelled.

However, you can still access plenty of Python technology news and information for free by subscribing to PyCon online./p>

Options include receiving email notifications and watching the PyCon 2020 YouTube Channel.

Content includes:
• Recorded talks and tutorials
• Online Summit and Hatchery programs
• Poster presenters sharing their creations
• Startup Row company presentations
• Sponsor workshop videos and job postings

Try My Free Python Coding Tutorials

Python Coding on Android with QPython

Would you like to write Python code on your android smart phone or tablet?

Well you can by using the free Google Play downloadable app QPython. In fact there are two variants of QPython. The basic one supports the older but better supported version 2.7, while QPython3 supports the newer version 3 of the language.

With QPython you can do mathematical calculations, perform decisions and branching, define loops, create lists, write functions and even develop your own Python modules.

QPython has both an interactive console mode for instant coding results, plus a built-in editor to create ‘.py’ files. These ‘.py’ files can be executed directly on the Android smartphone or tablet, or transferred to another device, such as the Raspberry Pi or anyWindows or Mac or Linux computer.

Try My Free Python Coding Tutorials

CircuitPython, Adafruit and MicroPython

CircuitPython is a variant of the python language specifically aimed at the popular range of maker-style board products from AdaFruit Industries.

Digging under the covers the CircuitPython language is actually a fork of the popular MicroPython language. Consequently when there's a MicroPython update the CircuitPython language is also updated to accommodate any new features and functionality.

However, MicroPython is designed to support a much wider range of boards from many different manufacturers. As a result coding in CircuitPython is going to be a little different in places, so using a MicroPython reference manual isn’t always going to work.

Also note that CircuitPython only has a single file module download to the target board. While his means it isn't as flexible as MicroPython, it does make it considerably simpler to implement and debug.

Free micro:bit MicroPython Coding Tutorials

LEGO® Boost and Python Coding

The LEGO® Boost kit is all about delivering a simplified route into robotics.

You can create models using standard LEGO® bricks. And there's an easy-to-get-started block-based coding app for iOS and Android.

The kit's Move Hub accepts a real-time stream of code instructions, sent via bluetooth, to interact with the built-in motors, LEDs and sensors.

However, it's also possible to interact with the Move Hub using Python, a far more flexible alternative block-based coding.

First you'll need a Python installation on your PC, Mac or Raspberry Pi 3B/3B+/3A+ or Zero W.

You'll also have to install some dependencies:

sudo apt-get install python3-pip
sudo pip3 install pexpect
sudo pip3 install pygatt

Then install the LEGO® Boost Python library module:

wget https://github.com/undera/pylgbst/archive/master.zip
unzip master.zip
cd pylgbst-master
sudo python3 setup.py install

To find out more on using Python with LEGO® Boost (and get to access to plenty of code samples) check out the series of articles by Mike Cook in the official Raspberry Pi magazine, starting at issue 80.

And read my More LEGO® Boost Python Coding post for additional inspiration.

My Free Raspberry Pi Python Coding Tutorials