{"id":314,"date":"2019-12-31T00:54:41","date_gmt":"2019-12-31T00:54:41","guid":{"rendered":"https:\/\/towardsbetterworld.com\/?page_id=314"},"modified":"2020-01-01T18:57:20","modified_gmt":"2020-01-01T18:57:20","slug":"basic-python","status":"publish","type":"page","link":"https:\/\/towardsbetterworld.com\/index.php\/basic-python\/","title":{"rendered":"Basic Python"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">data types-<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Integer(int) &#8211; Whole numbers 2, 200, 350<\/li><li>Floating Point(float) &#8211; Numbers with decimals 3.5, 5.6, 200.0<\/li><li>Strings(str) &#8211; Ordered sequence of characters &#8220;hello&#8221; &#8216;hi&#8217; &#8220;240&#8221;<\/li><li>Lists(list) &#8211; Ordered sequence of objects [&#8220;hi&#8221;,123,4.0]<\/li><li>Dictionaries(dict) &#8211; Unordered key value pairs {&#8220;key1&#8243;:&#8221;value1&#8221;, &#8220;key2&#8243;:&#8221;value2&#8221;}<\/li><li>Tuples(tup) &#8211; Ordered immutable sequence of objects (&#8220;hi&#8221;,123,4.0)<\/li><li>Sets(set) &#8211; Unordered collection of unique objects : {&#8220;a&#8221;,&#8221;b&#8221;}<\/li><li>Booleans(bool) &#8211; Logical value indicating true or false. <\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Python is dynamically typed, so you do not need to mention the data type while doing variable assignment.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">strings &#8211;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>>mystring = \"Hello World\"\n>mystring[0]\n'H'\n>mystring[8]\n'r'\n>mystring[-2]\n'l'\n#Slicing\n>mystring = \"abcdefghijk\"\n>mystring\n'abcdefghijk'\n>mystring[:3]\n'abc'\n>mystring[3:6]\n'def'\n>mystring[::2]\n'acegik'\n>mystring[2:7:2]\n'ceg'\n>mystring[::-1]\n'kjihgfedcba'\n\n#String Concatenation\n>a=\"Hello\"\n>b=\"World\"\n>a+\" \"+b\nHello World\n\n#String Formatting\n>print('The {} {} in the {}'.format('Sun','rises','East.'))\nThe Sun rises in the East.\n\n>name = \"Sam\"\n>age = 30\n>print(f'{name} is {age} years old.')<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">lists &#8211;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>>mylist = [1, 2, 3]\n>mylist[0]\n1\n>mylist[1:]\n[2,3]\n>mylist2 = [4,5]\n>mylist + mylist2\n[1,2,3,4,5]\n#mutable\nmylist[0]=100\n>mylist\n[100,2,3]\n>mylist.append(200)\n[100,2,3,200]\n>mylist.pop()\n200\n>mylist\n[100,2,3]\n>mylist.pop(0)\n100\n>mylist\n[2,3]\n\n#Sorting\n>list1 = ['d','b','c','a']\n>list1.sort()\n>list1\n>['a','b','c','d']<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">dictionaries &#8211;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#Unordered Key value pairs\n>prices = {'apples':2.99,'milk':5.00}\n>prices['apple']\n2.99\n>d = {'k1':123,'k2':[0,1,2],'k3':100}\n>d['k2']\n[0,1,2]\n>d.keys()\ndict_keys(['k1','k2','k3'])<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">tuples-<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Tuples are similar to lists except that they are immutable  and once created cannot be changed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>t = (1,2,3)\n>type(t)\ntuple\n>t1 = ('one', 2)\n>t1[0]\n'one'\n>t1[-1]\n2\n>t2 = ('a','a','b')\n>t2.count('a')\n2\n>t2.index('a')\n0 <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">sets &#8211;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#Sets are unordered collections of unique elements.\n>myset = set()\n>myset.add(1)\n>myset\n{1}\n>myset.add(2)\n{1, 2}\n>mylist = [1,1,1,1,2,2,2,3,3,3]\n>set(mylist)\n{1, 2, 3}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">booleans &#8211; <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#To convey true or false\n>True\nTrue\n>False\nFalse\n>type(False)\nbool\n>1 > 2 \nFalse\n>1==1\nTrue<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">File I\/O<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>>myfile = open('myfile.txt')\n>myfile.read()\n'Hello World'\n>myfile.read()\n''\n>myfile.seek(0)\n0\n>myfile2.readlines()\n['line1','line2']\n>myfile2.close()\n>with open('myfile.txt') as new_txt_file:\n     contents = new_text_file.read()\n#no need to close the file now<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">comparison operators &#8211; <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>==(Checks if two operands are equal)\n!=(Checks if two operands are not equal)\n> (Checks if one operand is greater than the second operand)\n&lt; (Checks if one operand is smaller than the second operand)\n>=(Checks if one operand is greater than or equal to the second operand)\n&lt;=(Checks if one operand is smaller than or equal to the second operand)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PYTHON STATEMENTS &#8211; <\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">if elif and else statements &#8211;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition: \n   &lt;statement>\nelif condition:\n   &lt;statement>\nelse condition:\n   &lt;statement><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">for loop &#8211; <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>my_iterable = [1, 2, 3]\nfor item in my_iterable:\n   print(item)\n>1\n>2\n>3\nfor x in 'Hello'\n   print(x)\n>H\n>e\n>l\n>l\n>o\n\nmy_list1 = [(1,2,3),(4,5,6),(7,8,9)]\nfor a,b,c in my_list1\n    print(b)\n>2\n>5\n>8\n\nd = {'a':1,'b':2,'c':3}\nfor value in d.values():\n   print(value)\n>1\n>2\n>3<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Useful operators &#8211; <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = [1,2,3]\nlist2 = ['a','b','c']\nfor item in zip(list1,list2)\n    print(item)\n>(1,'a')\n>(2,'b')\n>(3,'c')\n\n'a' in 'a world'\n>True\n\nresult = input('What is your age ?')<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">*args and **kwargs &#8211;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#you can provide any number of arguments and it will create a tuple from the arguments and process it.\ndef myfunc1(*args):\n   print(args)\n>myfunc1(4,10,15,20)\n(4,10,15,20)\n\n#you can provide a dictionary and use that to process in the function, you do not need to specify anything about that dictionary while creating function.\ndef myfunc2(**kwargs):\n    print(kwargs)\n>myfunc2(key1='value1', key2 = 'value2')\n{'key1':'value1', 'key2':'value2'}\n\n#You can also use the tuple and dictionary together.\ndef myfunc3(*args, **kwargs):\n    print(args)\n    print(kwargs)\n>myfunc3(2,3,4,key1='value1',key2='value2')\n(2,3,4)\n{'key1':'value1','key2':'value2'}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>data types- Integer(int) &#8211; Whole numbers 2, 200, 350 Floating Point(float) &#8211; Numbers with decimals 3.5, 5.6, 200.0 Strings(str) &#8211; Ordered sequence of characters &#8220;hello&#8221; &#8216;hi&#8217; &#8220;240&#8221; Lists(list) &#8211; Ordered sequence of objects [&#8220;hi&#8221;,123,4.0] Dictionaries(dict) &#8211; Unordered key value pairs {&#8220;key1&#8243;:&#8221;value1&#8221;, &#8220;key2&#8243;:&#8221;value2&#8221;} Tuples(tup) &#8211; Ordered immutable sequence of objects (&#8220;hi&#8221;,123,4.0) Sets(set) &#8211; Unordered collection of &hellip; <a href=\"https:\/\/towardsbetterworld.com\/index.php\/basic-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Basic Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"class_list":["post-314","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/pages\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":24,"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/pages\/314\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/pages\/314\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/towardsbetterworld.com\/index.php\/wp-json\/wp\/v2\/media?parent=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}