{"id":1436,"date":"2022-03-27T18:59:04","date_gmt":"2022-03-27T18:59:04","guid":{"rendered":"https:\/\/visitgis.com\/?p=1436"},"modified":"2022-03-27T19:07:22","modified_gmt":"2022-03-27T19:07:22","slug":"9-basic-concepts-of-python","status":"publish","type":"post","link":"https:\/\/visitgis.com\/9-basic-concepts-of-python\/","title":{"rendered":"9 Basic Concepts of Python"},"content":{"rendered":"\n

Python is highly demanding programming language in job market. You can use python in large and small scale projects. As a GIS developer, it will be great addition in you skills, if you get expertise in python programming <\/a>skills.<\/p>\n\n\n\n

1. STRING OPERATIONS<\/h2>\n\n\n\n

What is sting?<\/h3>\n\n\n\n

In [2]:<\/p>\n\n\n\n

#String is collection of words as givern below.<\/em>\n\"Umair Asghar\"\n<\/pre>\n\n\n\n

Out[2]:<\/p>\n\n\n\n

'Umair Asghar'<\/pre>\n\n\n\n

We can use single quotation marks too.In [3]:<\/p>\n\n\n\n

'Umair Asghar'\n<\/pre>\n\n\n\n

Out[3]:<\/p>\n\n\n\n

'Umair Asghar'<\/pre>\n\n\n\n

String can be space or digitsIn [4]:<\/p>\n\n\n\n

'Umair 1 2 3 4 5 6 9'\n<\/pre>\n\n\n\n

Out[4]:<\/p>\n\n\n\n

'Umair 1 2 3 4 5 6 9'<\/pre>\n\n\n\n

String can be special characters.<\/p>\n\n\n\n

‘@#$%^&*()_’<\/p>\n\n\n\n

How to print the string?In [8]:<\/p>\n\n\n\n

print ('How to print the string')\n<\/pre>\n\n\n\n
How to print the string\n<\/pre>\n\n\n\n

We can bind or assign a string to another variable:In [26]:<\/p>\n\n\n\n

name =<\/strong> 'Uamir Asgahr'\nprint(name)\n#or simpel use name to print the statement.<\/em>\nname\n<\/pre>\n\n\n\n
Uamir Asgahr\n<\/pre>\n\n\n\n

Out[26]:<\/p>\n\n\n\n

'Uamir Asgahr'<\/pre>\n\n\n\n

2. Indexing<\/h2>\n\n\n\n

It is helpful to think of a string as an ordered sequence. Each element in the sequence can be accessed using an index represented by the array of numbers:<\/p>\n\n\n\n

\"\"\/<\/figure>\n\n\n\n

In [10]:<\/p>\n\n\n\n

# Print the first element in the string<\/em>\n\nprint(name[0])\n<\/pre>\n\n\n\n
U\n<\/pre>\n\n\n\n

In [11]:<\/p>\n\n\n\n

# we can print index 6<\/em>\n\nprint(name[6])\n<\/pre>\n\n\n\n
A\n<\/pre>\n\n\n\n

In [14]:<\/p>\n\n\n\n

print(name[11])\n<\/pre>\n\n\n\n
r\n<\/pre>\n\n\n\n

Negative Indexing<\/h3>\n\n\n\n

We can also use negative indexing with strings:In [15]:<\/p>\n\n\n\n

print(name[-<\/strong>1])\n<\/pre>\n\n\n\n
r\n<\/pre>\n\n\n\n

In [17]:<\/p>\n\n\n\n

print(name[-<\/strong>11])\n<\/pre>\n\n\n\n
a\n<\/pre>\n\n\n\n

Find the length of the string.<\/h3>\n\n\n\n

In [18]:<\/p>\n\n\n\n

len(\"Umair Asgahr\")\n<\/pre>\n\n\n\n

Out[18]:<\/p>\n\n\n\n

12<\/pre>\n\n\n\n

3. Slicing<\/h2>\n\n\n\n

We can obtain multiple characters from a string using slicing, we can obtain the 0 to 4th and 8th to the 12th element:In [19]:<\/p>\n\n\n\n

name [0:5]\n<\/pre>\n\n\n\n

Out[19]:<\/p>\n\n\n\n

'Uamir'<\/pre>\n\n\n\n

In [20]:<\/p>\n\n\n\n

name [6:12]\n<\/pre>\n\n\n\n

Out[20]:<\/p>\n\n\n\n

'Asgahr'<\/pre>\n\n\n\n

4. Stride<\/h2>\n\n\n\n

We can also input a stride value as follows, with the ‘2’ indicating that we are selecting every second variable:<\/p>\n\n\n\n

\"\"\/<\/figure>\n\n\n\n

In [23]:<\/p>\n\n\n\n

name[::3]\n<\/pre>\n\n\n\n

Out[23]:<\/p>\n\n\n\n

'UiAa'<\/pre>\n\n\n\n

In [24]:<\/p>\n\n\n\n

name[0:5:2]\n<\/pre>\n\n\n\n

Out[24]:<\/p>\n\n\n\n

'Umr'<\/pre>\n\n\n\n

5. Concatenate Strings<\/h2>\n\n\n\n

We can concatenate or combine strings by using the addition symbols, and the result is a new string that is a combination of both:In [25]:<\/p>\n\n\n\n

# Concatenate two strings<\/em>\n\nstatement =<\/strong> name +<\/strong> \"is the best\"\nstatement\n<\/pre>\n\n\n\n

Out[25]:<\/p>\n\n\n\n

'Uamir Asgahris the best'<\/pre>\n\n\n\n

To replicate values of a string we simply multiply the string by the number of times we would like to replicate it. In this case, the number is three. The result is a new string, and this new string consists of three copies of the original string:In [29]:<\/p>\n\n\n\n

6*<\/strong>name\n<\/pre>\n\n\n\n

Out[29]:<\/p>\n\n\n\n

'Uamir AsgahrUamir AsgahrUamir AsgahrUamir AsgahrUamir AsgahrUamir Asgahr'<\/pre>\n\n\n\n

You can create a new string by setting it to the original variable. Concatenated with a new string, the result is a new string that changes from Michael Jackson to \u201cMichael Jackson is the best”.In [31]:<\/p>\n\n\n\n

changestring =<\/strong> name +<\/strong> ' is the great man.'\nchangestring\n<\/pre>\n\n\n\n

Out[31]:<\/p>\n\n\n\n

'Uamir Asgahr is the great man.'<\/pre>\n\n\n\n

6. Escape Sequences<\/h2>\n\n\n\n

Back slashes represent the beginning of escape sequences. Escape sequences represent strings that may be difficult to input. For example, back slash “n” represents a new line. The output is given by a new line after the back slash “n” is encountered:In [32]:<\/p>\n\n\n\n

#line escape<\/em>\nprint ('Umair Asghar is a \\n Py developer')\n<\/pre>\n\n\n\n
Umair Asghar is a \n Py developer\n<\/pre>\n\n\n\n

In [34]:<\/p>\n\n\n\n

# tab escape<\/em>\nprint ('Umair Asgahr is not a \\t king py developer')\n<\/pre>\n\n\n\n
Umair Asgahr is not a \t king py developer\n<\/pre>\n\n\n\n

In [35]:<\/p>\n\n\n\n

#include backslash<\/em>\nprint ('Umair Asgahr is including \\\\ in the statement.')\n<\/pre>\n\n\n\n
Umair Asgahr is including \\ in the statement.\n<\/pre>\n\n\n\n

In [37]:<\/p>\n\n\n\n

#how to show string as it is. Its raw string use r in start of string.<\/em>\nprint (r'Umair Asghar is nothing but a  \\ py developer')\n<\/pre>\n\n\n\n
Umair Asghar is nothing but a  \\ py developer\n<\/pre>\n\n\n\n

7. String Operations<\/h2>\n\n\n\n

There are many string operation methods in Python that can be used to manipulate the data. We are going to use some basic string operations on the data.<\/p>\n\n\n\n

Let’s try with the method upper; this method converts lower case characters to upper case characters:In [ ]:<\/p>\n\n\n\n

 \n<\/pre>\n\n\n\n

In [38]:<\/p>\n\n\n\n

# Convert all the characters in string to upper case<\/em>\n\na =<\/strong> \"Thriller is the sixth studio album\"\nprint(\"before upper:\", a)\nb =<\/strong> a.<\/strong>upper()\nprint(\"After upper:\", b)\n<\/pre>\n\n\n\n
before upper: Thriller is the sixth studio album\nAfter upper: THRILLER IS THE SIXTH STUDIO ALBUM\n<\/pre>\n\n\n\n

Replace function<\/h2>\n\n\n\n

In [39]:<\/p>\n\n\n\n

a =<\/strong> 'Umair Asghar is a great py developer'\nb =<\/strong> a.<\/strong>replace('great','greatest')\nb\n<\/pre>\n\n\n\n

Out[39]:<\/p>\n\n\n\n

'Umair Asghar is a greatest py developer'<\/pre>\n\n\n\n

In [41]:<\/p>\n\n\n\n

#find<\/em>\nname.<\/strong>find('UA')\n<\/pre>\n\n\n\n

Out[41]:<\/p>\n\n\n\n

-1<\/pre>\n\n\n\n

In [43]:<\/p>\n\n\n\n

# Find the substring in the string.<\/em>\n\nname.<\/strong>find('Umair')\n<\/pre>\n\n\n\n

Out[43]:<\/p>\n\n\n\n

-1<\/pre>\n\n\n\n

In [44]:<\/p>\n\n\n\n

# If cannot find the substring in the string<\/em>\n\nname.<\/strong>find('Jasdfasdasdf')\n<\/pre>\n\n\n\n

Out[44]:<\/p>\n\n\n\n

-1<\/pre>\n\n\n\n

8. Lists in Python<\/h2>\n\n\n\n

Objectives<\/h3>\n\n\n\n

After completing this lab you will be able to:<\/p>\n\n\n\n

Perform list operations in Python, including indexing, list manipulation and copy\/clone list.<\/p>\n\n\n\n

About the Dataset<\/h3>\n\n\n\n

Imagine you received album recommendations from your friends and compiled all of the recommandations into a table, with specific information about each album.<\/p>\n\n\n\n

The table has one row for each movie and several columns:<\/p>\n\n\n\n