0% found this document useful (0 votes)
5 views19 pages

Lecture Python

The document provides an overview of the Python programming language, including its historical background, basic syntax, and fundamental concepts such as input/output functions, libraries, conditional statements, and subroutines. It highlights the purpose of the 'Hello World' program and demonstrates examples in Python, C++, and Java. Additionally, it explains the use of libraries and the importance of organizing code into functions for better reusability.

Uploaded by

thequeeloveofvt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Lecture Python

The document provides an overview of the Python programming language, including its historical background, basic syntax, and fundamental concepts such as input/output functions, libraries, conditional statements, and subroutines. It highlights the purpose of the 'Hello World' program and demonstrates examples in Python, C++, and Java. Additionally, it explains the use of libraries and the importance of organizing code into functions for better reusability.

Uploaded by

thequeeloveofvt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Information Analysis and Visualisation

Python Programming Language

University of Greenwich
Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Historical Overview
• The Python programming language was introduced by Guido van Rossum1 in the late 1980s and
early 1990s

• The initial aim was to develop a language, which combines the positive characteristics of the
general-purpose languages, such a C2, and a shell script3 (as used in a POSIX environment)

• The latest stable release of the language is version 3.11.1 (2022)

1[Link]
2The main purpose of the C programming language was to allow OS to be developed by using high level language, as
opposed to an assembly language. It is the language used in all UNIX-like OS, including Linux and MacOS
3Initially POSIX OS used Command Line Interface (CLI) with typing commands being the main interaction between the

user and the computer Konstantin Kapinchev


Information Analysis and Visualisation
Python Programming Language

• "Hello World" program in Python and other languages

• The purpose of the "Hello World" programs:

• Test the translator (compiler/interpreter)

• Test the output of the environment

• Introduce a new programming language to learners

• Illustrate basic functionality

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• "Hello World" Examples

• These are the "Hello World" programs written in 3 different programming languages, C++, Java and Python

// C++ // Java
#include <iostream>
public class Main { public static void main(String[] args) {
using namespace std; [Link]("Hello World");}
}
int main() {
cout << "Hello World!";
return 0; # Python
} print('Hello World')
Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Input and Output Functions

• The 'print' function is the basic output function in Python

• When invoked, the print function will display the content of its parameters on the console

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Input and Output Functions

• Function 'print' can be used with multiple parameters, mixing variables and textual or numerical values

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Input and Output Functions

• Textual and numeric data

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Input and Output Functions

• Formatting numerical output

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Libraries

• Libraries extend the functionalities of the programming languages

• In most cases implemented as collection of classes and functions

• Can be developed by third parties

• Using third party libraries has advantages and disadvantages

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Libraries

• Example:
• Function 'sqrt' for calculating the square root of a real number

Note:

By checking the Python documentation, we can


find out which library defines the function we
want to use:

[Link]

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Libraries

• Example:
• Function 'sqrt' for calculating the square root of a real number

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Conditional Statement

• Is the square root function defined for every real1 number? Note:

Mathematicians extended the


definition of the square root
operation in order to work with
negative values1. In this case,
square root of a negative value
is a complex number in the form
of a+bi, where i is the imaginary
unit2.
1This is reflected in the cmath library
2Some use j instead of i
1Chaging int to float to allow input of real numbers Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Conditional Statement

• Block diagram of IF statement

IF
Condition Code if true

Code if false

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Conditional Statement

• Adding the conditional statement 'if' solves the problem

Note:

Two possible outputs


depending on the value of
variable a

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Conditional Statement

• Adding the conditional statement 'if' solves the problem

Note:

Result when variable 'a'


has a negative value

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Conditional Statement
Notes:
• Adding the conditional statement 'if' solves the problem
The keywords 'if' and 'else'
are followed by colons.

The indentation of one tab


defines the scope of the 'if'
statement. This syntax is
characteristics of Python.

Also note the notation '>='


for 'greater than or equal to'.

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Subroutines

• In practice programs tend to have significant number of lines and statements

• There is a need to organise the programs into logically separated blocks

• Ideally, those blocks would be independent from particular programs, in order to be re-used in other
programs

• Most programming languages have their own approach regarding subroutines, although most of them
follow some common concepts and principles

• In general, these logically separated blocks are denoted as 'subroutines'

• Most programming languages, Python included, support subroutines Konstantin Kapinchev


Information Analysis and Visualisation
Python Programming Language

• Subroutines
Note:
• In Python subroutines are called functions
Input The indentation on the
second and third lines
defines the block of the
subroutine, or function.

Keywords We observed the same use


Output of indentation as part of the
'if' statement.

Konstantin Kapinchev
Information Analysis and Visualisation
Python Programming Language

• Subroutines
Note:
• A Python example:
Subroutines can be used in
multiple situations,
including on the right-hand
side of the equal sign.

Their parameters can be


variables or specific values.

Konstantin Kapinchev

You might also like