Home » Programming » Python

How to Add/Append Items to a Dictionary in Python [Examples]

Add Item to Python Dictionary

This short tutorial will show you how to add single or multiple items (as key:value pairs) to a dictionary in the Python programming language. What is a Dictionary in Python? We’ve covered what dictionaries are in our article here. There are a couple of ways to add items to a dictionary, so we’ve put together this article separately to make sure we’ve got everything covered! Checking Whether a Key Exists in a Dictionary As outlined in the article linked above, dictionaries store data as a series of key:value pairs. Before … Read more

Home » Programming » Python

How to Merge Dictionaries in Python, With Examples

Python Merge Dictionaries

This article will show you the simplest ways to merge two or more dictionaries in the Python programming language. Note that the code examples in this article are written for Python version 3 – and will not work with Python version 2! What is a Dictionary in Python? We’ve covered what dictionaries are in our article here. As there are a couple of ways to merge multiple dictionaries in Python, we’ve put together this article separately to make sure we’ve got everything covered! Merging/Joining Using Dictionary Union … Read more

Home » Programming » Python

The Python ord() Function – What it Does and How to Use It

Python ord()

The ord() function returns an integer representation of a Unicode character. Here’s why it does that and how it’s used. Python ord() Function Syntax First, here’s the syntax for the ord() function: ord(CHAR) Note that: CHAR is the single Unicode character you wish to receive the corresponding integer representation of Only a single character is allowed! OK, But Why? The integer returned by ord() is the Unicode code point of the character. It’s a unique number that identifies the character. Unicode represents characters from several languages and includes symbols, emojis, and combinations of all of them. When data is saved … Read more

Home » Programming » Python

How to Slice a Dictionary in Python, With Examples

Python Dictionary slice()

We’ve covered how to use the slice function to slice strings, tuples, lists, and other sequences. This tutorial will show you how to slice dictionaries, with examples. Dictionaries Not sure what a dictionary is in the Python programming language? Get up to speed with our article here. The slice() Function and the islice() Function Check out the full article here on how the slice() function works – we’ve covered it in-depth. Unfortunately, the slice() function built-in to Python does not work with dictionaries. If you do try to use slice() with a dictionary type variable, you will receive the following error: TypeError: … Read more

Home » Programming » Python

Multi-Line Comment Blocks in Python – HowTo, With Examples

Python multi-line comment block

This short article will show you how to use comments in Python, including multi-line comments. What is a Comment? A comment in programing is one or more lines of text that aren’t interpreted as programming instructions. Instead, the computer skips them when executing your code, completely ignoring them. Therefore, comments don’t have to conform to any valid syntax. They’re just text that you read, and the computer doesn’t. Single-Line Comments in Python Comments in Python are any line that starts with a # (hash) character. # This … Read more

Home » Programming » Python

Scopes & Global Variables in Python – Explained

Python Scopes & Global Variables

This article will explain and demonstrate scopes in Python – including the global scope and how to declare global variables. What is a Scope? As you move towards building more complex Python applications, you’ll be using functions, loops, try/except statements for error handling, and other more advanced constructs. Which variables are available within each of these constructs is defined by the variable’s scope. The scope is where the variable is available within your program. Different variables have different scopes, and totally distinct/separate variables in different scopes can share … Read more

Home » Programming » Python

Catch Errors/Exceptions in Python with try/except [Examples]

Python try/except to Catch Errors

This article will show you how to use the try/except/finally statements. What are Errors/Exceptions A computer program will produce an error, also known as an exception, when something goes wrong. This could be due to mistyped programming commands (syntax errors) or because the application encountered something unexpected – be it bad user input or a logical condition that can’t be met. When something goes wrong, an exception/error will be thrown – which will usually result in the application stopping and the error being displayed so that it can be found and fixed. … Read more

Home » Programming » Python

How to Write to a Text File in Python

Python Write File

This article will show you how to write to a file in the Python programming Language – examples included! All files are, essentially, text files. Data is formatted and stored in various ways – comma-separated values for spreadsheets (CSV), plain text for text files (TXT), all the way to complex image formats for storing documents and images like PDF and JPG files. Once the program you are building is up and running, you’ll want to be able to save the output it produces – so … Read more

Home » Programming » Python

Python 2 vs Python 3 – Which Should I Be Using?

Python 2 vs Python 3

There are still many tutorials and resources online that are written for Python 2 rather than the newer Python 3 – So, which should you be using? The short answer is below: Python 3. Why You Should Use Python 3 Python 3 is better in every way. It is a major revision that was released almost a decade after Python 2. The smart people who build programming languages probably learned a lot of lessons in that time. The syntax in Python 3 is more consistent … Read more

Home » Programming » Python

10 Fun Python Projects for Beginners – Kids and Adults

Python Projects For Beginners

Here are 10 projects to get you started learning the Python programming language. Not a newbie? Check them out anyway for some Saturday afternoon project ideas. If you’re trying to learn something new, making it fun can be a big help. Being engaged in a project means you remember why you did something the way you did and helps to make you more confident when using the same tools again. Bonus if you get creative and start adding your own touches – it’s the best way to … Read more