Module 06 - Basic Programming Concepts
Module 06 - Basic Programming Concepts
168
6.1. Introduction
Computer programs are collections of instructions that tell a computer how to interact
with the user, interact with the computer hardware and process data. The first programmable
computers required the programmers to write explicit instructions to directly manipulate the
hardware of the computer. This “machine language” was very tedious to write by hand since
even simple tasks such as printing some output on the screen require 10 or 20 machine language
commands. Machine language is often referred to as a “low level language” since the code
directly manipulates the hardware of the computer.
By contrast, higher level languages such as “C”, C++, Pascal, COBOL, FORTRAN,
ADA and Java are called “compiled languages”. In a compiled language, the programmer writes
more general instructions and a compiler (a special piece of software) automatically translates
these high level instructions into machine language. The machine language is then executed by
the computer. A large portion of software in use today is programmed in this fashion. We can
contrast compiled programming languages with interpreted programming languages. In an
interpreted programming language, the statements that the programmer writes are interpreted as
the program is running. This means they are translated into machine language on the fly and
then execute as the program is running. Some popular interpreted languages include Basic,
Visual Basic, Perl, Python, and shell scripting languages such as those found in the UNIX, Linux
and MacOS X environment.
b. What is a Compiler
You write your computer program using your favourite programming language
and save it in a text file called the program file. Now let us try to get a little more detail
on how the computer understands a program written by you using a programming
language. Actually, the computer cannot understand your program directly given in the
text format, so we need to convert this program in a binary format, which can be
understood by the computer. The conversion from text program to binary file is done by
another software called Compiler and this process of conversion from text formatted
program to binary format file is called program compilation. Finally, you can execute
binary file to perform the programmed task. We are not going into the details of a
compiler and the different phases of compilation.
169
Figure 6.1. - illustration of the process
So, if you are going to write your program in any such language, which needs
compilation like C, C++, Java and Pascal, etc., then you will need to install their
compilers before you start programming.
c. Interpreter
We just discussed about compilers and the compilation process. Compilers are
required in case you are going to write your program in a programming language that
needs to be compiled into binary format before its execution. There are other
programming languages such as Python, PHP, and Perl, which do not need any
compilation into binary format, rather an interpreter can be used to read such programs
line by line and execute them directly without any further conversion. So, if you are
going to write your programs in PHP, Python, Perl, Ruby, etc., then you will need to
install their interpreters before you start programming.
d. Online Compilation
If you are not able to set up any editor, compiler, or interpreter on your machine,
then [Link] provides a facility to compile and run almost all the programs
170
online with an ease of a single click. So do not worry and let's proceed further to have a
thrilling experience to become a computer programmer in simple and easy steps.
e. Variable Declaration
Variables are place holders for data a program might use or manipulate.
Variables are given names so that we can assign values to them and refer to them later
to read the values. Variables typically store values of a given type. Types generally
include:
This primer endeavours to introduce the topic of Boolean logic and demonstrates how it
is used in a variety of situations. Boolean logic is called a Two Valued Logic because an
expression may only take on one of two values: True or False. An expression is some collection
of logical operands and logical operators that are combined together. In arithmetic, the operands
are numbers and the operators are the familiar addition, subtraction, multiplication and division.
In Boolean logic, the operands are statements (that can be proven True or False) and the
operators are logical AND, OR and NOT.
For example, consider this expression: 4 < 6
Clearly this expression is true since the number four is less than the number 6.
As another example, consider the following statement:
171
I am 6 feet tall OR I am president of the United States
Notice in this case that only one of the parts of the sentence (separated by OR) need be True in
order for the entire sentence to be considered True. Another way of saying this is: It is true that
I am 6 feet tall OR I am the president of the United States.
There are three Boolean operators: AND, OR and NOT. These operators are
written differently depending on the language being used. In mathematics, the logical
operators are
Written as
The following table compares how AND OR and NOT are written in different
programming languages:
Language AND OR NOT
Mathematics
“C” or C++ && || !
Perl & || !
Java & || !
The Boolean operators are evaluated in the following fashion. For the AND
operator, the combination of two “True” values results in “True” all other combinations
evaluate to False.
For example:
True AND True evaluates to True
True AND False evaluates to False
False AND True evaluates to False
False AND False evaluates to False
For the OR operator, as long as one of the value is True, then the expression evaluates to
True:
True OR True evaluates to True
True OR False evaluates to True
False OR True evaluates to True
False OR False evaluates to False
The NOT operator is called the “complimentary” operator. It reverses the truth value of
the operand:
NOT True evaluates to False
NOT False evaluates to True.
172
b. Comparison Operators
==
or eq
Perl > or gt < or lt >= or ge <= or le != or ne
Java == > < >= <= !=
Note for Perl language comparison of numbers uses the typical mathematical
operators. Comparison of strings uses the eq, ge, le, gt, lt and ne operators.
Comparison operators are used to form expressions that can be evaluated as True or False.
For example, we might ask if there are more than 30 students in the class using the
following expression:
Number_of_Students > 30
If there are more than 30 students in the class, then this expression will evaluate
to True. If there are 30 or fewer students in the class, then this expression will evaluate
to False. The following are more examples of these types of expressions:
Question Expression
Does Alice make more than $35,000 per year? Alice_Salary > 35000
Did the NY Giants defeat the Dallas Cowboys? Giants_Points > Cowboys_Points
For each of these examples, supplying values for each of the expressions allows
us to determine if that expression is True or False. For example, if Alice only makes
$31,000, then the first expression would evaluate to False.
173
c. Combining Boolean and Comparison Operators
In the previous sections, we have seen how the Boolean operators and
Comparison operators can be used to form expressions. In this section, we combine the
two types of operators to form more complex expressions.
For example, suppose we ask the question: Are there more than 30 students in the class,
and are there more than 30 seats in the room? This might be expressed as:
Number_of_Students > 30 AND Number_of_Seats > 30
174
As an exercise, evaluate the following expressions:
Expression Evaluation
Alice_salary < 25000 OR
Alice_Department = 4
Notice in the last two cases, parenthesis may be used to group portions of the
expression. In general Boolean expressions are evaluated from left to right except where
parenthesis change the order. In the third example above, notice that when multiple
conditions are separated by OR, it only takes one of the conditions to be True in order
to cause the entire expression to evaluate to True.
d. Conditional Statements
SAD stands for Systems Analysis and Design and it is a process of identifying, analyzing,
designing and implementing computer-based information systems that will suit a particular
business. It entails identifying the information requirements of an organization and then creating
systems that can address the needs and challenges of an organization to improve its operations.
175
6.3.1 Introduction to Software Development Approaches
• Example: “It should take the system not more than 2seconds to call up the
login page.”
6.5.1 Interviews
176
• This means using face to face communications to investors to get detailed
information from them.
6.5.2 Observations
• Observing the existing systems and how the users engage with them in
order to map the utility and efficiency of the system.
6.5.3 Questionnaires
6.6.1 Purpose
• Verifiable: The requirements should be assuming this is the case then the
following attributes should characterize the requirements;
177
• Example: An SRS of a library management system where every aspect of
the system is described right from the login module of the user to the process of
cataloging the books.
Class Diagram
Level 2 Diagram
Testing the system with the user to confirm it satisfies business needs.
Example: A business conducting tests on a new inventory system before
final deployment
6.10. C#.Net
a. Introduction to C#
• Boolean Conditions
• Automatic Garbage Collection
• Standard Library
• Assembly Versioning
• Properties and Events
• Delegates and Events Management
• Easy-to-use Generics
• Indexers Conditional Compilation
• Simple Multithreading
• LINQ and Lambda Expressions
• Integration with Windows
c. Programming Environment
In this chapter, we will discuss the tools required for creating C# programming.
We have already mentioned that C# is part of .Net framework and is used for writing
.Net applications. Therefore, before discussing the available tools for running a C#
program, let us understand how C# relates to the .Net framework.
The .Net framework is a revolutionary platform that helps you to write the
following types of applications:
• Windows applications
• Web applications
• Web services
180
• Common Language Runtime (CLR)
• The .Net Framework Class Library
• Common Language Specification
• Common Type System
• Metadata and Assemblies
• Windows Forms
• [Link] and [Link] AJAX
• [Link]
• Windows Workflow Foundation (WF)
• Windows Presentation Foundation
• Windows Communication Foundation (WCF)
• LINQ
For the jobs each of these components perform, please see [Link] -
Introduction, and for details of each component, please consult Microsoft's
documentation.
The last two are freely available from Microsoft official website. Using
these tools, you can write all kinds of C# programs from simple command-line
applications to more complex applications. You can also write C# source code
files using a basic text editor like Notepad, and compile the code into assemblies
using the command-line compiler, which is again a part of the .NET Framework.
Note: You need an active internet connection for installing the express edition.
181
Eclipse: Popular for Java development but also supports other languages
through plugins.
PyCharm: Best for Python development, offering excellent features for
code completion, debugging, and testing.
NetBeans: IDE supporting Java, PHP, and HTML5, known for its ease
of use and versatility.
Suitability Examples:
Operators:
Arithmetic Operators: +, -, *, /
o Example: total = price * quantity;
Logical Operators: && (AND), || (OR), ! (NOT)
o Example: if (age > 18 && citizen)
Control Structures:
If-Else Statements:
o Example: if (temperature > 30) { /* code */ }
Loops: for, while, do-while
o Example: for (int i = 0; i < 10; i++) { /* code */ }
182
Functions (Built-in and User-Defined):
Procedures:
Modules:
Creating a Project:
o Example: In Visual Studio, create a new C# Windows Forms
Application project.
Writing Code:
o Example: Implement a login form with fields for username and
password.
Debugging:
o Example: Set breakpoints and use step-through debugging in PyCharm.
Create a project
To start, create a C# application project. The project type comes with all the template files
you need.
1. Open Visual Studio, and select Create a new project in the Start window.
183
2. In the Create a new project window, select All languages, and then
choose C# from the dropdown list. Choose Windows from the All-platforms list, and
choose Console from the All-project types list.
After you apply the language, platform, and project type filters, choose
the Console App template, and then select Next.
184
4. In the Additional information window, select .NET 8.0 for the Target
Framework field. Then, select Create.
1. In Solution Explorer, in the right pane, select [Link] to display the file in
the code editor
2. In the code editor, replace the default "Hello World" code that
says [Link]("Hello World!");.
185
6.14 Developing Desktop Applications
Developing Desktop Applications refers to the process of creating software programs that
run on a desktop or laptop computer, rather than on a web server or mobile device. This involves
designing, coding, testing, and deploying applications that provide users with a graphical
interface and are installed locally on their operating system. These applications can range from
simple tools to complex systems and typically require an understanding of programming
languages, user interface design, and interaction with the underlying operating system and
hardware.
Create a project
186
3. In Create a new project, select the Windows Forms App (.NET
Framework) template for C#.
You can refine your search to quickly get to the template you
want. For example, type Windows Forms App in the search box. Next,
select C# from the language list, and then select Windows from the
platform list.
187
Visual Studio opens your new project.
After you select your C# project template and name your project, Visual
Studio opens a form for you. A form is a Windows user interface. Create a Hello
World application by adding controls to the form. Then run the app.
If you don't see the Toolbox option, you can open it from the menu bar.
Select View > Toolbox or Ctrl+Alt+X.
188
3. Select the Button control and then drag it onto the form.
If you don't see the Properties window, you can open it from the menu bar.
Select View > Properties Window or F4.
189
5. In the Design section of the Properties window, change the name
from button1 to btnClickThis, and then select Enter.
After you add a button control to create an action, add a label control to send text to.
1. Select the Label control from the Toolbox. Then drag it onto the form and
drop it beneath the Click this button.
2. In either the Design section or the (DataBindings) section of
the Properties window, change the name of label1 to lblHelloWorld. Then
select Enter.
1. In the [Link] [Design] window, double-click the Click this button to open
the [Link] window.
190
3. Run the application
2. Several things happen. In the Visual Studio IDE, the Diagnostics Tools window
opens, and an Output window opens, too. But outside of the IDE, a Form1 dialog box
appears. It includes your Click this button and text that says label1.
3. Select the Click this button in the Form1 dialog box. Notice that the label1 text
changes to Hello World!
191
4. Close the Form1 dialog box to stop running the app.
192
6.16 Test the Developed Program Using the Test Cases Written
Running Tests:
o Example: Use pytest for running test cases in Python.
193
6.17 Designing Static Websites
Static Web Page: A web page that displays fixed content to all users, created
with HTML and CSS. It does not change unless manually updated.
Web Designing Tools: Software used for creating and editing web pages,
including text editors (e.g., Visual Studio Code), design tools (e.g., Adobe
Photoshop), and development tools (e.g., browser developer tools).
Site Map: A diagram that outlines the structure of a website, showing all pages
and their relationships.
Navigation Structures: The way users move through a website, including top
navigation bars, sidebars, and footer menus.
HTML (Hypertext Markup Language): The standard language for creating web
pages. It structures content using elements such as headings, paragraphs, links, and
forms.
CSS (Cascading Style Sheets): A stylesheet language used to control the layout
and appearance of web pages. CSS can be applied inline, internally, or externally.
Embedding Media: Incorporating objects like images, videos, and audio into
web pages using HTML tags.
These definitions provide a basic understanding of static website design concepts and
practices.
Web Page: A document on the World Wide Web, often written in HTML,
displayed in a web browser.
Website: A collection of related web pages under a single domain name, linked
together to provide a cohesive experience.
Static Web Page: Displays the same content to every visitor. Created with HTML
and CSS only, and does not change unless manually updated.
Dynamic Web Page: Content can change based on user interaction or other
factors. Requires server-side scripting languages like PHP or JavaScript.
Purpose: Determine the main goal of the website (e.g., information, sales,
portfolio).
Target Audience: Understand who will use the website and their needs.
Content: Decide what content will be displayed (text, images, videos).
Functionality: Identify any required features (forms, contact info, navigation).
Example: For a small business website, requirements might include a homepage, services
page, contact form, and a gallery of products.
Example: A site map for an e-commerce site might include Home, Products, About
Us, Contact, and Blog.
Example: An online store might have a top navigation bar with links to Home,
Shop, Cart, and Account.
Definition: The standard language for creating web pages. HTML structures the
content on the page.
Example:
195
6.19.3 HTML Forms
Example:
Example:
CSS Syntax
Example:
Inline: CSS applied directly within an HTML element using the style attribute.
Internal: CSS written within a <style> tag in the HTML document's <head>.
External: CSS stored in a separate .css file linked to the HTML document.
197
Audio: Use <audio> tag for sound files.
Other Embeds: Embed objects like PDFs using the <embed> or <object> tag.
These notes provide a structured approach to designing static websites, covering the
essential concepts and practical examples for each topic.
In this section, let’s create a full-fledged website using only HTML and CSS. Most of the
users have a question today – Can you create a website just using HTML and CSS?
It is quite possible to create a good-looking website with the help of only HTML and CSS. HTML
stands for Hypertext markup language and provides the skeleton for our website. However, CSS
(Cascading Style Sheet) allows the skeleton to be better-looking. Let us use seven steps to create
a good-looking website from scratch.
198
Step 2: Set up the boiler code
Create a new project folder and create an empty [Link] file inside the folder. Here,
add the boilerplate code to the HTML file.
<!DOCTYPE html>
<html lang="en">
<head>
199
</head>
<body>
<h1>Test</h1>
</body>
</html>
Before starting the actual content add some test content in your HTML file, and run it on
the browser to test if the code is working fine.
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<header>
/header>
<main>
<section id="intro">
200
</section>
<section id="about">
</section>
<section id="contact">
</section>
</main>
<footer>
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>How to create a website using HTML and CSS</title>
<link rel="stylesheet" href="css/[Link]">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#intro">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<main>
<section id="intro">
<div class="Container">
<img src="Images/[Link]" alt="display picture of doggo">
<h2>My name is Doggo</h2>
</div>
201
</section>
<section id="about">
<div class="container">
<h1>About Me</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Sint, similique?</p>
<ul>
<li>Btech Qualified</li>
<li>Software Engineer</li>
<li>GATE AIR 01</li>
</ul>
</div>
</section>
<section id="contact">
<div class="container">
<h1>Contact me</h1>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nam, laudantium.</p>
<ul>
<li>Email ID</li>
<li>Insta ID</li>
<li>Facebook ID</li>
</ul>
</div>
</section>
</main>
<footer>
<p>© Copyright 2022 Doggo Co LTd.</p>
</footer>
</body>
</html>
Now, if you reload the page, you are going to get an output something like this. You are
now going to give this webpage some CSS in the next step to make it good-looking.
202
Step 5: Create CSS for the layout
Before adding the depth in the CSS, let us first add some basic CSS to make our webpage
look somewhat similar to the layout that we designed in the first step.
Moreover, we linked our HTML file to a CSS file in the second step while writing our boilerplate
code. Add the basic layout CSS in the linked CSS file. In this step, we are going to focus on
height, width, padding, margin, and display of the sections and images, to make them adjustable
according to the webpage.
*{
padding: 0;
margin: 0;
}
header{
203
height: 45px;
}
header nav ul{
display: flex;
margin-left: 80%;
}
section{
height: 100vh;
border: 1px solid grey;
display: flex;
justify-content: center;
align-items: center;
}
.Container{
margin-top: 10%
}
.Container img{
height: 300px;
}
.Container h2{
margin-top: 3%;
}
footer {
line-height: 40px;
display: flex;
justify-content: center;
}
*{
padding: 0;
margin: 0;
}
header{
height: 45px;
}
204
header nav ul{
display: flex;
margin-left: 70%;
list-style: none;
}
header a{
text-decoration: none;
color: brown;
section{
height: 100vh;
border: 1px solid grey;
display: flex;
justify-content: center;
align-items: center;
}
.Container img{
height: 300px;
border-radius: 50%;
}
.Container h2{
margin-top: 2%;
font-size: 3em;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode',
Geneva, Verdana, sans-serif;
}
.Container p, ul{
margin-top: 2%;
font-size: 1.5rem;
}
footer {
line-height: 40px;
display: flex;
justify-content: center;
font-size: 1rem;
}
After completing the entire code of our website, it will look something like this. Note
that you can add more CSS to make it further good-looking.
206
6.21 Introduction to Python
207
6.21.1 Why to Learn Python?
Python is consistently rated as one of the world's most popular programming
languages. Python is fairly easy to learn, so if you are starting to learn any programming
language then Python could be your great choice. Today various Schools, Colleges and
Universities are teaching Python as their primary programming language. There are many
other good reasons which makes Python as the top choice of any programmer:
208
This programming language also extends its applications to computer vision and
image processing.
It is also favored in many tasks like Automation, Job Scheduling, GUI
development, etc.
The latest release of Python is 3.x. As mentioned before, Python is one of the most widely
used language over the web. I'm going to list few of them here:
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
Easy-to-maintain − Python's source code is fairly easy-to-maintain.
A broad standard library − Python's bulk of the library is very portable and
cross-platform compatible on UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
Extendable − You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more efficient.
Databases − Python provides interfaces to all major commercial databases.
GUI Programming − Python supports GUI applications that can be created and
ported to many system calls, libraries and windows systems, such as Windows MFC,
Macintosh, and the X Window system of Unix.
Scalable − Python provides a better structure and support for large programs than
shell scripting.
6.25 Pythonic Code Style
Python leaves you free to choose to program in an object-oriented, procedural, functional,
aspect-oriented, or even logic-oriented way. These freedoms make Python a great language to
write clean and beautiful code.
Pythonic Code Style is actually more of a design philosophy and suggests to write a code which
is :
Clean
Simple
Beautiful
209
Explicit
Readable
6.26 Evolution of Python – The Major Python Versions
Following are the important stages in the history of Python −
Python 0.9.0
Python's first published version is 0.9. It was released in February 1991. It consisted of
features such as classes with inheritance, exception handling, and core data types like lists and
dictionaries.
Python 1.0
In January 1994, version 1.0 was released, armed with functional programming tools,
features like support for complex numbers etc and module system which allows a better code
organization and reuse.
Python 2.0
Next major version − Python 2.0 was launched in October 2000. Many new features such
as list comprehension, garbage collection and Unicode support were included with it.
Throughout the 2000s, Python 2.x became the dominant version, gaining traction in industries
ranging from web development to scientific research. Various useful libraries like like NumPy,
SciPy, and Django were also developed.
Python 3.0
Python 3.0, a completely revamped version of Python was released in December 2008.
The primary objective of this revamp was to remove a lot of discrepancies that had crept in
Python 2.x versions. Python 3 was backported to Python 2.6. It also included a utility named
as python2to3 to facilitate automatic translation of Python 2 code to Python 3. Python 3
provided new syntax, unicode support and Improved integer division.
EOL for Python 2.x
Even after the release of Python 3, Python Software Foundation continued to support the
Python 2 branch with incremental micro versions till 2019. However, it decided to discontinue
the support by the end of year 2020, at which time Python 2.7.17 was the last version in the
branch.
Current Version of Python
Meanwhile, more and more features have been incorporated into Python's 3.x branch. As
of date, Python 3.11.2 is the current stable version, released in February 2023.
210
6.27 Python's most important features are as follows:
1. Easy to Learn
This is one of the most important reasons for the popularity of Python. Python has a
limited set of keywords. Its features such as simple syntax, usage of indentation to avoid clutter
of curly brackets and dynamic typing that doesn't necessitate prior declaration of variable help a
beginner to learn Python quickly and easily.
2. Dynamically Typed
Python is a dynamically typed programming language. In Python, you don't need to
specify the variable time at the time of the variable declaration. The types are specified at the
runtime based on the assigned value due to its dynamically typed feature.
3. Interpreter Based
Instructions in any programming languages must be translated into machine code for the
processor to execute them. Programming languages are either compiler based or interpreter
based.
In case of a compiler, a machine language version of the entire source program is generated. The
conversion fails even if there is a single erroneous statement. Hence, the development process is
tedious for the beginners. The C family languages (including C, C++, Java, C# etc) are compiler
based.
4. Interactive
Standard Python distribution comes with an interactive shell that works on the principle
of REPL (Read – Evaluate – Print – Loop). The shell presents a Python prompt >>>. You can
211
type any valid Python expression and press Enter. Python interpreter immediately returns the
response and the prompt comes back to read the next expression.
5. Multi-paradigm
Python is a completely object-oriented language. Everything in a Python program is
an object. However, Python conveniently encapsulates its object orientation to be used as an
imperative or procedural language – such as C. Python also provides certain functionality that
resembles functional programming. Moreover, certain third-party tools have been developed to
support other programming paradigms such as aspect-oriented and logic programming.
6. Standard Library
Even though it has a very few keywords (only Thirty-Five), Python software is
distributed with a standard library made of large number of modules and packages. Thus, Python
has out of box support for programming needs such as serialization, data compression, internet
data handling, and many more. Python is known for its batteries included approach.
Some of the Python's popular modules are:
NumPy
Pandas
Matplotlib
Tkinter
Math
7. Open Source and Cross Platform
Python is a cross-platform language. Pre-compiled binaries are available for use on
various operating system platforms such as Windows, Linux, Mac OS, Android OS. The
reference implementation of Python is called CPython and is written in C. You can download
the source code and compile it for your OS platform.
A Python program is first compiled to an intermediate platform independent byte code. The
virtual machine inside the interpreter then executes the byte code. This behaviour makes Python
a cross-platform language, and thus a Python program can be easily ported from one OS platform
to other.
212
8. GUI Applications
Python's standard distribution has an excellent graphics library called TKinter. It is a
Python port for the vastly popular GUI toolkit called TCL/Tk. You can build attractive user-
friendly GUI applications in Python. GUI toolkits are generally written in C/C++. Many of them
have been ported to Python. Examples are PyQt, WxWidgets, PySimpleGUI etc.
9. Database Connectivity
Almost any type of database can be used as a backend with the Python application. DB-
API is a set of specifications for database driver software to let Python communicate with a
relational database. With many third-party libraries, Python can also work with NoSQL
databases such as MongoDB.
10. Extensible
The term extensibility implies the ability to add new features or modify existing features.
As stated earlier, CPython (which is Python's reference implementation) is written in C. Hence
one can easily write modules/libraries in C and incorporate them in the standard library. There
are other implementations of Python such as Jython (written in Java) and IPython (written in
C#). Hence, it is possible to write and merge new functionality in these implementations with
Java and C# respectively.
11. Active Developer Community
As a result of Python's popularity and open-source nature, a large number of Python
developers often interact with online forums and conferences. Python Software Foundation also
has a significant member base, involved in the organization's mission to "Promote, Protect, and
Advance the Python Programming Language"
Python also enjoys a significant institutional support. Major IT companies Google, Microsoft,
and Meta contribute immensely by preparing documentation and other resources.
213
6.28 Difference between Python and C++
Step 1: Install Python. Make sure that Python is installed on your system or not.
Step 2: Choose Text Editor or IDE to write the code.
Step 3: Open Text Editor or IDE, create a new file, and write the code to print
Hello World.
Step 4: Save the file with a file name and extension ".py".
214
Step 5: Compile/Run the program.
In the above code, we wrote two lines. The first line is the Python comment that
will be ignored by the Python interpreter, and the second line is the print() statement that
will print the given message ("Hello World") on the output screen.
Output
This tutorial will teach you How Python Interpreter Works in interactive and scripted
mode. Python code is executed by one statement at a time method. Python interpreter has two
215
components. The translator checks the statement for syntax. If found correct, it generates an
intermediate byte code. There is a Python virtual machine which then converts the byte code in
native binary and executes it. The following diagram illustrates the mechanism:
To close the interactive session, enter the end-of-line character (ctrl+D for Linux
and ctrl+Z for Windows). You may also type quit() in front of the Python prompt and
press Enter to return to the OS prompt.
The interactive shell available with standard Python distribution is not equipped
with features like line editing, history search, auto-completion etc. You can use other
advanced interactive interpreter software such as IPython and bpython to have
additional functionalities.
216
6.30.2 Python Interpreter - Scripting Mode
Instead of entering and obtaining the result of one instruction at a time as in the
interactive environment, it is possible to save a set of instructions in a text file, make sure
that it has .py extension, and use the name as the command line parameter for Python
command.
Save the following lines as [Link], with the use of any text editor such as vim on Linux
or Notepad on Windows.
217
You can now execute the script directly, without using it as a command-line argument.
The Python syntax defines a set of rules that are used to create a Python Program. The
Python Programming Language Syntax has many similarities to Perl, C, and Java Programming
Languages. However, there are some definite differences between the languages.
First Python Program
Let us execute a Python program to print "Hello, World!" in two different modes of Python
Programming. (a) Interactive Mode Programming (b) Script Mode Programming.
Python - Interactive Mode Programming
We can invoke a Python interpreter from command line by typing python at the command
prompt as following –
Here >>> denotes a Python Command Prompt where you can type your commands. Let's type
the following text at the Python prompt and press the Enter –
If you are running older version of Python, like Python 2.4.x, then you would need to use
print statement without parenthesis as in print "Hello, World!". However, in Python version
3.x, this produces the following result −
218
assume that you have Python interpreter path set in PATH variable. Now, let's try to run this
program as follows −
The following list shows the Python keywords. These are reserved words and you cannot
use them as constant or variable or any other identifier names. All the Python keywords contain
lowercase letters only.
Python programming provides no braces to indicate blocks of code for class and
function definitions or flow control. Blocks of code are denoted by line indentation, which is
rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be
indented the same amount. For example −
219
6.34 Python – Variables
Python variables are the reserved memory locations used to store values with in a Python
Program. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, Python interpreter allocates memory and decides what can
be stored in the reserved memory. Therefore, by assigning different data types to Python
variables, you can store integers, decimals or characters in these variables.
6.34.1 Creating Python Variables
Python variables do not need explicit declaration to reserve memory space or you
can say to create a variable. A Python variable is created automatically when you assign
a value to it. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand
to the right of the = operator is the value stored in the variable. For example –
Example to Create Python Variables This example creates different types (an
integer, a float, and a string) of variables.
220
6.34.3. Deleting Python Variables
can delete the reference to a number object by using the del statement. The
syntax of the del statement is −
Python data types are actually classes, and the defined variables are their instances or
objects. Since Python is dynamically typed, the data type of a variable is determined at runtime
based on the assigned value.
In general, the data types are used to define the type of a variable. It represents the type of data
we are going to store in a variable and determines what operations can be done on it.
Each programming language has its own classification of data items. With these datatypes, we
can store different types of data values.
Types of Data Types in Python
Python supports the following built-in data types −
Python operators are special symbols used to perform specific operations on one or more
operands. The variables, values, or expressions can be used as operands. For example, Python's
221
addition operator (+) is used to perform addition operations on two variables, values, or
expressions.
The following table contains all arithmetic operators with their symbols, names,
and examples (assume that the values of a and b are 10 and 20, respectively) −
222
6.36.3. Python Comparison Operators
Python Comparison operators compare the values on either side of them and
decide the relation among them. They are also called Relational operators.
The following table contains all comparison operators with their symbols,
names, and examples (assume that the values of a and b are 10 and 20, respectively) –
223