Category: Code Snippets

Python get current directory

Introduction Today, we are going to talk about directories, files and paths in Python. If you are in rush and only need to know the syntax, here is how to get the current working directory… For more information, continue reading… Python provides dedicated cross platform modules to interface with the operating system and file system.

x for x in Python

Introduction This is going to be a compact post. Did the title catch your attention? Actually, It was intentional as we could have named it list comprehensions in Python. Looping in Python is awesome, if you are curious, you may check this post for more details about loops. In this post, we are only going

Hangman in Python

Introduction In this Python code snippet, we are going to implement an easy hangman solver using simple source code. The game code is adapted to work with both Python 2.x and Python 3.x. The example provided is a console based application for beginners. Let us get started with the tutorial… Python 2.x program There is

Python random number generator code

Introduction Ready for more code snippets? In this post, we are going to talk about random numbers in Python. Have you ever thought about what random means or even if randomness makes any sense in the context of computing! (as computers execute instructions blindly). Is randomness useful? can we apply it in real life computational

Python unicode strings tutorial

Introduction Unicode is an important topic in computing but it is a little bit confusing. In particular, it can be more confusing in the context of Python programming language. For that reason, the aim of our article today is to clarify the ambiguity and hopefully put things into perspective. Beginners may get confused because they

Python compare strings

Introduction It is hard (even impossible) to find a real world application not using string comparison. It can be used in database lookup, searching for files on disk, sorting contacts and many other scenarios. String comparison in Python is not hard as we will demonstrate in today’s code snippets. Let us get started… Python string

Difference between re.search and re.match in Python

Table of contents Introduction What is a regular expression? What is Python raw string? Python RE module Python regular expressions syntax summary Regular expressions flags re.match re.search Why use re.match? Regular expression compilation re.match and re.search examples re.fullmatch Match objects re.findall re.finditer re.split re.sub Summary References Introduction Welcome to a new Python code snippets post.