Call Us On : +91 7042 195 641  [email protected]

Python for beginners

Hello and Welcome to an interesting tutorial series on one of the most trending programming language, Python. In this series ‘Python for beginners’, we will discuss Python for beginners from scratch! So let’s start with a brief introduction.

python for beginners
Python for beginners

What is Python?

Python is a powerful open-source language with elegant syntax, easy-to-use language, and having a large standard library. Python supports many common programming tasks such as accessing files from the OS,  reading & modifying files,  connecting to web servers, web development, game development, and data visualization.

Why Python?

Python is the world’s fastest-growing and most popular programming language among software engineers, mathematicians, data analysts, accountants, and even kids because of its very beginner-friendly syntax and operations. People from different disciplines use python for a variety of different tasks such as Data Analysis, Machine Learning, Artificial Intelligence (AI), and Automation.

Automation is one of the biggest uses of Python amongst people who are not software developers or have any programming background. For example, if you constantly have to do boring, repetitive tasks such as copying files and folders around, renaming them, and uploading them to a server, a simple python code of few lines would do all these tasks within a few seconds for you! And that’s just one example. If you continuously have to work with excel sheets, pdf’s, CSV files, etc., you can automate all these using a simple python script.

So you don’t need to be a software developer to use Python. You could be a mathematician, an accountant, or any creative person, and use python to make your life easier. Some of the more useful applications of Python include web applications, mobile & desktop applications, testing, and hacking making it a multipurpose language. Finally, let’s summarize the reasons why you should learn Python.

  • It solves complex problems in a very few lines of code and in very less time.
  • Python has a huge community to solve your issues whenever you’re stuck somewhere.
  • It is cross-platform, so you can build and run your Python code in Windows, Mac, and Linux.
  • It has a large ecosystem of libraries, tools, and frameworks.

Python Installation

Python is an open-source programming language, so you can use and deploy anywhere for free! You can download the latest version of Python from its official website. Python is cross-platform and therefore you can use it in your favorite operating system. For installing Python on Unix/Linux platforms follow this link and Windows users can find the installation guide here.

So let’s begin our journey of learning Python for beginners with the “First Python Program”!

First Python Program

Let’s create our first python program that is super easy! In this program, you will print ‘Hello World’ on your screen. And to do this, you need to write a one-line command.

print('Hello World')

% output
Hello World

Here print is a function and the entity within the single quotes is the argument of the function. The type of the argument here is string. We will discuss different kinds of data types later in the tutorial. A snapshot of the Python print command run on the command window in Windows OS is shown in the following figure.

Hello World - Python for beginners
Hello World – Python for beginners

Strings

A string is a group of characters. So ‘Hello World’ is a string that is a collection of characters. You can also call these characters as the elements of the string. Another way of using the print function is to first store the string into a variable and then passing it into the print function. For example,

message = 'Hello Universe'
print(message)

% output
Hello Universe

Here the string ‘Hello World’ is stored in the variable ‘message’, and it is passed into the print function.

You can also use double quotes for displaying a message, eg. “Hello World”. Python gives you both the options because if a single quote is already used in the string, it would be difficult to understand which one of them is outer quotes and which are the inner ones. For example, if you wish to print ‘This is Harry’s book’, it will give an error. So to print this, you need to write the string in double-quotes as follows.

print("This is Harry's book")

% output
This is Harry's book

Similarly, if the string contains a double quote, use single quotes as the outer one.

If you wish to create a multi-line string, you need to use three double quotes before and after the string. For example,

message = """Hello, My name is Adam.
I live in London.
I study programming."""
print(message)

% output
Hello, My name is Adam.
I live in London.
I study programming.

Length of a string

You can find the length of the string using the len() function. It returns the number of characters present in the string. For example,

msg = "Hello World"
print(len(msg))

% output
11

Note that the spacing is treated as a character in the string. The len() function returns the length of other data types as well (lists, tuples, dictionaries). We will discuss these data types in the later sections.

Index of a string

The index represents the location of a character in the string. For example, in the string ‘Hello World’, ‘H’ is located in the first position, ‘e’ in second, and so on. Indexing in Python starts with 0. That is, the index of H is 0, that of e is 1, and so on.

To get the element/character at a particular position, you need to write the variable name followed by the index in square brackets. For example,

msg = 'Hello World'
print(msg[0])

% output
H

Similarly, for the subsequent elements,

print(msg[1])
print(msg[2])
print(msg[3])
print(msg[4])

% output
e
l
l
o

Since the length of the variable msg is 11, the maximum index is 10 (as it starts with 0).

print(msg[10])

% output
d

If you try to pass an index that exceeds the length of the string, you’ll get an error.

You can access the range of indices also. For example, to print only ‘Hello’ using the string variable msg, use the following command.

print(msg[0:5])

% output
Hello

Here 0 is the starting index, the colon is used for a range of indices, and the number after colon (5 here) indicates the index up to which the values are to be shown. Note that the starting index is inclusive (i.e, 0th index is included) and the ending index is exclusive (i.e, all indices before 5 is included but not the 5th one).

Omitting the first index will automatically consider 0 as the first index. For example, excluding 0 in the above command produces the same result.

print(msg[:5])

% output
Hello

Similarly, only starting index followed by a colon (without the last index) returns the element at the starting index and all the elements after it. For example,

print(msg[6:])

% output
World

You can access the last element of the string by setting index as -1.

print(msg[-1])

% output
d

print(msg[-2])

% output
l

Accessing a range of characters using negative indices is also possible.

print(msg[-5:])

% output
World

Functions/Methods associated with a variable

There are some in-built functions used to access some useful functionalities associated with the variable. For example,

message = 'Hello World'

1. Lowercase of the string

print(message.lower())

% output
hello world

2.  Uppercase of the string

print(message.upper())

% output
HELLO WORLD

3. Count the number of times a character (or a set of characters) has appeared in the string.

print(message.count('Hello'))

% output
1

print(message.count('l'))

% output
3

4. Find the index of a particular character in the string.

print(message.find('World'))

% output
6

The word ‘World’ starts at index 6.

print(message.find('l'))

% output
2

If you try to find a character that doesn’t exist in the string, it will return -1.

print(message.find('Hi'))

% output
-1

5. Replace a set of characters with other ones. The syntax for replace method is- .(‘ ‘, ‘ ‘). For example,

message.replace('World', 'Universe')
print(message)

% output
Hello World

We got the same result even after applying the replace() function! That is because only applying replace() is not sufficient. We need to store the output message into a variable. There are two ways you can do it. One is to store it in a new variable and then print it.

new_message = message.replace('World','Universe')
print(new_message)

% output
Hello Universe

Note that the original variable (message) is still unchanged.

print(message)

% output
Hello World

So what if we want the original variable to be updated with the replaced string? It’s simple! We store the output string in the original string itself.

message = message.replace('World','Universe')
print(message)

% output
Hello Universe

Concatenation of strings

Concatenation means joining two (or more) strings together. You can concatenate strings in python using + operator. For example,

print('Hello'+'Adam')

% output
HelloAdam

The strings are concatenated but the spacing is not maintained. You can do it two ways – either concatenate a space in between or make put a space within the string.

print('Hello'+' '+'Adam')
print('Hello'+' Adam')

% output
Hello Adam
Hello Adam

Another way to demonstrate the concatenation is by using variables for each string.

greeting = 'Hello'
name = 'Adam'
message = greeting+', '+name
print(message)

% output
Hello, Adam

You can concatenate more strings together to add other fields like – Hello, Adam. Welcome! However, using multiple + operator might be complicated or hard to keep track of.

To overcome this issue, we have a formatted string where placeholders are used to store the variables, and the rest of the string is written as it is. For example,

message = '{}, {}. Welcome!'.format(greeting,name)
print(message)

% output
Hello, Adam. Welcome!

Here { } are the placeholders and format is the function that places these variables into the placeholders.

The f’string

Another simple way of formating string is the f string. This feature is available in python 3.6 or higher version. Here you don’t need to write the variables separately. The variables are written within the place holders. The syntax for f string is,

message = f'{greeting}, {name}. Welcome'
print(message)

% output
Hello, Adam. Welcome

An interesting feature of this format is that you can use the variable functions within the placeholders. For example,

message = f'{greeting}, {name.upper()}. Welcome'
print(message)

% output
Hello, ADAM. Welcome

Ok, so this completes the first tutorial of the series ‘Python for beginners’ where we’ve learned the introduction and usage of Python and studied the python string in detail. In the next tutorial, we’ll learn about the Python Integers and Floats datatypes.

Thank you.

Contact us!

For training, Research Assistance, and Consultancy Services on Computer Vision, Machine Learning, Deep Learning, Signal Processing, Image Processing, and Medical Signal/Image Processing using Python and MATLAB.

Close Menu