Skip to main content

Posts

How to update Python and how to know if you should

Knowing how to update Python is important if you want to ensure you have access to the latest features, bug-fixes, and security. So how do you keep up with the times? This post will show you how. Also read:  How to round in Python The easy part: how to update Python Updating Python is the easy part. Simply head over to the website and then download the latest version of the interpreter: Python Download Page This will download an installer, and when you run that file, you’ll be taken through the next steps. Seeing as you want to know how to update Python, chances are high you already have an older version installed on your machine. In that case, you will be prompted to update and… that’s it. Easy! If you don’t already have Python installed on your machine, then you should check out our guide for that here: How to install Python and start coding on Windows, Mac, or Linux What version of Python should you use? At the time of writing, the newest version of Python is Python

How to comment in Python – Tips and best practices

Commenting your code is good practice if you want to help other people understand what you’ve written. This makes it crucial to learn how to comment in Python if you work on a big team. However, it’s also very important if you want to understand what you’ve written at a point in the future. Returning to old code can be disorienting, and this is a problem if you hope to offer ongoing support for an app. Also read:  How to use strings in Python In this post, we’re going to look at how to comment in Python, and how to comment in a way that is logical and helpful. How to comment in Python and make it useful The good news is that it is extremely easy to comment in Python. You simply need to prefix whatever you are going to type with a hashtag: #This is a comment! This way, whatever you have written will be ignored by the interpreter and will be highlighted for anyone viewing your code. You can place a Python comment either on its own line, or even in-line with the code you want to

How to create a file in Python and more

Credit: Adam Sinicki / Android Authority Learning how to create a file in Python will open up a huge number of coding possibilities. This is useful if you want to store user-data to provide consistency between uses. However, it’s also extremely useful for manipulating data, for scraping content, and much more. The good news is that, as ever, Python makes this process extremely simple. How to create a file in Python in three lines of code Use the following code to create a file called “BabyFile.txt” and to write “Hello der!” into it: myFile = open("BabyFile.txt", "w+") myFile.write("Hello der!") myFile.close() As you can see, we declare our new variable myFile and then use the built-in open and write commands to open and write to the file. The “w+” tells Python that we will be writing a new file. If the file already exists, then this will write over the file. If you replace this with a “w” then the file will be created only if it doesn’t alre

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss

How to use strings in Python

Python is one of the easiest programming languages to get to grips with, and also one of the most powerful and in-demand. Learning Python is not only fun then, but also a fantastic career move. One of the first concepts you’ll need to familiarize yourself with to that end is how to use strings. In this post, you’ll learn how to create, change, and concatenate strings in Python. Also read:  How to round in Python So… what’s a string? In case you’re scratching your head, a string is a variable that represents a series of alphanumeric characters and symbols. This could be a name, a word, or a whole sentence. Strings are useful when you want to show text on the screen that might change in response to user input. They’re also useful for storing data: for instance names in a database. Python makes it extremely easy to create strings. All you need to do is choose the word that is going to represent your string, and then store the data using the equals sign. So: name = “Adam” Creates

How to print in Python

Credit: Adam Sinicki / Android Authority When it comes to learning to code in a new language, it makes sense to start at the beginning. Learning how to print in Python is most certainly the beginning. The “print” command, as in BASIC, is what you will use in Python to display text on the screen. This is what we use to write “Hello World!” which is the first thing a programmer will traditionally do when working with a new language. In this post, we will learn how to print in Python, and how to do a couple of (slightly) more advanced things, such as showing strings and other variables. Also read:  How to call a function in Python How to print in Python 2 and Python 3 Printing in Python is extremely simple. All you need to do is to write: Print(“Your text here”) However, this is very slightly complicated by the fact that there are two popular versions of Python. That example will work with Python 3 (the currently supported version), whereas you’ll need to drop the brackets i

How to round in Python

Credit: Adam Sinicki / Android Authority Want to know how to round in Python? This can be useful for a wide range of applications, particularly when converting a float into an integer, or when presenting data in a readable manner. Thankfully, this is an easy job: there are actually three separate ways to round in Python. How to round in Python: know your options First, you can use the built-in round() function. This will round values either up or down. math.floor() , meanwhile, will round the value down. Finally, math.ceil() will round the value up to the nearest whole. In order to use the latter two statements, you will need to import the math module with the line: import math . To see how this might work, use the following code: import math number = 7.234 print(round(number)) print(math.floor(number)) print(math.ceil(number)) Now hit run, and you will see the output looks like so: 7 7 8 As you can see, the first two options rounded down to 7, while the final o