<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Morris Mulitu</title>
    <description>The latest articles on DEV Community by Morris Mulitu (@mulitu).</description>
    <link>https://dev.to/mulitu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F635485%2F7e1c6ce4-c8ae-425b-be63-9fa74bdd1a6a.jpeg</url>
      <title>DEV Community: Morris Mulitu</title>
      <link>https://dev.to/mulitu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mulitu"/>
    <language>en</language>
    <item>
      <title>First Flask app!</title>
      <dc:creator>Morris Mulitu</dc:creator>
      <pubDate>Wed, 11 Aug 2021 13:04:12 +0000</pubDate>
      <link>https://dev.to/mulitu/first-flask-app-3olk</link>
      <guid>https://dev.to/mulitu/first-flask-app-3olk</guid>
      <description>&lt;p&gt;If you want to build a web app in Python then it is highly likely that you will use a framework!. A &lt;a href="https://www.fullstackpython.com/web-frameworks.html"&gt;framework&lt;/a&gt; "is a code library that makes a developer's life easier when building reliable, scalable, and maintainable web applications" Flask is such a framework in Python.&lt;/p&gt;

&lt;p&gt;Flask is relatively a new framework. It has however taken the python web development world by storm. It has become quite popular. It is offering alot of extensibility, flexibility and clean code possibilities. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why Flask?
&lt;/h2&gt;

&lt;p&gt;Flask is thought to be more pythonic than &lt;a href="https://www.djangoproject.com/"&gt;Django&lt;/a&gt; due to the fact that the equivalent Flask web application is more explicit. Ideally, it is also easier to get started with for beginners. &lt;/p&gt;

&lt;h2&gt;
  
  
  Hello, World! with Flask
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;'Hello, World!'&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the Flask library. This code will show "Hello, World!" on localhost port 5000 if you run it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Flask startup!
&lt;/h2&gt;

&lt;p&gt;You can install the Flask package from PPI &lt;br&gt;
&lt;a href="https://pypi.python.org/"&gt;(Python Package Index)&lt;/a&gt;&lt;br&gt;
First make a directory eg, &lt;strong&gt;flask_todo&lt;/strong&gt; and then have the &lt;strong&gt;flask&lt;/strong&gt; package installed. Also install &lt;a href="https://flask-sqlalchemy.palletsprojects.com/en/2.x/"&gt;&lt;strong&gt;flask-sqlalchemy&lt;/strong&gt;&lt;/a&gt; to facilitate a simpler SQL database connection.&lt;br&gt;
It is best to work in a &lt;a href="https://docs.python.org/3/library/venv.html"&gt;virtual environment&lt;/a&gt;. &lt;br&gt;
Do enter the following commands;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir flask_todo
$ cd flask_todo
$ pipenv install --python 3.6
$ pipenv shell
(flask-someHash) $ pipenv install flask flask-sqlalchemy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just in case you want to make if a Git repo, do run &lt;em&gt;git init&lt;/em&gt; here.This will be the project's root. Just incase you intend to codebase to different machines you can have all the setup files in this directory.&lt;br&gt;
The best way to move it is to turn the codebase into an installable Python distribution. While at the root of the project create a &lt;strong&gt;setup.py&lt;/strong&gt; and a directory &lt;strong&gt;todo&lt;/strong&gt; to hold the source code.&lt;br&gt;
An example of the setup.py is as below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;setuptools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;find_packages&lt;/span&gt;

&lt;span class="n"&gt;requires&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;'flask'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'flask-sqlalchemy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'psycopg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'flask_todo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'0.0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'A To-Do List built with Flask'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'&amp;lt;Your actual name here&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;author_email&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'&amp;lt;Your actual e-mail address here&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;keywords&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'web flask'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;packages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;find_packages&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;include_package_data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;install_requires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;requires&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this you will have all the needed packages in the &lt;strong&gt;requires&lt;/strong&gt; list.Additionally, you'll also have everything you need to set up and install the package in &lt;strong&gt;site-packages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;todo&lt;/strong&gt; directory, create &lt;strong&gt;&lt;strong&gt;init&lt;/strong&gt;.py&lt;/strong&gt; and &lt;strong&gt;app.py&lt;/strong&gt; files.The &lt;strong&gt;init&lt;/strong&gt;.py file allows you to import from todo as if it was an installed package. The app.py file will be the application's root. Just in case you are using &lt;a href="https://pipenv.pypa.io/en/latest/"&gt;pipenv&lt;/a&gt; you can find your virtual environment with &lt;strong&gt;pipenv --venv&lt;/strong&gt; and set up that environment variable in your environment's &lt;strong&gt;active&lt;/strong&gt; script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export FLASK_APP=$VIRTUAL_ENV/../todo/app.py
export DEBUG='True'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A little code!
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="s"&gt;"""Print 'Hello, world!' as the response body."""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;'Hello, world!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a basic Flask application.&lt;em&gt;app&lt;/em&gt; is an instance of Flask taking in the &lt;em&gt;&lt;strong&gt;name&lt;/strong&gt;&lt;/em&gt; of the script file.&lt;br&gt;
&lt;em&gt;app.route&lt;/em&gt; specifies the routes used to access the application.Any view you specify must be decorated by &lt;em&gt;app.route&lt;/em&gt; to be a functional part of the application.In this example, when the app is running and accessed at &lt;em&gt;&lt;a href="http://domainname/"&gt;http://domainname/&lt;/a&gt;&lt;/em&gt;, the user will receive "Hello, World!" as a response.&lt;/p&gt;
&lt;h2&gt;
  
  
  Connecting the database in Flask
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;flask-sqlalchemy&lt;/em&gt; package connects a SQL database to a Flask application.You need the database URL to connect to a SQL database.You can use Postgres database.The intermediary to talk to the Postgres database is the &lt;em&gt;psycopg2&lt;/em&gt; package.Include it in the list of required packages in &lt;em&gt;setup.py&lt;/em&gt;. The &lt;em&gt;flask-sqlalchemy&lt;/em&gt; will recognize Postgres from the database URL. Flask will need the database URL to be part of its central configuration through the &lt;em&gt;SQLALCHEMY_DATABASE_URI&lt;/em&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask_sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SQLAlchemy&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'SQLALCHEMY_DATABASE_URI'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'postgres://localhost:5432/flask_todo'&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SQLAlchemy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make use of environment variables to make things easier.They ensures that, no matter what machine the code is on, it will always point tp the right stuff if that stuff is configured in the running environment.&lt;br&gt;
In the same place you declared &lt;em&gt;FLASK_APP&lt;/em&gt;, declare a &lt;em&gt;DATABASE_URL&lt;/em&gt; pointing to the location of your Postgres database. Development tends to work locally, so just point to your local database.&lt;/p&gt;

&lt;p&gt;Your application has a database connection! Awesome&lt;/p&gt;

&lt;p&gt;Do enjoy!&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction To Python Functions</title>
      <dc:creator>Morris Mulitu</dc:creator>
      <pubDate>Tue, 03 Aug 2021 17:27:20 +0000</pubDate>
      <link>https://dev.to/mulitu/introduction-to-python-functions-40ap</link>
      <guid>https://dev.to/mulitu/introduction-to-python-functions-40ap</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What is a function in Python?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In Python, a functions are a group of related statements that do specific tasks.&lt;br&gt;
Functions are used to create smaller chunks of code in python making programming more streamlined and organized. This hence makes code more understandable to others who who view it. Most importantly, functions make code less repetitive and enhance reusability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;function_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""docstring"""&lt;/span&gt;
    &lt;span class="n"&gt;statement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the example above;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The keyword &lt;strong&gt;def&lt;/strong&gt; indicates the start of the function.&lt;/li&gt;
&lt;li&gt;The function name is used to uniquely identify the function.&lt;/li&gt;
&lt;li&gt;Parameters / arguments are used to pass values to the function. They however are optional.&lt;/li&gt;
&lt;li&gt;The end of the function header is denoted by a colon(:)&lt;/li&gt;
&lt;li&gt;The (docstring) segment used above details what the funtion is supposed to do. &lt;/li&gt;
&lt;li&gt;There can be an optional &lt;strong&gt;return&lt;/strong&gt; statement to return values from the functions. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Example&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""
    This function greets to
    the person passed in as
    a parameter
    """&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;". Good morning!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Calling a function in python&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After defining a python function you can go on to call it from another function, a python prompt or maybe another program. &lt;br&gt;
Type the function name and parameters to call it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Morris'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Morris&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;Good&lt;/span&gt; &lt;span class="n"&gt;morning&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete function and its calling;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""
    This function greets to
    the person passed in as
    a parameter
    """&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;". Good morning!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Morris'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Morris. Good morning!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docstrings
&lt;/h2&gt;

&lt;p&gt;The documentation string or docstring is the first string after the function header. It details what the specific function does. It is optional but hightly recommended. Use triple quotes to enable the docstrings to span over multiple lines. &lt;/p&gt;

&lt;h2&gt;
  
  
  Return statement
&lt;/h2&gt;

&lt;p&gt;Used to exit the functions and go back to w herever it was called from.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return [expression_list]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A return statement can contain an expression that gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; print(greet("May"))
Hello, May. Good morning!
None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, None is the returned value since &lt;em&gt;greet()&lt;/em&gt; directly prints the name and no &lt;em&gt;return&lt;/em&gt; statement is used.&lt;/p&gt;

&lt;p&gt;Example of return&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;absolute_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""This function returns the absolute
    value of the entered number"""&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;


&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;absolute_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;absolute_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Python Function Arguments
&lt;/h2&gt;

&lt;p&gt;You can define a function that takes variable number of arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""This function greets to
    the person with the provided message"""&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;', '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monica"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Good morning!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Monica, Good morning!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the function &lt;em&gt;greet()&lt;/em&gt; has two parameters.&lt;br&gt;
If we call it with a different number of arguments, the interpreter will show an error message. Below is a call to this function with one and no arguments along with their respective error messages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; greet("Monica")    # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; greet()    # no arguments
TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variable Function Arguments
&lt;/h2&gt;

&lt;p&gt;There are other ways to define a function that can take variable number of arguments.&lt;/p&gt;

&lt;p&gt;Python Default Arguments&lt;br&gt;
We can provide a default value to an argument by using the assignment operator (=).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Good morning!"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""
    This function greets to
    the person with the
    provided message.

    If the message is not provided,
    it defaults to "Good
    morning!"
    """&lt;/span&gt;

    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;', '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Kate"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bruce"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How do you do?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Kate, Good morning!
Hello Bruce, How do you do?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, the parameter name does not have a default value and is required (mandatory) during a call.&lt;/p&gt;

&lt;p&gt;On the other hand, the parameter msg has a default value of "Good morning!". So, it is optional during a call. If a value is provided, it will overwrite the default value.&lt;/p&gt;

&lt;p&gt;Any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.&lt;/p&gt;

&lt;p&gt;This means to say, non-default arguments cannot follow default arguments. For example, if we had defined the function header above as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def greet(msg = "Good morning!", name):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would get an error as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SyntaxError: non-default argument follows default argument
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Python Keyword Arguments
&lt;/h2&gt;

&lt;p&gt;When we call a function with some values, these values get assigned to the arguments according to their position.&lt;/p&gt;

&lt;p&gt;For example, in the above function greet(), when we called it as greet("Bruce", "How do you do?"), the value "Bruce" gets assigned to the argument name and similarly "How do you do?" to msg.&lt;/p&gt;

&lt;p&gt;Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed. Following calls to the above function are all valid and produce the same result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 2 keyword arguments
greet(name = "Bruce",msg = "How do you do?")

# 2 keyword arguments (out of order)
greet(msg = "How do you do?",name = "Bruce") 

1 positional, 1 keyword argument
greet("Bruce", msg = "How do you do?")  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can mix positional arguments with keyword arguments during a function call. But we must keep in mind that keyword arguments must follow positional arguments.&lt;/p&gt;

&lt;p&gt;Having a positional argument after keyword arguments will result in errors. For example, the function call as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greet(name="Bruce","How do you do?")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will result in an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SyntaxError: non-keyword arg after keyword arg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Python Arbitrary Arguments
&lt;/h2&gt;

&lt;p&gt;Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with an arbitrary number of arguments.&lt;/p&gt;

&lt;p&gt;In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument. Here is an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""This function greets all
    the person in the names tuple."""&lt;/span&gt;

    &lt;span class="c1"&gt;# names is a tuple with arguments
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monica"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Luke"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Steve"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Monica
Hello Luke
Hello Steve
Hello John
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have called the function with multiple arguments. These arguments get wrapped up into a tuple before being passed into the function. Inside the function, we use a for loop to retrieve all the arguments back.&lt;/p&gt;

</description>
      <category>python</category>
      <category>functional</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python Basics, Pythons 101!
</title>
      <dc:creator>Morris Mulitu</dc:creator>
      <pubDate>Sat, 24 Jul 2021 17:10:26 +0000</pubDate>
      <link>https://dev.to/mulitu/python-basics-pythons-101-45dp</link>
      <guid>https://dev.to/mulitu/python-basics-pythons-101-45dp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction: What is Python?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python is a general-purpose, high level, interpreted language with easy syntax and dynamic semantics. The development of python started as a hobby for its creator Guido Van Rossum in 1989. He wanted to make a language that was beautiful to look at and easy for everyone to read. To do this he used indentation instead of curly braces to basically describe the various blocks of code. The language did not receive so much ‘love’ initially because it was slower than other languages at the time. With the rise of machine learning and artificial intelligence, python has come into the spotlight. It makes the tasks much more useful and easy to implement. With the increased computing power available currently, the focus has shifted to increased productivity even if there shall be some extra time taken when python is used.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is Python popular?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Python is the best language to learn as a first-time programmer. Its easy syntax and high-level features make it the best. It is the language for everybody. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python is an open-source language meaning it is free for anybody to use. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python can be used to make almost anything. Python can be used in GY applications, web applications, mobile applications, server-side coding, artificial intelligence, machine learning and many more. Python always finds a way to do that which you need to be done. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python has a huge library or modules support. There is a big community that comes together to make the libraries or modules that can be used to obtain any solutions. For example, machine learning algorithms are already available and you can use them to obtain better solutions. You will hence end up having higher productivity and much less coding in this case. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Features of Python&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Simplicity&lt;/em&gt;. This makes python fun. It makes you think more of your solution rather than the syntax.&lt;br&gt;
Portability. Python code can be shared among different people and devices and its working remains identical holding all other factors constant. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Interpretation&lt;/em&gt;. Python is an interpreted language because it goes through an interpreter, which turns the code into the language understood by your computer’s processor. This makes the management of CPU, memory and debugging much simpler. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;huge library support&lt;/em&gt; will help you to obtain solutions to your problems easier. You can get started off with data science, web development and others on the go with python. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Object orientation&lt;/em&gt;. You may have questions about how developers make apps that are tuned to a specific domain. This is due to the Object-Oriented Programming concepts. Python supports OOP meaning you can model a real-world problem into your code. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Where is Python used in the industry?&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.google.com"&gt;Google&lt;/a&gt; - Google uses python to provide better search results based on the ranking of websites and much more. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.dropbox.com"&gt;Dropbox&lt;/a&gt;-This cloud storage platform uses python in its server and client applications.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.netflix.com"&gt;Netflix&lt;/a&gt;-Netflix uses machine learning with python to cluster users based on their interest in shows and tailor them such that watchers stick around longer. &lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.nsa.gov/"&gt;NSA&lt;/a&gt;-NSA uses python for cybersecurity analysis, encryption and decryption. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.bittorrent.com/"&gt;BitTorrent&lt;/a&gt;-It started as a simple python file. Uses python to transfer files in a peer to peer scenario. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, &lt;a href="https://www.python.org/downloads/"&gt;download the latest version of Python&lt;/a&gt; from the download page.Check the documentation and installation manual provided there ans ensure all system requirements are met prior to installation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your "Hello World!"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a new folder. You might as well name it &lt;em&gt;helloworld&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Launch &lt;a href="https://code.visualstudio.com/"&gt;VS code&lt;/a&gt; or any other source-code editor of your choice and open the &lt;em&gt;helloworld&lt;/em&gt; folder. &lt;/p&gt;

&lt;p&gt;Create a new .py file such as &lt;em&gt;helloworld.py&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Enter the following line of code;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello, World!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The print() is an inbuilt function to display screen messages. In this case, &lt;strong&gt;Hello, World!&lt;/strong&gt; message will be shown. &lt;/p&gt;

&lt;p&gt;To execute the helloworld.py file,launch the Command Prompt if on Windows or the Terminal on macOS or Linux. VS code also has an inbuilt one. &lt;/p&gt;

&lt;p&gt;Navigate tyo the folder where the .py file is saved and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 helloworld.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to display&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that you have written your first python, welcome to the world of &lt;a href="https://docs.python.org/3/tutorial/"&gt;python programming&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>programming</category>
      <category>datascience</category>
    </item>
  </channel>
</rss>
