Python ord() Function (Code Examples)

What does the ord() function do? The built-in ord() function in Python converts any Unicode character into a number. It only takes one parameter which must be a single Unicode character, any more than one character produces an error. An example of the type of error you will get when sending more than 1 Unicode character is demonstrated here: 1 2 3 4 5 6 >>> ord('A') 65 >>> ord('ABC') Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: ord() expected a character, but string of length 3 found Notice how Python informs us of what is expected – a character, but a string was passed in of length 3. ...

November 11, 2021 · 6 min · 1158 words · Ryan Sheehy

Split A String Into List Of Characters In Python [Code Examples, One-Liner, No Imports]

A common requirement in Python is to split a string into the characters which make up the string. I’ve previously shown how you can do this by breaking up a word into separate cells using a spreadsheet, but how do you do this in Python? As a string is a data type that can be iterated it means each unit element within the string, being a character, can be referencing an index on the string. ...

November 11, 2021 · 3 min · 625 words · Ryan Sheehy

List Length In Python: Using len() Function

To find the length of a list use the built-in len() function which takes a single parameter that is iterable. For example this could be a string, list, tuple or dictionary and here is how it would look: 1 2 3 4 5 6 7 8 9 >>> my_list = ['a', 'b', 'c'] >>> len(my_list) 3 >>> my_dict = {'a': 100, 'b': 200, 'c': 300} >>> len(my_dict) 3 >>> my_str = 'How long is a piece of string?' >>> len(my_str) 30 This may be the easiest and quickest way of being able to find the length of a list and other data types, but what if you want to exclude certain items from your list? ...

November 11, 2021 · 3 min · 435 words · Ryan Sheehy

How To Use NOT Operator In IF Statement In Python [Code Examples]

One way to determine if one value does not equal another is to use the != comparator, but what if you’re not comparing two values – how can you determine if something is not true? Python has an inbuilt operator aptly called not which permits a user to check a variable, or a function’s result to test if the variable or returned value is not valid. Some classic use cases of this type of logic applied in my Python code have been checking if a file exists, other instances have been when scraping a web page and checking if the element existed on the page. ...

November 10, 2021 · 4 min · 850 words · Ryan Sheehy

Enumerate Dictionary In Python: Video Demonstration

Previously I looked at how to use the enumerate() built-in function with a for loop to provide two variables when iterating through a list. In that article the enumerate() function provides both an index number and the element in each list by wrapping these together into a tuple. But what happens when the enumerate() function is applied to a dictionary? As done previously let’s inspect the output of the enumerated dictionary by printing its contents: ...

November 9, 2021 · 2 min · 403 words · Ryan Sheehy

How To Use 2 Variables In For Loop In Python (Code Examples)

The for loop in Python allows the user to iterate through a variable that is iterable, such as a list. In Python the syntax for the loop is simply: for x in my_list: and you’re off looping through each element in my_list. But how do you access the index number of the element? In other programming languages, such as JavaScript, looping involves just the index number: 1 2 3 for (var i = 0; i < my_list.length; i += 1) { var elem = my_list[i] } But what would be Python’s equivalent? ...

November 9, 2021 · 3 min · 487 words · Ryan Sheehy

How To Print A Tab In Python

How do you print a tab character in Python? The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' (don’t forget the quotation marks – it needs to be parked inside a string). To see the tab-spaced character in the REPL output pane, wrap any variable containing a tab character in the built-in print() function. Here’s a simple example: 1 2 3 >>> my_tabbed_string = 'Space\tman' >>> print(my_tabbed_string) Space man What if you’d prefer to see the tab character instead of the actual spacing? ...

November 7, 2021 · 3 min · 528 words · Ryan Sheehy

How To Print A List In Python (And With Custom Formatting)

When you want to print the contents of a list you have a couple of different options depending upon the context. If you’re in the REPL there’s the easy method of entering the name of the variable storing the list, to the option of using the standard print function. If you’re within a script file the best option would be to use the print function, or if you want to import the sys library to write the equivalent of print by using sys.stdout.write(). ...

November 5, 2021 · 4 min · 752 words · Ryan Sheehy

Python Increment By 1

How do you increase a variable by one in Python? In other popular programming languages to increment a variable by one you can use the simple increment syntax of ++, for example i++ will make the value of i increase by 1, but how do you do this in Python? To increment a variable in Python use the syntax += 1, for example to increment the variable i by 1 write i += 1. This is the shorter version of the longer form syntax i = i + 1. ...

October 24, 2021 · 6 min · 1079 words · Ryan Sheehy

Transpose A List Of Lists: Python Examples, No Imports

When using a spreadsheet it can be as easy as using the TRANSPOSE function on a range to transform the rows and columns into columns and rows, but how do you do that in Python? To transpose a list of lists in Python first flatten the two-dimensional list into a one-dimensional list then use the following: [flat_list[e::len(list_2d[0])] for e in range(len(list_2d[0]))] where flat_list represents the flattened list, and list_2d represents the original 2D list. ...

October 22, 2021 · 6 min · 1210 words · Ryan Sheehy