Python is a high-level, interpreted programming language that is widely used for web development, data analysis, artificial intelligence, and other applications.
It has a clean syntax and is easy to learn, making it a great choice for beginners as well as experienced developers.
Here’s a quick overview from Fireship.io.
Overview
Variables and Data Types
In Python, variables are used to store values such as numbers, strings, and other data types.
To assign a value to a variable, you simply use the “=” sign.
For example, to assign the value 5 to a variable called “x”, you would write:
|
|
Python has several built-in data types:
- integers (whole numbers)
- floats (decimal numbers)
- booleans (represent
True
orFalse
) - strings (used to represent text)
- lists (used to store collections of values)
|
|
Control Flow
Control flow refers to the way that a program executes its instructions based on certain conditions.
Python uses if-else
statements and loops
to control the flow of a program.
An if-else
statement is used to execute certain code if a certain condition is met, and other code if it is not.
For example:
|
|
A loop
is used to repeat a certain block of code a certain number of times, or until a certain condition is met.
For example:
|
|
Functions
Functions are used to group together a certain block of code that can be called and executed multiple times throughout a program.
A function
can take in certain parameters and can return a certain value.
Example:
|
|
Output:
|
|
Modules
Modules are used to group together certain functions and variables that can be imported and used in other Python programs.
Python has a large number of built-in modules that provide useful functionality, such as the math
module for performing mathematical calculations.
You can also create your own modules to organize your code and make it more reusable.
Example:
|
|
Output:
|
|
Web Frameworks
Frameworks provide a set of libraries and tools to help developers build web applications more quickly and easily, typically by provideing a set of features, such as routing, templating, database integration, and security, that are common to most web applications
Flask
Flask is one of the popular web frameworks for building web applications in Python, it is lightweight, flexible and provides tools and libraries for handling HTTP requests, rendering templates, managing sessions, and interacting with databases.
Django
Django, on the other hand, is a full-stack framework, meaning it provides many similar built-in tools and features as Flask, but includes more, it is usually better suited for building large, complex web applications with a lot of functionality.
Other Notable Frameworks
Package Management
The Python Package Index (PyPI) is a repository of software for the Python programming language.
PyPI helps you find and install software developed and shared by the Python community. You can learn more about installing packages on the PyPi packaging documentation.
Build a Simple Calculator
Now that we have covered some of the basics of Python, let’s create a simple calculator program.
We’ll create a program that will take in two numbers and perform simple operations on them, such as addition, subtraction, multiplication, or division.
Create Your Python File
- Create a
.py
file, in this case, we can call itsimple_calculator.py
. - Copy and paste the following code:
|
|
This should give us a simple calculator we can play around with, it utilizes Python’s built-in calculator module, which means we don’t have to define all the operators and such.
Run
|
|
You’ll be prompted to enter a first number
and a second number
, then from there you can select which operation to perform on these two numbers.
It would look something like this:
|
|
Awesome! Now you’ve built yourself a handy calculator in Python that works in your CLI!
Build a Calculator App for the Web Using Flask
What we’ll do next is re-create the calculator app in the web browser and we’ll utilize Flask to run this calculator as a web app, giving us more of a graphical way to interact with it.
Install Flask
|
|
Create Your Python File
Create your python file, in this case, we can call it web_calculator.py
, but you can name it anything you want.
|
|
Here, we have defined two routes. The first route, “/”, displays a simple HTML form that allows the user to input two numbers and select an operation.
The second route, “/calculate”, is called when the form is submitted and performs the selected operation on the two numbers.
Build the HTML file
- Create an
index.html
to include our new code for the calculator app. - Create a folder titled
templates
, and move yourindex.html
file into it.
|
|
Run
|
|
This will start a local web server that you can access in your web browser at:
|
|
Now you’ve set up a simple calculator with Python and Flask that runs on the browser!
Add In Some CSS (Optional)
If you wish to make the design of the calculator look a bit nicer, you could write up a css
file for it.
- In the same directory as your
.py
, create a folder titledstatic
. - Inside this folder create your
css
file, you can call itstyle.css
.
You can use the template below to give the web app a material design look:
|
|
Now make sure to go back to your index.html
file and link the newly created style.css
by adding it inside of the <head>
.
|
|
You should now see your calculator with a more polished look:
That’s it! You now have a simple calculator web app that you can use to perform calculations in your web browser.
You can always use your built-in calculator on your system, but we all know it’s not as cool as one that we can build!
Quick Tip
If you wish to version control your code after building such an awesome app, check out how to use Git and push it to a central source like GitHub.
Fore example, you can always check out the code for this project.
Conclusion
Overall, Python is awesome and helpful because it is versatile, easy-to-learn, and has a thriving community that supports its development and growth.
Its many benefits make it an excellent choice for anyone who wants to learn a powerful and widely-used programming language and build potentially cool stuff with it.