Python Programming Basics with Examples

Python is a popular and a powerful scripting language that can do everything, you can perform web scraping, networking tools, scientific tools, Raspberry PI programming, Web development, video games, and much more. With Python programming, you can do even system programming regardless the platform you are using.

Machine learning work can be done using some Python libraries like TensorFlow which is used in many artificial intelligence projects.

You can perform natural language processing (NLP) using Python libraries like NLTK.

Two days ago, a Russian friend was searching for Python programming on the website unfortunately, he didn’t find any posts about Python, and since Russian people are smart people.

I decided to start some Python good posts, so he gets satisfied and all of the site visitors.

We will discuss the basic Python programming in this post, and on the future posts will build tools and see Python programming in action.

If you find the content of this post is a little tricky, tell me so I start from zero level with Python programming in the upcoming posts, since I assume this for beginners with a very little understanding of what Python is.



Manipulating Strings

Strings in Python are immutable, so they cannot be changed. Any change to a string’s contents requires making a new copy.

Dealing with string is very simple in Python.

String Concatenation

This is string concatenation, we can also multiply a string like this:

String Multiplication

Concatenate with Non-string

You can concatenate with non-string by just converting the value to a string using the str() function like this:

Search for a Substring

You can search for a substring using find method like this:

The find method prints the position of the first occurrence of the likegeeks string if it’s found.

If nothing found, it will return -1 as a result.

The find function starts from the first character, however, you can start from the nth character like this:

Since we start from the 12th character, there is no word called likegeeks from that position so it will return -1.

Get Substrings

So we got the index of the string we’re searching for, now we want to print that matched.

You can print string by index like this:

The first print line prints from the first character till the second character, while the second print line prints from the second character till the end. Notice the position of the colon. The count starts from zero.

If you use a negative number, the counting will start backward instead, like the fourth print line which prints the last character in the string.

Replace strings

You can replace a string using the replace method like this:

If you have many occurrences and you want to replace the first occurrence only, you can specify that:

The first word only got replaced.

Strip Strings

You can trim white spaces from a string using the strip method like this:

You can strip from the right only or left only using rstrip() or lstrip() methods respectively.

Change Character Case

You can change the case of the characters if you want to compare them or something.

Convert Strings to Numbers

We have the str() function which casts the value to a string, but this is not the only cast function in Python programming.

There are int() , float() , long() and other cast functions that you can use.

The int() function cast the input to integer, while float() function cast the input to float.

The first print line just concatenates the two numbers without summation, while the second print line adds the two values and print the total.

Count Strings

You can use the min(), max() and len() functions to calculate the minimum character or maximum character value or the total length of characters.

Iterate over Strings

You can iterate over the string and manipulate every character individually like this:

The len() function counts the length of objects.

Encode Strings

If you are using Python 3, all strings are stored as Unicode strings by default, but if you are using Python 2 you may need to encode your strings like this:


Manipulating numbers

Numbers in Python programming are defined like this:


You can define integers, floats the same way.

If you have float number you can round it, using int() function like this:

Round Numbers

You can use the round() function to round numbers like this:

Just specify how many numbers need to be rounded to the round() function.

User-defined Precision Numbers

You may need to work with floating numbers that are of arbitrary precision.

You can use the decimal module that handles numbers of user-defined precision.

You can import the module like this:

Generate Random Numbers

The random module in Python gives functions to generate random numbers.

The generated number is between 0.0 and 1.0.

You can generate a random number from your choices like this:


Manipulating Dates and Times

Python programming language provides a module called datetime that helps in handling dates, times.

You can extract the value you need from the date, like the above examples.

You can get the differences between two times or two dates like this:

The timediff variable is an object of type timedelta, however, you can create this kind of object yourself like this:

Format Date and time

The strftime() method takes a format specification and it formats the date or time based on that format.

And the following table specifies some of the format options that you can use:

%a          Prints locale abbreviated name of the weekday.

%A         Prints the weekday.

%w         Prints the day of the week as a number.

%d          Prints the day of the month as a zero-padded number.

%b          Prints the month as locale abbreviated name.

%B          Prints the month as locale full name.

%m        Prints the month as a zero-padded number.

%y          Prints the year as a zero-padded two-digit number.

%Y         Prints the year as a zero-padded four-digit number.

%H         Prints the hour (24-hour clock) as a zero-padded number.

%I           Prints the hour (12-hour clock) as a zero-padded number.

%p          Prints AM or PM.

%M        Prints the minute as a zero-padded number.

%S          Prints the second as a zero-padded number.

Create Date from String

You can use the strptime() function to create a date from a string like this:

date1=datetime.datetime.strptime(“2015-11-21”, “%Y-%m-%d”)

Also, it can be created like this:

date1= datetime.datetime(year=2015, month=11, day=21)


Dealing with File System

Dealing with files is very easy in Python programming, believe it or not, this is the easiest language you can use to deal with files. You can say that Python is the easiest language in doing many things.

Copy Files

The shutil module contains a function for copying files.

If my_file.txt is a symlink, the above code will create copied_file.txt as a separate file.

You can create a copy of a symlink instead like this:

copied_path = shutil.copy('my_file.txt', 'copied_file.txt', follow_symlinks=False)

Move Files

You can move files from one location to another like this:

You can rename a file using the rename function from os module like this:

Read and Write Text Files

You can use the open function to open files, and then use the read or write methods to read from them and write to them.

First, we open the file for reading using the open function, then we start reading the file content using read function, finally, we put the grabbed content into the variable content.

You can specify how many bytes you want to read for the read() function:

If the file is not too big, you can read the entire contents into a list, then iterate over that list to print the output.

You can write to a file by specifying the mode to open function like this. You have two modes of writing, the write mode and append mode.

This is the write mode where you will overwrite the old file content.

And this is the append mode:

Creating Directories

You can create a new directory using mkdir function from os module like this:

This code will throw an error if the directory exists. Don’t worry, we will talk about exception handling in future posts so you can avoid such errors.

Get Access & Modification & Creation Time

You can use getmtime(), getatime() and getctime() to get modification time, access time and creation time respectively.

The returned time is formatted as a Unix timestamp, we can convert it to human readable format like this:

Iterating Over Files

You can use listdir() function from os module to get the files:

Also, you can use the glob module to do the same thing:

You can write any extension for file globbing, like *.doc to get all word documents only.

Serializing Python Objects

This process is used to serialize Python object to a byte stream for later reuse.

You can do that using pickle module:

You can deserialize this data using load() function like this:

Compressing Files

The Python standard library enables you to work with different types of compressed files like tar, zip, gzip, bzip2.

To work with a zip file you can use the zipfile module:

You can create a zip file from your files like this:

You can extract the zip file using extractall() method like this:

Also, you can append files to existing zip file by using append mode like this:

The same coding above is done when dealing with gz or bz files. You need to use gzip module or bz2 module.

Then you can read and write in the same way.

You can deal with rar files using unrar package. First, install the package

pip install unrar

Then you can use it the same way.

Parse CSV Files

A very useful package called pandas. This package can parse CSV and excel files, and extract data from it easily.

First, install the package

pip install pandas

Then you can use it in your modules like this:

Pandas treats the first column as a label for each row by default.You can pass the  index_col parameter to specify the column index if it’s not the first column.

If there are no row labels in your document, you should use the parameter index_col=False.

To write to CSV file, you can use to_csv() method.


Parse Excel Files

You can use read_excel() method from pandas module to parse excel files.

data = pd.read_excel('file.xls', sheetname='Sheet1')

If you have multiple sheets, you can load it like this:

data = pd.ExcelFile('file.xls')

You can write to excel files like this:

data.to_excel('file.xls', sheet='Sheet1')


Networking and Connectivity

The Python language has a socket class that provides a way of accessing the network at a low level, it has to support many networking protocols.

This code we establish a connection to a host on IP on port 5050.

Once a socket has been opened, you can send and receive data.

m_sock.sendall(b'Hello World')

Notice that I used the b character before the string because data needs to be a byte string.

If have a bigger message you should iterate over your message like this:

For receiving data, you need to tell the methods how many bytes to read in at a time.

data_in = m_sock.recv(2000)

This works because you know for sure that the message being sent is less than 2000 bytes long.

If the message is big, you must loop over and over until you collect all of the separate chunks.

Here we define an empty buffer, then we start to write the message into the buffer.

Reading an E-Mail from POP mail server

We already discussed Linux mail server and everything about it, now, how we can access it using the Python programming?

The poplib module enables you to communicate with a POP server.

The getpass module asks the end user for passwords securely.

If you need a secure connection, use the POP3_SSL class instead.

To get the message list and the message count, you can do it like this:

Don’t forget to close any open connections after you finish working with the POP server.


Reading an E-Mail from IMAP mail server

You can work with an IMAP email server using the imaplib module.

If you are using SSL on your IMAP server, you should use the IMAP4_SSL class instead.

To get a list of e-mails, you need to actually do a search like this:

data =, 'ALL')

Then you can iterate over the returned e-mail indices in the data variable and fetch the message

msg = my_imap.fetch(email_id, '(RFC822)')

Finally, don’t forget to close the connection:

Sending an E-Mail

E-mails are sent using the SMTP protocol. The smtplib in Python is used to handle this.

If you are using SSL on your SMTP server, you should use the SMTP_SSL class instead.

Once the connection is opened, you can send the message like this:

Web Crawling

To talk to a web server, you need to use urllib.request submodule.

Post to a Web Page

If you need to submit a web form, you know that you should send POST request to the web page and that’s what we will do.

Note that we can use mechanize or urllib2, there are many ways to achieve that.

Create a Mini Server

The socket class supports listening for incoming connections.

Now you can accept incoming connections like this:

Also, don’t forget to close the connection after you’ve finished



Threading in Python Programming

Running multiple processes in parallel, which is called threading is very useful, especially when you need to run a process in a different thread and keep your current thread unattached to avoid freezing.

The Python language includes a module called threading that contains a Thread class.

If the function takes a long time to finish, you can check to see whether it is still running or not by using the is_alive() method.

Sometimes your threads need to access global resources safely. You can do that using locks.


Using Raspberry PI

With raspberry PI you can create your own technology. It’s a single board computer with cheap price.

You can use the Python module RPi.GPIO to work with the Raspberry PI.

First, install the package in your Raspberry PI like this:

$ sudo apt-get install python-dev python-rpi.gpio

Now you can use it in your scripts. You can write output on the Raspberry Pi’s GPIO bus:

Reading from the Raspberry Pi’s GPIO

You can use the RPi.GPIO Python module to read data in from the GPIO like this:

Actually, we’ve covered a tiny bit of Python, there is a lot to cover.

I promise you that I will do my best on the upcoming Python posts to cover the Python programming language basics, then we can start building awesome tools.

I hope you enjoy the post. Keep coming back.

Thank you.

10 thoughts on “Python Programming Basics with Examples

  1. Thanks.
    Please, should you do the basic tutorial?
    I have wanted some graphics interfaces tools (like Tweal Tool) on linux to easily change the grub, plymouth, lightdm, etc.
    I know that some of them just exist, but they are not complete. Grub, for example, has a lot of configurations that I only can modify by editing files manually.

    For sure I will be not able to do something so deep, but at least it will be a start.

    1. Great to hear that.
      Small starts become big things after that, so keep up the awesome work.
      I didn’t understand your question, do you mean you need a basic tutorial? or what?

      1. Yes. Is it possible?
        About graphical calls/commands/gui in general using phyton, will you write about?


  2. Thanks so much for sharing this, it really helps esp for beginners in python coding like me…

    I would like to ask if you could also come up with a tutorial that deals with basic web publishing with python including accepting keyboard strokes as input… my objective is to build a simple blog website that is BBS-like and didn’t need to use a mouse to navigate the pages… All it need is to type a keyword as a menu option, like a linux shell… Thanks so much in advance…

    1. You are welcome.
      The upcoming posts will be about web crawling and I think i will make some posts about machine learning and artificial intelligence using python.
      Regarding your question i think this is a small trick to work with and you don’t need python in this, you need to use some client side library like JQuery or modern one like Vuejs which enables you to listen for keyboard events and show & hide your HTML elements for visitors.

      1. Thanks again for your immediate response… Now, I understand.

        Back to python, if I may suggest to have a tutorial that is project based? I mean, each tutorial will really build a real working programs – console based apps like a text editor, file commander, or network tool program and in web apps like a basic blog static website, a simple cms website projects with explanations how it is done line by line or per paragraph?

        I know this is sounds too demanding but in the goal of teaching as I’ve read and watched a lot of python tutorials and books but they’re all the same, still leaves the student hanging, I mean, OK I read about the functions and its syntax, what now? how am I gonna use it in real scenario? Where and when I’m gonna apply those functions? Should I just go and google for other’s python codes and learn from it without knowing nor understanding deeply? This is also to avoid script kiddies, copy and paste codes only, no idea what each code really do…

        Again, thank you so much in advance. 🙂

        1. You are welcome at any time.
          I know what you mean. You need complete real-world application posts, so everything is combined and working together.
          I will do my best to make the upcoming posts to be like this.

          1. Yes please, so that this wouldn’t be an echo or just another python code overview. Others, title their piece as “how-to…” but all they write is just like an entry in man pages, not even showing how to use them in real application esp as you mentioned, machine learning, artificial intelligence and also data visualisation, lots of articles about python about data visualisation but they never showed any sample of a python application, only code snippets of functions… it was like telling a word in a dictionary but not including a sample on how to use it in real usage or application. 🙂 Thanks

          2. Some people like code snippets because it solve problems quickly.
            But avid readers like complete articles.
            Writing those kind of articles requires a lot of time and effort.
            Anyway, I’ll do my best.
            Thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *