{"id":37040,"date":"2016-08-23T20:36:14","date_gmt":"2016-08-24T01:36:14","guid":{"rendered":"http:\/\/luispalominotblog.wordpress.com\/?p=282"},"modified":"2016-08-23T20:36:14","modified_gmt":"2016-08-24T01:36:14","slug":"importing-using-and-creating-libraries-in-python","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/importing-using-and-creating-libraries-in-python\/","title":{"rendered":"Importing, using and creating libraries in Python"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/luispalominotblog.wordpress.com\/2016\/08\/24\/importin-using-and-creating-libraries-in-python\/\"><a href=\"https:\/\/luispalominotblog.wordpress.com\">Programming<\/a><\/a><\/em><br \/><\/p>\n<h2>Importing and using Module\/Library<\/h2>\n<p>A module (also called library) is a file containing Python definitions and statements. Python has many modules we can import. For example, when we need to use the trigonometrical\u00a0<em>sin() <\/em>or<em> cos()\u00a0<\/em>functions or we need to use the constant number pi we can import the module math using the import statement to use that module&#8217;s functions. So if we want to import the math module we would have to do something like this.<\/p>\n<p><img class=\" size-full wp-image-299 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/14.png?w=775\" alt=\"1.png\"   \/><\/p>\n<p>Inside the module math there are many different functions that can be used, whenever you want to use one of them you just need to type the library followed by a dot and the name of the function you want to use. See the example below where we print to the console the result of sin(3)<\/p>\n<p>*Notice that all trigonometrical functions take only radians\u00a0as parameters and NOT degrees<\/p>\n<p><img class=\" size-full wp-image-309 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/15.png?w=775\" alt=\"1.png\"   \/><\/p>\n<h2>Creating your own libraries<\/h2>\n<p>Now that you know how to import libraries in Python it&#8217;s time for us to take the next step and start making our own libraries, which will contain functions created by ourselves. Let&#8217;s say you are working on a project\u00a0that requires several functions to get the area of some geometrical\u00a0figures. Either you can create those functions within the file you are working on or you can have a separate file containing the functions. Let&#8217;s take a look at how it&#8217;s done. On the upper left side of your eclipse screen you have your modules (.py files), folders and packages as shown in the picture below.<\/p>\n<p><img class=\" size-full wp-image-316 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/25.png?w=775\" alt=\"2.png\"   \/><\/p>\n<p>Imagine that you are working with the <em>Exercices2.py<\/em> module (Sorry for the bad spelling). Now you are going to create another module\u00a0within the folder <em>FirstPartial.\u00a0<\/em>For the purpose of this example, I&#8217;m going to name the module <em>mathAreas.py<\/em><\/p>\n<p><img class=\" size-full wp-image-328 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/33.png?w=775\" alt=\"3.png\"   \/><\/p>\n<p>And now I&#8217;m <div class=\"post-limited-image\"><img class=\" size-full wp-image-338 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/52.png?w=775\" alt=\"5.png\"   \/><\/div>\n<div class=\"post-limited-image\"><img class=\" size-full wp-image-348 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/6.png?w=775\" alt=\"6.png\"   \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/luispalominotblog.wordpress.com\/282\/\" \/><\/div>\n<div class=\"post-limited-image\"><img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=luispalominotblog.wordpress.com&#038;blog=115253528&%23038;post=282&%23038;subd=luispalominotblog&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/><\/div><!--more--> to write some functions inside <em>mathAreas.py<\/em> to get the area of a circle, square, rectangle and triangle as shown in the picture below.<\/p>\n<p><img class=\" size-full wp-image-338 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/52.png?w=775\" alt=\"5.png\"   \/><\/p>\n<p>Now let&#8217;s return to the Exercices2.py module, we are going to use the import statement and select mathAreas. And then we are going to write some code to prove that the functions within mathAreas really work. We are going to print to the console the area of a rectangle whose sides are 4 cm x 10 cm.<\/p>\n<p><img class=\" size-full wp-image-348 aligncenter\" src=\"https:\/\/luispalominotblog.files.wordpress.com\/2016\/08\/6.png?w=775\" alt=\"6.png\"   \/><\/p>\n<p>Great! Now you know how to create your own libraries and import them. Keep the good work and I see you in the next blog post.<\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/luispalominotblog.wordpress.com\/282\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/luispalominotblog.wordpress.com\/282\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=luispalominotblog.wordpress.com&#038;blog=115253528&%23038;post=282&%23038;subd=luispalominotblog&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Programming Importing and using Module\/Library A module (also called library) is a file containing Python definitions and statements. Python has many modules we can import. For example, when we need to use the trigonometrical\u00a0sin() or cos()\u00a0functions or we need to use the constant number pi we can import the module math using &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/importing-using-and-creating-libraries-in-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Importing, using and creating libraries in Python<\/span><\/a><\/p>\n","protected":false},"author":14,"featured_media":299,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[28],"class_list":["post-37040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-uncategorized"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=37040"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37040\/revisions"}],"predecessor-version":[{"id":37041,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37040\/revisions\/37041"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=37040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=37040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=37040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}