Introduction to Python
Programming
What is programming?
• programming means telling a computer to do something.
• A computer program is made up of a number of instructions.
• An instruction is a basic command you give to a computer, usually to
a single, very specific thing.
Why learn programming?
• Programming can be very interesting and rewarding, as a hobby or a
profession.
• To know more about how computers work and how you can make
them do what you want.
• Maybe you can’t find a program that does exactly what you want or
need it to do, so you want to write your own.
History of Python
• Python was created in the early 1990s by Guido van Rossum at the
National Research Institute for Mathematics and Computer Science in
Netherlands.
• Python was created as a successor of a language called ABC (All Basic Code)
and released publicly in1991.
• Python is derived from many other languages, including ABC, Modula-3, C,
C++, Algol-68, Smalltalk, and UNIX shell and other scripting languages.
• In 2001 the Python Software Foundation (PSF) was formed, a non-profit
organization created specifically to own Python-related Intellectual
Property
Python Versions
• There are two main branches of Python currently. Python 2.x and
Python 3.x.
• The last release of Python 2.x is 2.7.18(April 20, 2020), and the most
recent release of Python 3.x is 3.14.3.
• Python 3.x break backward compatibility with previous releases of
Python.
• It was created to correct some design flaws of the language and make
the language more clean.
Why Python programming?
• Python was created from the start to be easy to learn.
• Python is free.
• Python is open source software.
• Python is used by thousands of professionals around the world.
• Python runs on different kinds of computers.
• Python is powerful (allows programmers to get more done with less effort).
Types of Translators
1) Interpreters (e.g., Python is an interpreted language)
• Each time the program is run the interpreter translates the program.
• If there are any translation errors during the process of interpreting the program, the
program will stop.
• Advantages: partial execution, multi-platform
2) Compilers (e.g., ‘C’, C++ are compiled languages)
• Before the program is run the compiler translates the program all at once.
• Has to be perfect before you get anything to run
• If there are any translation errors during the compilation process, no machine language executable
will be produced (nothing to execute)
• If there are no translation errors during compilation then a machine language program is created
which can then be executed .
• Advantage: faster, translate once
Interpreted and (Byte-) Compiled
• Python is classified as an interpreted language, meaning that compile-
time is no longer a factor during development.
• However, Python is not a purely interpreted language. Actually, it is a
byte-compiled language, resulting in an intermediate form closer to
machine language.
Main features of Python
• High Level
• ML Assembly Fortran Pascal C C++ Java Perl Python
• Easy-to-learn
• Beginers can learn to write program with python fast and easily.
• Easy-to-maintain
• Python source code is short and need less memory space.
• Robust
• Errors can be detected and solved easily
Main features of Python
• Support both Interactive and non-interactive (script) Modes
• <Will be explained later>
• Support GUI Programming
• GUI Programming is done with the aid of graphics, menus, lists, icons, …
• Portable (Cross-Platforms)
• A program written in python can be executed in Windows, Linux, MC-OS, Solaris, ...
• Interpreted and (Byte-) Compiled
Main features of Python
• Support Databases
• Extendable
• Codes from other languages (C, java, …) can be included in a python program
• Embedded
• Codes of python can be embedded into many other language
• A broad standard library
• A library is a collection of ready-written-codes that could be imported to solve
a well-defined problems.
Python Libraries
• A library is a collection of portable codes and programs that solve
well-defined problems and can be reused and imported easily in your
program.
• Python has many and rich general and specialized frameworks in
various fields.
Applications of Python (Where to Use Python)
• Desktop Applications
• Web Applications
• Database Applications
• Networking Applications
• Games
• Data Analysis
• AI and Machine Learning
• IoT Applications
❑However, Until Now, Python is not preferable in mobile Applications
Python Packages (Frameworks)
• Instead of having all of Python functionality built into its core, you need to
install different packages for different topics.
• Lots of Python packages exists, depending on what you are going to solve.
• For example, we have Python packages for Desktop GUI Development,
Database Development, Web Development, Software Development, etc.
• In that way you also find Python packages for almost everything, from
scientific computing to website development and gaming to robotics and
spacecraft control.
• These packages need to be downloaded and installed separately.
Python Frameworks and Libraries
• Web Development Frameworks
✓Django
✓Flask
✓Pyramid
✓Tornado
✓Bottle
• Scientific Computing Frameworks
✓NumPy :
✓SciPy
✓Matplotlib
✓Pandas
Python Limitation
• Performance
• As an interpreted language, Python performance is less than compiled
language like C, C++.
• Mobile Application
• Until now, Python is not preferred nor supported in smart phone applications
Flavors (Implementations) of Python Interpreter
• CPython
• CPython is written in C and is the reference implementation of the Python. It is
the default and most widely-used implementation.
• Jython
• Jython is written in Java. A Jython program can import any Java class. It
compiles to Java bytecode.
• Brython
• Brython stands for Browser Python. It is an implementation of Python that runs
in the browser.
Flavors (Implementations) of Python Interpreter
• IronPython
• IronPython is written in C#. It can function as an extensibility layer to application
frameworks written in a .NET language.
• RubyPython
• It acts as a bridge between the Python and Ruby interpreters. It marshals data between
Python and Ruby virtual machines.
• PyPy
• Interesting to know how PyPy is Python implemented in Python. This makes it faster
and easier to experiment with. However, the standard implementation is CPython
• MicroPython
• This is an implementation of Python meant to run on a microcontroller. It uses a
MicroPython board that runs MicroPython on bare metal.
Who Uses Python
Growth of Major Programming Languages
Source
Most Loved, Dreaded, and Wanted Languages
source
The Most Popular Programming Languages
The Top Programming Languages
Compare python with other languages
• A program to display “Hello, World!”
Downloading and Installing Python
• All versions of Python for the most platforms are found at
[Link]
• Python is available on a wide variety of platforms:
➢ All UNIX flavors (Linux, MacOS X, Solaris, FreeBSD, etc.)
➢ Win32 (Windows NT, 2000, XP, etc.)
➢ Older platforms: MacOS 8/9, Windows 3.x, DOS, OS/2, AIX
➢ Handhelds (PDAs/phones): Nokia Series 60/SymbianOS, Windows CE/Pocket PC
Starting Python In Windows environment
• You can use either of these ways to start python in windows
environment:
• Through Python IDLE
• Through Python Command Prompt
Python IDLE : Introduction
• IDLE is an Integrated DeveLopment Environment for Python.
• It is an application like a word processor which helps developers to
write programs.
• It has two modes Interactive and Development.
IDLE Interactive Mode
• To start IDLE click on IDLE (Python GUI) icon
• You will see a new window opens up and the cursor is waiting beside
'>>>' sign which is called command prompt.
• This mode is called interactive mode as you can interact with IDLE
directly.
Printing “Hello World” with Interactive Mode
Python Programs (Scripts)
• Interactive Python is good for experiments and programs of 3-4 lines long
• But most programs are much longer so we type them into a file and tell
python to run the commands in the file.
• This way of writing programs is called development mode or script mode.
• In a sense we are “giving Python a script”
• As convention, we add “.py” as the suffix on the end of these files to
indicate they contain Python
Python IDLE: Development mode
1. Start with a new window
2. Type print "Hello World" in the new window.
3. save the program
4. To run the program select Run menu and click (or just press F5)
Python IDLE: Syntax Colors
Syntax Color Syntax Color
• Keywords → Orange • functions → Purple
• Strings → Green
• Comments → Red
• Definitions → Blue
Python command line interface
Python command line interface
Python’s Traditional Runtime Execution Model
1. The Python HLL 2. The whole 3. PVM (part of the Python
program(source system) sends byte code
source code is instructions one by one
code) is written compiled into to execution
and saved as [Link] “byte-code (interpretation).
• Byte code is a lower-level, platform-independent representation of your source
code. Python saves byte code like this as a startup speed optimization.
File Extensions in Python
• While developing a program in Python, many files many types of files
may be generated and each type with different extension.
• .py –The normal extension for a Python source file
• .pyc- The compiled bytecode
• .pyd- A Windows DLL file
• .pyo- A file created with optimizations
• .pyw- A Python script for Windows
• .pyz- A Python script archive.
.pyd [Python Dynamic Module]
• A .pyd file is a file that contains compiled C++ code.
• Your Python program can import the .pyd file directly without
needing to translate or interpret it.
• This is very helpful for a programmer that does not want to recode a
complicated module already written in C++ and rewrite it in Python.
.pyo [Python Optimized File]
• A .pyo file is produced from .py file by pystone tool
• Unlike .py file, a .pyo file is harder for humans to read code directly.
• Pystone optimizes the source code for faster execution.
• It removes sections of code that are never executed (Dead code
elimination)
.pyw [Python Windows Script]
• A .pyw file is very like an executable file.
• Assume that there is a Python script that displays a window with
button and text field.
• If you run with it as a .py file, it will open command prompt and
display GUI window.
• If, you run as a .pyw file, it will only display the GUI window.
.pyz [Python Compressed Script]
• A .pyz is a compressed file that may contain many types of one
project (for example, .pyc, .pyo, …)
• This will reduce the size of your project distribution package, which
makes it faster to download and deploy.
• Security – Source code not visible
Types of Errors
• Errors in programming is not avoidable.
• Programming errors are called bugs and the process of tracking them
down is called debugging.
• There are three types of errors
1. syntax errors
2. runtime errors
3. semantic errors
Syntax Error (compile time error)
• Syntax error refers to the structure of a program and the rules about
that structure.
• For example, parenthesis have to come in matching pairs, so (1+2) is
legal, but 8) is syntax error.
• If there is a single syntax error in your program, python will display an
error message and quit, and you will not be able to run the rest of
your program.
Runtime Error (exception)
• An error that causes abnormal termination of a program during
running time is called runtime error.
• Runtime error, so called because the error does not appear until the
program has started running.
• These errors are also called exceptions because they usually indicate
that something exceptional or bad have happened.
• Exp, Dividing by zero errors.
Semantic Error (logical error)
• If there is a semantic error in your program, it will run successfully(no error
messages), but it will not do the right thing.
• The problem is that the program you wrote is not the program you wanted
to write.
• Identifying semantic error can be tricky because it requires you to work
backward by looking at the output of the program and trying to figure what
is going on.
• Exp, calculating the average of the numbers x, y
• avg = x + y /2 # This statement is semantically wrong
• avg = (x + y) /2 # This is the statement