Python programming basics
Python

Python Programming Basics with Examples

Python is a popular and a powerful scripting language that can do everything, you can do web crawling, 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.

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 decide 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 start 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.

You can use a negative number to start counting 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 whitespaces 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

Since upper case and lower case characters are treated differently, 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:

Here we’ve used the len() function which 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:

a=15

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 locale full name of 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.

Copying Files

There are two functions from the shutil module that can be used to copy files.

If file1.txt is a symlink, this function call will create file2.txt as a separate file.

If you want to actually create a copy of the symlink instead, you can do it like this:

new_path = shutil.copy('file1.txt', 'file2.txt', follow_symlinks=False)

Moving Files

You can move files from one location to another using move function from shutils module

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:

fd.read(20)

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:

By default, pandas will treat the first column as a label for each row. If the row labels are in a different column, you can use the parameter index_col to specify the column index.

If there is no row labels at all, you will probably need to use the parameter index_col=False.

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

data.to_csv('file.csv)

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 that you wish to work with, you can load it like this:

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

The same way like CSV files, 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 different networking protocols.

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

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

my_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 = my_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 longer, 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?

There is a module in python called poplib that enables communication with a POP server.

The getpass module helps your code to ask in a secure way for passwords from the end user.

If the POP server you are using is secured, you need to use the POP3_SSL class instead.

After successful connection, you can interact with the POP server.

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

pop_serv.quit()

Reading an E-Mail from IMAP mail server

The imaplib module enables you to communicate with an IMAP email server.

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 = my_imap.search(None, 'ALL')

Then you can loop 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

The urllib in python handles communications over several different protocols.

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 from the Python standard library supports listening for incoming connections.

You may get a warning from your firewall, depending on the settings used on your machine.

After the socket has been created, you need to explicitly accept incoming connections.

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

conn.close()

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.

  • Yuri

    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.
    Thanks.

    • 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?
      Regards.

      • Yuri

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

        Thanks.

        • Sure I will.
          I think I will start with web crawling and going forward.

  • Raleigh Guevarra

    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…

    • 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.

      • Raleigh Guevarra

        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. 🙂

        • 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.
          Regards.

          • Raleigh Guevarra

            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

          • 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 🙂