{"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 In [2]:<\/p>\n\n\n\n Out[2]:<\/p>\n\n\n\n We can use single quotation marks too.In [3]:<\/p>\n\n\n\n Out[3]:<\/p>\n\n\n\n String can be space or digitsIn [4]:<\/p>\n\n\n\n Out[4]:<\/p>\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 We can bind or assign a string to another variable:In [26]:<\/p>\n\n\n\n Out[26]:<\/p>\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 In [10]:<\/p>\n\n\n\n In [11]:<\/p>\n\n\n\n In [14]:<\/p>\n\n\n\n We can also use negative indexing with strings:In [15]:<\/p>\n\n\n\n In [17]:<\/p>\n\n\n\n In [18]:<\/p>\n\n\n\n Out[18]:<\/p>\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 Out[19]:<\/p>\n\n\n\n In [20]:<\/p>\n\n\n\n Out[20]:<\/p>\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 In [23]:<\/p>\n\n\n\n Out[23]:<\/p>\n\n\n\n In [24]:<\/p>\n\n\n\n Out[24]:<\/p>\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 Out[25]:<\/p>\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 Out[29]:<\/p>\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 Out[31]:<\/p>\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 In [34]:<\/p>\n\n\n\n In [35]:<\/p>\n\n\n\n In [37]:<\/p>\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 In [38]:<\/p>\n\n\n\n In [39]:<\/p>\n\n\n\n Out[39]:<\/p>\n\n\n\n In [41]:<\/p>\n\n\n\n Out[41]:<\/p>\n\n\n\n In [43]:<\/p>\n\n\n\n Out[43]:<\/p>\n\n\n\n In [44]:<\/p>\n\n\n\n Out[44]:<\/p>\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 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 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 The dataset can be seen below:<\/p>\n\n\n\n <table font-size:xx-small>Artist Album Released Length Genre Music recording sales (millions) Claimed sales (millions) Released Soundtrack Rating (friends) Michael Jackson Thriller 1982 00:42:19 Pop, rock, R&B 46 65 30-Nov-82 10.0 AC\/DC Back in Black 1980 00:42:11 Hard rock 26.1 50 25-Jul-80 8.5 Pink Floyd The Dark Side of the Moon 1973 00:42:49 Progressive rock 24.2 45 01-Mar-73 9.5 Whitney Houston The Bodyguard 1992 00:57:44 Soundtrack\/R&B, soul, pop 26.1 50 25-Jul-80 Y 7.0 Meat Loaf Bat Out of Hell 1977 00:46:33 Hard rock, progressive rock 20.6 43 21-Oct-77 7.0 Eagles Their Greatest Hits (1971-1975) 1976 00:43:08 Rock, soft rock, folk rock 32.2 42 17-Feb-76 9.5 Bee Gees Saturday Night Fever 1977 1:15:54 Disco 20.6 40 15-Nov-77 Y 9.0 Fleetwood Mac Rumours 1977 00:40:01 Soft rock 27.9 40 04-Feb-77 9.5 <\/table><\/font><\/p>\n\n\n\n We are going to take a look at lists in Python. A list is a sequenced collection of different objects such as integers, strings, and other lists as well. The address of each element within a list is called an index. An index is used to access and refer to items within a list.In [8]:<\/p>\n\n\n\n Out[8]:<\/p>\n\n\n\n In [14]:<\/p>\n\n\n\n Lists can contain strings, floats, and integers. As a result, we can nest other lists, and we can also nest tuples and other data structures. The same indexing conventions apply for nesting:In [21]:<\/p>\n\n\n\n Out[21]:<\/p>\n\n\n\n We can also perform slicing in lists. For example, if we want the last two elements, we use the following command:In [24]:<\/p>\n\n\n\n Out[24]:<\/p>\n\n\n\n In [33]:<\/p>\n\n\n\n Out[33]:<\/p>\n\n\n\n In [34]:<\/p>\n\n\n\n Out[34]:<\/p>\n\n\n\n In [36]:<\/p>\n\n\n\n Out[36]:<\/p>\n\n\n\n In [37]:<\/p>\n\n\n\n Out[37]:<\/p>\n\n\n\n In [38]:<\/p>\n\n\n\n In [39]:<\/p>\n\n\n\n In [40]:<\/p>\n\n\n\n Out[40]:<\/p>\n\n\n\n In [42]:<\/p>\n\n\n\n Out[42]:<\/p>\n\n\n\n In [43]:<\/p>\n\n\n\n In [44]:<\/p>\n\n\n\n In [45]:<\/p>\n\n\n\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 skills. 1. STRING OPERATIONS What is sting? In [2]: #String is collection of words as givern below. “Umair […]<\/p>\n","protected":false},"author":1,"featured_media":1478,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2,58],"tags":[128,127,126,116,120,119,123,124,125,122],"class_list":["post-1436","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-geographical-information-system","category-gis-tutorials","tag-list-operations","tag-lists-in-python","tag-pyhton-escape-sequences","tag-python","tag-python-dictionary","tag-python-download","tag-python-indexing","tag-python-slicing","tag-python-stride","tag-python-string-operations"],"yoast_head":"\n1. STRING OPERATIONS<\/h2>\n\n\n\n
What is sting?<\/h3>\n\n\n\n
#String is collection of words as givern below.<\/em>\n\"Umair Asghar\"\n<\/pre>\n\n\n\n
'Umair Asghar'<\/pre>\n\n\n\n
'Umair Asghar'\n<\/pre>\n\n\n\n
'Umair Asghar'<\/pre>\n\n\n\n
'Umair 1 2 3 4 5 6 9'\n<\/pre>\n\n\n\n
'Umair 1 2 3 4 5 6 9'<\/pre>\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
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
'Uamir Asgahr'<\/pre>\n\n\n\n
2. Indexing<\/h2>\n\n\n\n
<\/figure>\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
# we can print index 6<\/em>\n\nprint(name[6])\n<\/pre>\n\n\n\n
A\n<\/pre>\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
print(name[-<\/strong>1])\n<\/pre>\n\n\n\n
r\n<\/pre>\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
len(\"Umair Asgahr\")\n<\/pre>\n\n\n\n
12<\/pre>\n\n\n\n
3. Slicing<\/h2>\n\n\n\n
name [0:5]\n<\/pre>\n\n\n\n
'Uamir'<\/pre>\n\n\n\n
name [6:12]\n<\/pre>\n\n\n\n
'Asgahr'<\/pre>\n\n\n\n
4. Stride<\/h2>\n\n\n\n
<\/figure>\n\n\n\n
name[::3]\n<\/pre>\n\n\n\n
'UiAa'<\/pre>\n\n\n\n
name[0:5:2]\n<\/pre>\n\n\n\n
'Umr'<\/pre>\n\n\n\n
5. Concatenate Strings<\/h2>\n\n\n\n
# Concatenate two strings<\/em>\n\nstatement =<\/strong> name +<\/strong> \"is the best\"\nstatement\n<\/pre>\n\n\n\n
'Uamir Asgahris the best'<\/pre>\n\n\n\n
6*<\/strong>name\n<\/pre>\n\n\n\n
'Uamir AsgahrUamir AsgahrUamir AsgahrUamir AsgahrUamir AsgahrUamir Asgahr'<\/pre>\n\n\n\n
changestring =<\/strong> name +<\/strong> ' is the great man.'\nchangestring\n<\/pre>\n\n\n\n
'Uamir Asgahr is the great man.'<\/pre>\n\n\n\n
6. Escape Sequences<\/h2>\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
# 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
#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
#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
\n<\/pre>\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
a =<\/strong> 'Umair Asghar is a great py developer'\nb =<\/strong> a.<\/strong>replace('great','greatest')\nb\n<\/pre>\n\n\n\n
'Umair Asghar is a greatest py developer'<\/pre>\n\n\n\n
#find<\/em>\nname.<\/strong>find('UA')\n<\/pre>\n\n\n\n
-1<\/pre>\n\n\n\n
# Find the substring in the string.<\/em>\n\nname.<\/strong>find('Umair')\n<\/pre>\n\n\n\n
-1<\/pre>\n\n\n\n
# If cannot find the substring in the string<\/em>\n\nname.<\/strong>find('Jasdfasdasdf')\n<\/pre>\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
About the Dataset<\/h3>\n\n\n\n
<\/li><\/ul>\n\n\n\nLists<\/h2>\n\n\n\n
Indexing<\/h3>\n\n\n\n
#create list<\/em>\nmylist =<\/strong> ['umair asghar','23','56.8']\nmylist\n<\/pre>\n\n\n\n
['umair asghar', '23', '56.8']<\/pre>\n\n\n\n
# Print the elements on each index<\/em>\n\nprint('the same element using negative and positive indexing:\\n Postive:',mylist[0],\n'\\n Negative:' , mylist[-<\/strong>3] )\nprint('the same element using negative and positive indexing:\\n Postive:',mylist[1],\n'\\n Negative:' , mylist[-<\/strong>2] )\nprint('the same element using negative and positive indexing:\\n Postive:',mylist[2],\n'\\n Negative:' , mylist[-<\/strong>1] )\n<\/pre>\n\n\n\n
the same element using negative and positive indexing:\n Postive: umair asghar \n Negative: umair asghar\nthe same element using negative and positive indexing:\n Postive: 23 \n Negative: 23\nthe same element using negative and positive indexing:\n Postive: 56.8 \n Negative: 56.8\n<\/pre>\n\n\n\n
List Content<\/h4>\n\n\n\n
# Sample List<\/em>\n\nnewL =<\/strong> [\"Umair Asghar\", 26.1, 1985, [1, 2], (\"N\", 1)]\nnewL\n<\/pre>\n\n\n\n
['Umair Asghar', 26.1, 1985, [1, 2], ('N', 1)]<\/pre>\n\n\n\n
9. List Operations<\/h2>\n\n\n\n
newL[3:5]\n<\/pre>\n\n\n\n
[[1, 2], ('N', 1)]<\/pre>\n\n\n\n
method extend<\/h2>\n\n\n\n
# Use extend to add elements to list<\/em>\n\nList =<\/strong> [\"Sting is here\",45,2532.2]\nList.<\/strong>extend(['newstring',235146])\nList\n<\/pre>\n\n\n\n
['Sting is here', 45, 2532.2, 'newstring', 235146]<\/pre>\n\n\n\n
Another similar method is append. If we apply append instead of extend, we add one element to the list:<\/h4>\n\n\n\n
List =<\/strong> [\"Sting is here\",45,2532.2]\nList.<\/strong>append(['newstring',235146])\nList\n<\/pre>\n\n\n\n
['Sting is here', 45, 2532.2, ['newstring', 235146]]<\/pre>\n\n\n\n
Each time we apply a method, the list changes. If we apply extend we add two new elements to the list. Therefore, The List is then modified by adding two new elements<\/h4>\n\n\n\n
# Use extend to add elements to list<\/em>\n\nList =<\/strong> [\"Sting is here\",45,2532.2]\nList.<\/strong>extend(['newstring',235146])\nList\n<\/pre>\n\n\n\n
['Sting is here', 45, 2532.2, 'newstring', 235146]<\/pre>\n\n\n\n
If we append the list [‘a’,’b’] we have one new element consisting of a nested list:<\/h2>\n\n\n\n
# Use append to add elements to list<\/em>\n\nList.<\/strong>append(['a','b'])\nList\n<\/pre>\n\n\n\n
['Sting is here', 45, 2532.2, 'newstring', 235146, ['a', 'b']]<\/pre>\n\n\n\n
As lists are mutable, we can change them. So, we can change the first element as follows:<\/h3>\n\n\n\n
# Change the element based on the index<\/em>\n\nA =<\/strong> [\"disco\", 10, 1.2]\nprint('Before change:', A)\nA[0] =<\/strong> 'hard rock'\nprint('After change:', A)\n<\/pre>\n\n\n\n
Before change: ['disco', 10, 1.2]\nAfter change: ['hard rock', 10, 1.2]\n<\/pre>\n\n\n\n
del<\/code> command<\/h3>\n\n\n\n
# Delete the element based on the index<\/em>\n\nprint('Before change:', A)\ndel<\/strong>(A[0])\nprint('After change:', A)\n<\/pre>\n\n\n\n
Before change: ['hard rock', 10, 1.2]\nAfter change: [10, 1.2]\n<\/pre>\n\n\n\n
split<\/code><\/h4>\n\n\n\n
# Split the string, default is by space<\/em>\n\n'hard rock'.<\/strong>split()\n<\/pre>\n\n\n\n
['hard', 'rock']<\/pre>\n\n\n\n
We can use the split function to separate strings on a specific character. We pass the character we would like to split on into the argument, which in this case is a comma. The result is a list, and each element corresponds to a set of characters that have been separated by a comma:<\/h5>\n\n\n\n
# Split the string by comma<\/em>\n\n'A,B,C,D'.<\/strong>split(',')\n<\/pre>\n\n\n\n
['A', 'B', 'C', 'D']<\/pre>\n\n\n\n
Copy and Clone List<\/h4>\n\n\n\n
# Copy (copy by reference) the list A<\/em>\n\nA =<\/strong> [\"hard rock\", 10, 1.2]\nB =<\/strong> A\nprint('A:', A)\nprint('B:', B)\n<\/pre>\n\n\n\n
A: ['hard rock', 10, 1.2]\nB: ['hard rock', 10, 1.2]\n<\/pre>\n\n\n\n
<<\/strong>img src=<\/strong>\"https:\/\/cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud\/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork\/labs\/Module%202\/images\/ListsVal.gif\" width=<\/strong>\"1000\" \/><\/strong>\n<\/pre>\n\n\n\n
File <\/strong>\"<ipython-input-44-70c12ba31b0d>\"<\/strong>, line <\/strong>1<\/strong>\n <img src=\"https:\/\/cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud\/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork\/labs\/Module%202\/images\/ListsVal.gif\" width=\"1000\" \/><\/strong>\n ^<\/strong>\nSyntaxError<\/strong>:<\/strong> invalid syntax\n<\/pre>\n\n\n\n
print('B[0]:', B[0])\nA[0] =<\/strong> \"hard rock\"\nprint('B[0]:', B[0])\n<\/pre>\n\n\n\n
B[0]: hard rock\nB[0]: hard rock\n<\/pre>\n","protected":false},"excerpt":{"rendered":"