Skip to main content

Posts

Showing posts from July, 2020

Kivy Python tutorial: Build attractive mobile apps in Python!

In this Kivy Python tutorial, you will learn how to use Kivy for Python app development. By the end, you’ll understand how to start building cross-platform apps for Android, iOS, and Windows using Python. Python is a powerful, flexible, and beginner-friendly programming language that has rapidly grown to become one of the most popular options for developers. But while Python is welcoming for newcomers and flexible enough for pros, getting the most from it will typically require a little help. Also read:  How to make a game in Python: An introduction to Pygame That is to say, there isn’t a whole lot you can build with Python out-of-the-box. If you want to make a game, you’ll need the module Pygame. If you plan on making a website, you’ll need to use one of the popular Python frameworks, such as Flask. But what if you want to make an Android app? In that case, you need Kivy! Kivy is a Python library that supports cross-platform development. That means you can use a single code ba

Windows 10 desktop on the Raspberry Pi 4, and more news from Gary Explains

Your bi-monthly dose of tech industry and developer news, brought to you by the Gary Explains Newsletter   Full Windows 10 Desktop on a Raspberry Pi 4: Thanks to the efforts of projects like “rpi4-uefi” and others, it is now possible to install Windows 10 on the Raspberry Pi. Here is my guide!  ( YouTube ) Deal: 96% off  the Raspberry Pi Mastery Bundle Rust: What is ownership and borrowing? One of the biggest hurdles for those learning Rust is the whole concept of ownership and borrowing. It can be a little tricky to understand, but once you do you will understand why Rust is called a memory-safe language. ( YouTube ) The death of Moore’s law: Fact or fiction? Moore’s law is oft quoted as being dead. But this really is the story of the rise and fall and the rise again of Moore’s law. ( YouTube ) Arm vs x86: Key differences explained: With Apple moving away from Intel and making its own CPUs based on the Arm architecture, it is important to understand the differences in th

How to define a function in Python

Learning how to define a function in Python is one of the most important steps to mastering the language. Functions are blocks of code that perform a specific task and can be “called” from any point in the rest of your program. This allows you to avoid writing out large amounts of code over and over, and it lets you handle dynamic processes that react to the context and user interaction. Let’s take a closer look! How to define a function in Python The good news is that Python makes it very simple to define functions. That’s because Python uses a very nice syntax that looks extremely similar to English. To define a function, we simply use the statement “def.” Can you guess what that’s short for? We then follow that statement with the name of our function (usually capitalizing each word), and then closed brackets. Finally, we use a colon and an indentation. Any code that we want to belong to the function, we then indent. We end with the statement “return” which tells Python to jum

How to write to a file in Python – Txt, Docx, CSV, and more!

Writing to files is one of the most important things you will learn in any new programming language. This allows you to save user data for future reference, to manipulate large data sets, or to build useful tools like word processors and spreadsheets. Let’s find out how to write to a file in Python! How to write to a file in Python – .txt files The simplest way to write to a file in Python, is to create a new text file. This will allow you to store any string to retrieve later. To do this, you first open the file, then add the content you want, and then close the file to finish. myFile = open("NewFile.txt", "w+") myFile.write("Hello World!") myFile.close() In this example, we have opened a new file, written the words “Hello World!” and then closed the file. The “w+” tells Python that we are writing to a new file. If the file already exists, then that file is overwritten. If the file doesn’t already exist, then it will be created. But what if yo

How to reverse a string in Python

Credit: Adam Sinicki / Android Authority As there is no in-built function, if you want to know how to reverse a string in Python, you will need to use one of two McGyver techniques. Fortunately, these are still relatively straightforward and don’t take long at all to learn. Here’s what you need to know. How to reverse a string in Python using slicing The first way to reverse a string, is to use a slice that steps backward. Slices in Python let you return chunks of data from strings, tuples, and lists. This is useful if you ever want to get a few items from a list, or if you want to grab a chunk of a string. Usually, a slice is used in order to provide a range such as “4-7”: stringy = "Hello world" print(stringy[4:10]) This will return “o worl”. Remember that the first value in a string or list has the index “0”. This is true for most programming languages. If we leave a number blank and just add a colon, then Python will refer to the first or last value respectiv

How to open CSV files in Python – store and retrieve large data sets

A CSV file is a “comma-separated values” file. In plain English, this is a text file that contains an unusually large amount of data. More often than not, this is used in order to create databases of information, where each unit of data is separated by a comma. Hence the name! Being able to manipulate, load, and store large amounts of data is a hugely beneficial skill when programming. This is particularly true in Python, seeing as Python is such a popular option for machine learning and data science. Read on then, and we’ll explore how to read CSV files in Python! How to read CSV files in Python by importing modules To get started, we’re first going to create our CSV file. You can do this in Excel by creating a simple spreadsheet and then choosing to save it as a CSV file. I made a little list of exercises, which looks like so: Credit: Adam Sinicki / Android Authority If we open this up as a text file, we see it is stored like this: Type of Exercise,Sets and Reps,Weigh

How to add Python to Path (Windows)

One of the great things about having Python installed on your Windows machine, is that you can access it from anywhere. Simply pull up a command prompt and you can start entering commands, installing new modules via pip, and feeling like a badass hacker! In order for this to work though, you need to know how to add Python to PATH. PATH is an environment variable. This is what tells the command line which folders it needs to look in when searching for the file. If Python isn’t added to PATH, then you would need to point CMD directly to the Python.exe and then run the executable file: C:\PythonX\python.exe But once you know how to add Python to PATH, you can simply type “py” and start running! Likewise, with Python added to PATH, you’ll be able to run Python files as simply as: C:\py PythonApplication.py How to add Python to PATH Before you read any further, it’s first worth checking whether Python is already available. An easy way to do this is to simply type “py” or “python”

When to use lists vs dictionaries in Python

One of the most fundamental and simple skills to learn as a new coder, is how to create a list in Python. But when you also have the option to create dictionaries – which are potentially more powerful – the question becomes why you should need this skill! In this post, we’ll break down how to create a list in Python and when you should choose one over a dictionary. How to create a list in Python and what they are for In programming, a list is a variable that contains lots of other variables. These are added to the list in a sequential order that can then be referenced at any time. In Python, a list can contain multiple data types: strings, integers, booleans, and more. Thus, the types of lists you build in Python will often be similar to the lists you create in real life: lists of names, lists of phone numbers, lists of places… etc. Learning how to create a list in Python is extremely easy. Simply define a variable using square brackets, then separate the elements in the list b

How to use dictionaries in Python

One of the first things any new developer should learn when they start Python, is how to create and store variables. These allow you to store and manipulate data, which is key to creating powerful and dynamic programs. One of the most powerful ways to store data in Python is with dictionaries. In this guide, you will learn how to create dictionaries, how to retrieve information, and how to add to a dictionary in Python! Also read:  How to round in Python Basic commands First, I’ll walk you through the statements you’ll need to create and access dictionaries. You’ll also learn how to add to a dictionary. Python makes all of this very easy! Then we’ll go over what this all means, and why you might want to use a dictionary over another method of storing values. To create a new dictionary, you simply use curly brackets. You then enter your data, with the key and the value separated by a colon, and each entry separated by a comma: newDict={"Jeff" : 7701489772,"Bill&quo

How to run a Python script in the terminal or CMD

Python is an extremely powerful and flexible programming language that is particularly popular among newcomers to coding thanks to its relatively gentle learning curve. That said, the flexibility of Python can also be what makes it complicated to get to grips with in some cases. There is no single set of tools used for Python programming and you won’t be able to build your programs into apps that you can share and sell without relying on external tools. Thus, you may find yourself needing to learn how to run a Python script in terminal – at least to begin with. Also read:  How to install Python and start coding on Windows, Mac, or Linux How to run a Python script in terminal or command prompt It really couldn’t be simpler to run a Python script in the terminal or command prompt! All you need to do is to open up a command prompt or terminal in the right directory (wherever the Python script is stored) and then type: Python hello.py You can also use: Py hello.py This will then o

How to check Python version – 3 methods

Using the latest version of Python is always advisable if you want to benefit from the latest security updates, bug fixes, and features. But how do you know which version you’re on? Read on, and we’ll look at how to check Python version, and install a newer update when necessary. How to check Python version When you download Python, you are downloading an interpreter. This means that you are effectively “teaching” your computer how to understand Python code. All this is to say that Python itself is not a piece of software with a user interface, and thus there is no automatic update involved. But checking the Python version is still easy. That’s because the actual software itself will be named based on the version. The latest version is Python 3.8.4, and if I search for “Python” on my Windows computer, I see that I have Python 3.6 and 3.5 – so I could do with an update! That is how to check Python version! You’ll also be able to find the version of the Python Shell you have instal

How to make a game in Python: An introduction to Pygame

Python is well known as one of the most beginner-friendly and flexible programming languages. But while Python has a fantastic onboarding experience for even the least experienced new programmers, it is actually more confusing to get to grips with in some other ways. Python is so flexible, that it isn’t immediately apparent what you can do with it. You can read a ton of tutorials, for example, and still not understand how to make a game in Python, or how to build a web app. In this post, we’re going to discuss how to make a very simple game in Python using Pygame, the popular selection of modules designed to facilitate simple game creation. What is Pygame? Something that can be tricky for new developers to understand is that programming languages seldom exist in a vacuum. When making an Android app for example, you will not only have to use Java or Kotlin (the two primary programming languages supported by Google) but also the Android SDK . This is the “software development ki