Software Environment
Software Environment
It is also a dynamically typed language that includes (but does not require
one to use) object-oriented features.
NASA has used Python for its software systems and has adopted it as the
standard scripting language for its Integrated Planning System.
History of Python
Python was created by Guido Van Rossum.
The design began in the late 1980s and was first released in February 1991.
Why the name Python?
No. It wasn't named after a dangerous snake. Rossum was fan of a comedy
series from late 70s. The name "Python" was adopted from the same series
"Monty Python's Flying Circus".
1
Python Version History
Implementation started - December 1989
Internal releases – 1990
2
Features of Python Programming
You can freely use and distribute Python, even for commercial use.
Not only you can use and distribute software’s written in it, you can
even make changes to the Python's source code.
Python has a large community constantly improving it in each
iteration.
3
3. Portability
You can move Python programs from one platform to another and
run it without any changes.
It runs seamlessly on almost all platforms including Windows, Mac
OS and Linux.
Unlike C/C++, you don't have to worry about daunting tasks like
memory management, garbage collection and so on.
Likewise, when you run Python code, it automatically converts your
code to the language your computer understands. You don't need to
worry about any lower-level operations.
4
7. Object-oriented
a=2
b=3
sum = a + b
print(sum)
Even if you have never programmed before, you can easily guess
that this program adds two numbers and prints it.
You don't need to define the type of a variable in Python. Also, it's
not necessary to add semicolon at the end of the statement.
Python enforces you to follow good practices (like proper
indentation). These small things can make learning much easier for
beginners.
5
3. Expressiveness of the language
Python vs PHP
From the development point of view, PHP is a web-oriented language.
Choosing between Python or PHP for web applications pay attention to these
characteristics:
6
Python vs Java
7
Python vs C#
In terms of the first language, Ruby and Python are the most popular ones.
Ruby is extremely popular technology for building websites. Among the most
famous are Twitter (the early version), Basecamp, Github, Airbnb, Slideshare and
Groupon.
8
Installing and Running Python in Windows
1. Go to Download Python page on the official site and click Download
Python 3.7 (You may see different version name).
2. When the download is completed, double-click the file and follow the
instructions to install it.
4. print("Hello, World!")
9
6. Write Python code (you can copy the code below for now) and save
(Shortcut: Ctrl+S) with .py file extension like: hello.py or your-first-
program.py
print("Hello, World!")
7. Go to Run > Run module (Shortcut: F5) and you can see the output.
Congratulations, you've successfully run your first Python program.
Interactive mode: is a command line shell which gives immediate output for
each statement, while running previously statements in active memory.
This mode is also referred as REPL (Read Evaluate Print Loop)
10
Normal mode: is where the scripted python file (.py) run in the Python
interpreter.
11
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
The way to run a python file is like this on the command line:
Let's write our first Python file, called helloworld.py, which can be done in any
text editor.
helloworld.py
print("Hello, World!")
Simple as that. Save your file. Open your command line, navigate to the directory
where you saved your file, and run:
12
Hello, World!
Congratulations, you have written and executed your first Python program.
To test a short amount of code in python sometimes it is quickest and easiest not
to write the code in a file. This is made possible because Python can be run as a
command line itself.
C:\Users\Your Name>python
From there you can write any python, including our hello world example from
earlier in the tutorial:
C:\Users\YourName>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit
(Intel)] onwin 32 Type "help", "copyright", "credits" or "license" for more
information.
Whenever you are done in the python command line, you can simply type the
following to quit the python command line interface:
exit()
13
Python - GUI Programming (Tkinter)
Python provides various options for developing graphical user interfaces (GUIs).
Most important are listed below.
Tkinter Programming
Python when combined with Tkinter provides a fast and easy way to create GUI
applications.
14
Tkinter Widgets
tkinter provides various controls, such as buttons, labels and text boxes used in a
GUI application. These controls are commonly called widgets.
widgets in Tkinter.
15
The Menubutton widget is used to display menus in your application.
9 Menu
The Menu widget is used to provide various commands to a user. These
commands are contained inside Menubutton.
10 Message
The Message widget is used to display multiline text fields for accepting
values from a user.
11 Radiobutton
The Radiobutton widget is used to display several options as radio
buttons. The user can select only one option at a time.
12 Scale
The Scale widget is used to provide a slider widget.
13 Scrollbar
The Scrollbar widget is used to add scrolling capability to various
widgets, such as list boxes.
14 Text
The Text widget is used to display text in multiple lines.
15 Toplevel
The Toplevel widget is used to provide a separate window container.
16 Spinbox
The Spinbox widget is a variant of the standard Tkinter Entry widget,
which can be used to select from a fixed number of values.
17 PanedWindow
A PanedWindow is a container widget that may contain any number of
panes, arranged horizontally or vertically.
18 LabelFrame
A labelframe is a simple container widget. Its primary purpose is to act
as a spacer or container for complex window layouts.
19 MessageBox
16
This module is used to display message boxes in your applications.
Standard attributes
Let us look at how some of their common attributes. such as sizes, colors and
fonts are specified.
Dimensions
Colors
Fonts
Anchors
Relief styles
Bitmaps
Cursors
Geometry Management
Tkinter contains the following geometry manager classes: pack, grid, and place.
17
Why Django?
It provides a set of tools and functionalities that solves many common problems
associated with Web development, such as security features, database access,
sessions, template processing, URL routing, internationalization, localization,
and much more.
Installation
The first thing we need to do is install some programs on our machine so to be
able to start playing with Django. The basic setup consists of installing
Python
Virtualenv
Django
18
Installing Virtualenv
we are going to use pip, a tool to manage and install Python packages, to
install virtualenv.
From now on, everything we install, including Django itself, will be installed
inside a Virtual Environment.
mkdir myproject
cd myproject
This folder is the higher level directory that will store all the files and things
related to our Django project, including its virtual environment.
let’s start by creating our very first virtual environment and installing Django.
virtualenv myvenv
myvenv\Scripts\activate
You will know it worked if you see (venv) in front of the command line, like
this:
19
to deactivate the venv run the command below:
venv\Scripts\deactivate.bat
Installing Django
Now that we have the venv activated, run the following command to install
Django:
20
Starting a New Project
To start a new Django project, run the command below:
After we run the command above, it will generate the base folder structure for a
Django project.
We will use it to run the development server, run tests, create migrations
and much more.
21
o __init__.py: this empty file tells Python that this folder is a Python
package.
o settings.py: this file contains all the project’s configuration.
o urls.py: this file is responsible for mapping the routes and paths in our
project.
For example, if you want to show something in the URL /about/, you
have to map it here first.
You don’t have to bother about it. Just let it be for now.
It’s very convenient during the development, so we don’t have to install anything
else to run the project locally.
For now, you can ignore the migration errors; we will get to that later.
Now open the following URL in a Web browser: http://127.0.0.1:8000 and you
should see the following page:
22
Hit CTRL + BREAK to stop the development server.
Django Apps
In the Django philosophy we have two important concepts:
It’s important to note that you can’t run a Django app without a project. Simple
websites like a blog can be written entirely inside a single app, which could be
named blog or weblog for example.
23
let’s create a simple Web Forum or Discussion Board. To create our first app, go
to the directory where the manage.py file is and executes the following
command:
o migrations/: here Django store some files to keep track of the changes you
create in the models.py file, so to keep the database and
the models.py synchronized.
o admin.py: this is a configuration file for a built-in Django app
called Django Admin.
o apps.py: this is a configuration file of the app itself.
o models.py: here is where we define the entities of our Web application.
The models are translated automatically by Django into database tables.
o tests.py: this file is used to write unit tests for the app.
o views.py: this is the file where we handle the request/response cycle of our
Web application.
24
Now that we created our first app, let’s configure our project to use it.
To do that, open the settings.py and try to find the INSTALLED_APPS variable:
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
As you can see, Django already come with 6 built-in apps installed. They offer
common functionalities that most Web applications need, like authentication,
sessions, static files management (images, javascripts, css, etc.) and so on.
Hello, World!
Let’s write our first view. We will explore it in great detail in the next tutorial.
But for now, let’s just experiment how it looks like to create a new page with
Django.
Open the views.py file inside the boards app, and add the following code:
views.py
25
def home(request):
return HttpResponse('Hello, World!')
Views are Python functions that receive an HttpRequest object and returns
an HttpResponse object. Receive a request as a parameter and returns
a response as a result. That’s the flow you have to keep in mind!
So, here we defined a simple view called home which simply returns a message
saying Hello, World!.
Now we have to tell Django when to serve this view. It’s done inside
the urls.py file:
urls.py
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^admin/', admin.site.urls),
]
If you compare the snippet above with your urls.py file, you will notice I added
the following new line: url(r'^$', views.home, name='home') and
imported the views module from our app boards using from boards
import views.
As I mentioned before, we will explore those concepts in great detail later on.
26
But for now, Django works with regex to match the requested URL. For
our home view, I’m using the ^$ regex, which will match an empty path, which
is the homepage (this url: http://127.0.0.1:8000). If I wanted to match the
URL http://127.0.0.1:8000/homepage/, my url would be:
27